on master help sign in
Add almost-certainly-terrible benchmarks.
commit05c49ffbbb0d476c05352560fd41690c6626369d authorWill Jones <will@sacharissa.co.uk> dateMon 31 Jul 2017, 08:59 (9 years ago) parent28e2092
2 files changed+97
benchmarks/Bench.hs+64 generic-lens.cabal+33
benchmarks/Bench.hsnew file+64
@@ -0,0 +1,64 @@
1+{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE DeriveGeneric #-}
3+{-# LANGUAGE NamedFieldPuns #-}
4+{-# LANGUAGE RankNTypes #-}
5+{-# LANGUAGE RecordWildCards #-}
6+{-# LANGUAGE TypeApplications #-}
7+
8+module Main (main) where
9+
10+import Control.DeepSeq
11+import Control.Lens.Operators
12+import Control.Lens.Type
13+import Control.Monad
14+import Criterion.Main
15+import Data.Generics.Product
16+import GHC.Generics
17+import Test.QuickCheck
18+
19+main :: IO ()
20+main = defaultMain
21+ [ env (arbitraryAnimalsOfLength 100) $ products 100
22+ , env (arbitraryAnimalsOfLength 1000) $ products 1000
23+ , env (arbitraryAnimalsOfLength 10000) $ products 10000
24+ , env (arbitraryAnimalsOfLength 100000) $ products 100000
25+ ]
26+
27+products :: Int -> [Animal] -> Benchmark
28+products n as
29+ = bgroup ("products/" ++ show n)
30+ [ bench "generic-lens/get" (nf (const $ map (^. field @"name") as) ())
31+ , bench "lens/get" (nf (const $ map (^. aName) as) ())
32+ , bench "generic-lens/set" (nf (const $ map (\a -> a & field @"name" .~ "Name") as) ())
33+ , bench "lens/set" (nf (const $ map (\a -> a & aName .~ "Name") as) ())
34+ ]
35+
36+arbitraryAnimalsOfLength :: Int -> IO [Animal]
37+arbitraryAnimalsOfLength n
38+ = replicateM n (generate arbitrary)
39+
40+data Animal = Animal
41+ { name :: String
42+ , age :: Int
43+ , eats :: String
44+ } deriving (Generic, Show)
45+
46+instance Arbitrary Animal where
47+ arbitrary = Animal <$> arbitrary <*> arbitrary <*> arbitrary
48+
49+instance NFData Animal where
50+ rnf Animal{..} = rnf name `seq` rnf age `seq` rnf eats
51+
52+aName :: Lens' Animal String
53+aName f Animal{..}
54+ = (\x -> Animal { name = x, age, eats }) <$> f name
55+
56+{-
57+aAge :: Lens' Animal Int
58+aAge f Animal{..}
59+ = (\x -> Animal { name, age = x, eats }) <$> f age
60+
61+aEats :: Lens' Animal String
62+aEats f Animal{..}
63+ = (\x -> Animal { name, age, eats = x }) <$> f eats
64+ -}
generic-lens.cabal+33
@@ -102,3 +102,36 @@ test-suite generic-lens-test
102102 , UndecidableInstances
103103 default-language:
104104 Haskell2010
105+
106+benchmark generic-lens-bench
107+ type:
108+ exitcode-stdio-1.0
109+ hs-source-dirs:
110+ benchmarks
111+ main-is:
112+ Bench.hs
113+
114+ build-depends: generic-lens
115+
116+ , QuickCheck
117+ , base
118+ , criterion
119+ , deepseq
120+ , lens
121+
122+ ghc-options:
123+ -Wall
124+ other-extensions: AllowAmbiguousTypes
125+ , DataKinds
126+ , FlexibleInstances
127+ , FunctionalDependencies
128+ , PolyKinds
129+ , Rank2Types
130+ , ScopedTypeVariables
131+ , TypeApplications
132+ , TypeFamilies
133+ , TypeOperators
134+ , UndecidableInstances
135+
136+ default-language:
137+ Haskell2010