on master help sign in
Merge pull request #447 from phadej/old-version-bench
5459273Adam Bergmark
.travis.yml+1−1
@@ -34,7 +34,7 @@ matrix:
34 - env: BUILD=cabal CABALVER=1.24 GHCVER=8.0.1
34 - env: BUILD=cabal CABALVER=1.24 GHCVER=8.0.1
35 compiler: ": #GHC 8.0.1"
35 compiler: ": #GHC 8.0.1"
36 addons: {apt: {packages: [cabal-install-1.24,ghc-8.0.1], sources: [hvr-ghc]}}
36 addons: {apt: {packages: [cabal-install-1.24,ghc-8.0.1], sources: [hvr-ghc]}}
37− - env: BUILD=stack CABALVER=1.22 GHCVER=7.10.3
37+ - env: BUILD=stack CABALVER=1.22 GHCVER=7.10.3 STACK_YAML=stack-bench.yaml
38 compiler: ": #GHC 7.10.3"
38 compiler: ": #GHC 7.10.3"
39 addons: {apt: {packages: [cabal-install-1.22,ghc-7.10.3], sources: [hvr-ghc]}}
39 addons: {apt: {packages: [cabal-install-1.22,ghc-7.10.3], sources: [hvr-ghc]}}
40
40
Data/Aeson/Types/ToJSON.hs+2−2
@@ -1870,10 +1870,10 @@ instance ToJSON1 Vector where
1870 {-# INLINE liftToEncoding #-}
1870 {-# INLINE liftToEncoding #-}
1871
1871
1872instance (ToJSON a) => ToJSON (Vector a) where
1872instance (ToJSON a) => ToJSON (Vector a) where
1873− toJSON = Array . V.map toJSON
1873+ toJSON = toJSON1
1874 {-# INLINE toJSON #-}
1874 {-# INLINE toJSON #-}
1875
1875
1876− toEncoding = encodeVector
1876+ toEncoding = toEncoding1
1877 {-# INLINE toEncoding #-}
1877 {-# INLINE toEncoding #-}
1878
1878
1879encodeVector :: (ToJSON a, VG.Vector v a) => v a -> Encoding
1879encodeVector :: (ToJSON a, VG.Vector v a) => v a -> Encoding
benchmarks/AesonFoldable.hs+49−20
@@ -1,6 +1,9 @@
1−{-# LANGUAGE OverloadedStrings #-}
1+{-# LANGUAGE OverloadedStrings, PackageImports #-}
2import Criterion.Main
2import Criterion.Main
3+
4+import qualified "aeson" Data.Aeson as A
3−import Data.Aeson
5+import qualified "aeson-benchmarks" Data.Aeson as B
6+
4import Data.Foldable (toList)
7import Data.Foldable (toList)
5
8
6import qualified Data.Sequence as S
9import qualified Data.Sequence as S
@@ -13,9 +16,13 @@ import qualified Data.Vector.Unboxed as U
13
16
14newtype L f = L { getL :: f Int }
17newtype L f = L { getL :: f Int }
15
18
16−instance Foldable f => ToJSON (L f) where
19+instance Foldable f => B.ToJSON (L f) where
17 toJSON = error "do not use this"
20 toJSON = error "do not use this"
21+ toEncoding = B.toEncoding . toList . getL
22+
23+instance Foldable f => A.ToJSON (L f) where
24+ toJSON = error "do not use this"
18− toEncoding = toEncoding . toList . getL
25+ toEncoding = A.toEncoding . toList . getL
19
26
20-------------------------------------------------------------------------------
27-------------------------------------------------------------------------------
21-- Foldable
28-- Foldable
@@ -23,9 +30,13 @@ instance Foldable f => ToJSON (L f) where
23
30
24newtype F f = F { getF :: f Int }
31newtype F f = F { getF :: f Int }
25
32
26−instance Foldable f => ToJSON (F f) where
33+instance Foldable f => B.ToJSON (F f) where
34+ toJSON = error "do not use this"
35+ toEncoding = B.foldable . getF
36+
37+instance Foldable f => A.ToJSON (F f) where
27 toJSON = error "do not use this"
38 toJSON = error "do not use this"
28− toEncoding = foldable . getF
39+ toEncoding = A.foldable . getF
29
40
30-------------------------------------------------------------------------------
41-------------------------------------------------------------------------------
31-- Values
42-- Values
@@ -47,34 +58,52 @@ valueUVector = U.fromList valueList
47-- Main
58-- Main
48-------------------------------------------------------------------------------
59-------------------------------------------------------------------------------
49
60
50−benchEncode
61+benchEncodeA
62+ :: A.ToJSON a
63+ => String
64+ -> a
65+ -> Benchmark
66+benchEncodeA name val
67+ = bench ("A " ++ name) $ nf A.encode val
68+
69+benchEncodeB
51− :: ToJSON a
70+ :: B.ToJSON a
52 => String
71 => String
53 -> a
72 -> a
54 -> Benchmark
73 -> Benchmark
55−benchEncode name val
74+benchEncodeB name val
56− = bench name $ nf encode val
75+ = bench ("B " ++ name) $ nf B.encode val
57
76
58main :: IO ()
77main :: IO ()
59main = defaultMain
78main = defaultMain
60 [ bgroup "encode"
79 [ bgroup "encode"
61 [ bgroup "List"
80 [ bgroup "List"
62− [ benchEncode "-" valueList
81+ [ benchEncodeB "-" valueList
63− , benchEncode "L" $ L valueList
82+ , benchEncodeB "L" $ L valueList
83+ , benchEncodeB "F" $ F valueList
84+ , benchEncodeA "-" valueList
85+ , benchEncodeA "L" $ L valueList
64− , benchEncode "F" $ F valueList
86+ , benchEncodeA "F" $ F valueList
65 ]
87 ]
66 , bgroup "Seq"
88 , bgroup "Seq"
67− [ benchEncode "-" valueSeq
89+ [ benchEncodeB "-" valueSeq
68− , benchEncode "L" $ L valueSeq
90+ , benchEncodeB "L" $ L valueSeq
91+ , benchEncodeB "F" $ F valueSeq
92+ , benchEncodeA "-" valueSeq
93+ , benchEncodeA "L" $ L valueSeq
69− , benchEncode "F" $ F valueSeq
94+ , benchEncodeA "F" $ F valueSeq
70 ]
95 ]
71 , bgroup "Vector"
96 , bgroup "Vector"
72− [ benchEncode "-" valueVector
97+ [ benchEncodeB "-" valueVector
73− , benchEncode "L" $ L valueVector
98+ , benchEncodeB "L" $ L valueVector
99+ , benchEncodeB "F" $ F valueVector
100+ , benchEncodeA "-" valueVector
101+ , benchEncodeA "L" $ L valueVector
74− , benchEncode "F" $ F valueVector
102+ , benchEncodeA "F" $ F valueVector
75 ]
103 ]
76 , bgroup "Vector.Unboxed"
104 , bgroup "Vector.Unboxed"
105+ [ benchEncodeB "-" valueUVector
77− [ benchEncode "-" valueUVector
106+ , benchEncodeA "-" valueUVector
78 ]
107 ]
79 ]
108 ]
80 ]
109 ]
benchmarks/AesonMap.hs+59−39
@@ -1,9 +1,7 @@
1−{-# LANGUAGE OverloadedStrings, RankNTypes #-}
1+{-# LANGUAGE OverloadedStrings, RankNTypes, PackageImports, FlexibleContexts #-}
2
2
3import Control.DeepSeq
3import Control.DeepSeq
4import Criterion.Main
4import Criterion.Main
5−import Data.Aeson
6−import Data.Aeson.Types (fromJSONKeyCoerce)
7import Data.Hashable
5import Data.Hashable
8import Data.Proxy (Proxy (..))
6import Data.Proxy (Proxy (..))
9import Data.Tagged (Tagged (..))
7import Data.Tagged (Tagged (..))
@@ -12,6 +10,11 @@ import qualified Data.Text as T
12import qualified Data.Map as M
10import qualified Data.Map as M
13import qualified Data.HashMap.Strict as HM
11import qualified Data.HashMap.Strict as HM
14
12
13+import qualified "aeson" Data.Aeson as A
14+
15+import qualified "aeson-benchmarks" Data.Aeson as B
16+import qualified "aeson-benchmarks" Data.Aeson.Types as B (fromJSONKeyCoerce)
17+
15value :: Int -> HM.HashMap T.Text T.Text
18value :: Int -> HM.HashMap T.Text T.Text
16value n = HM.fromList $ map f [1..n]
19value n = HM.fromList $ map f [1..n]
17 where
20 where
@@ -35,10 +38,11 @@ instance NFData T1 where
35 rnf (T1 t) = rnf t
38 rnf (T1 t) = rnf t
36instance Hashable T1 where
39instance Hashable T1 where
37 hashWithSalt salt (T1 t) = hashWithSalt salt t
40 hashWithSalt salt (T1 t) = hashWithSalt salt t
41+
38−instance FromJSON T1 where
42+instance B.FromJSON T1 where
39− parseJSON = withText "T1" $ pure . T1
43+ parseJSON = B.withText "T1" $ pure . T1
40−instance FromJSONKey T1 where
44+instance B.FromJSONKey T1 where
41− fromJSONKey = FromJSONKeyText T1
45+ fromJSONKey = B.FromJSONKeyText T1
42
46
43-------------------------------------------------------------------------------
47-------------------------------------------------------------------------------
44-- Coerce
48-- Coerce
@@ -51,10 +55,11 @@ instance NFData T2 where
51 rnf (T2 t) = rnf t
55 rnf (T2 t) = rnf t
52instance Hashable T2 where
56instance Hashable T2 where
53 hashWithSalt salt (T2 t) = hashWithSalt salt t
57 hashWithSalt salt (T2 t) = hashWithSalt salt t
58+
54−instance FromJSON T2 where
59+instance B.FromJSON T2 where
55− parseJSON = withText "T2" $ pure . T2
60+ parseJSON = B.withText "T2" $ pure . T2
56−instance FromJSONKey T2 where
61+instance B.FromJSONKey T2 where
57− fromJSONKey = fromJSONKeyCoerce
62+ fromJSONKey = B.fromJSONKeyCoerce
58
63
59-------------------------------------------------------------------------------
64-------------------------------------------------------------------------------
60-- TextParser
65-- TextParser
@@ -67,10 +72,11 @@ instance NFData T3 where
67 rnf (T3 t) = rnf t
72 rnf (T3 t) = rnf t
68instance Hashable T3 where
73instance Hashable T3 where
69 hashWithSalt salt (T3 t) = hashWithSalt salt t
74 hashWithSalt salt (T3 t) = hashWithSalt salt t
75+
70−instance FromJSON T3 where
76+instance B.FromJSON T3 where
71− parseJSON = withText "T3" $ pure . T3
77+ parseJSON = B.withText "T3" $ pure . T3
72−instance FromJSONKey T3 where
78+instance B.FromJSONKey T3 where
73− fromJSONKey = FromJSONKeyTextParser (pure . T3)
79+ fromJSONKey = B.FromJSONKeyTextParser (pure . T3)
74
80
75-------------------------------------------------------------------------------
81-------------------------------------------------------------------------------
76-- Values
82-- Values
@@ -83,30 +89,40 @@ value1000 = value 1000
83value10000 = value 10000
89value10000 = value 10000
84
90
85encodedValue10 :: LBS.ByteString
91encodedValue10 :: LBS.ByteString
86−encodedValue10 = encode $ value10
92+encodedValue10 = B.encode $ value10
87
93
88encodedValue100 :: LBS.ByteString
94encodedValue100 :: LBS.ByteString
89−encodedValue100 = encode $ value100
95+encodedValue100 = B.encode $ value100
90
96
91encodedValue1000 :: LBS.ByteString
97encodedValue1000 :: LBS.ByteString
92−encodedValue1000 = encode $ value1000
98+encodedValue1000 = B.encode $ value1000
93
99
94encodedValue10000 :: LBS.ByteString
100encodedValue10000 :: LBS.ByteString
95−encodedValue10000 = encode $ value10000
101+encodedValue10000 = B.encode $ value10000
96
102
97-------------------------------------------------------------------------------
103-------------------------------------------------------------------------------
98-- Helpers
104-- Helpers
99-------------------------------------------------------------------------------
105-------------------------------------------------------------------------------
100
106
101−decodeHM
107+decodeHMB
102− :: (FromJSONKey k, Eq k, Hashable k)
108+ :: (B.FromJSONKey k, Eq k, Hashable k)
103 => Proxy k -> LBS.ByteString -> Maybe (HM.HashMap k T.Text)
109 => Proxy k -> LBS.ByteString -> Maybe (HM.HashMap k T.Text)
104−decodeHM _ = decode
110+decodeHMB _ = B.decode
111+
112+decodeHMA
113+ :: (A.FromJSON (HM.HashMap k T.Text), Eq k, Hashable k)
114+ => Proxy k -> LBS.ByteString -> Maybe (HM.HashMap k T.Text)
115+decodeHMA _ = A.decode
116+
117+decodeMapB
118+ :: (B.FromJSONKey k, Ord k)
119+ => Proxy k -> LBS.ByteString -> Maybe (M.Map k T.Text)
120+decodeMapB _ = B.decode
105
121
106−decodeMap
122+decodeMapA
107− :: (FromJSONKey k, Ord k)
123+ :: (A.FromJSON (M.Map k T.Text), Ord k)
108 => Proxy k -> LBS.ByteString -> Maybe (M.Map k T.Text)
124 => Proxy k -> LBS.ByteString -> Maybe (M.Map k T.Text)
109−decodeMap _ = decode
125+decodeMapA _ = A.decode
110
126
111proxyText :: Proxy T.Text
127proxyText :: Proxy T.Text
112proxyText = Proxy
128proxyText = Proxy
@@ -132,14 +148,15 @@ benchDecodeHM
132 -> LBS.ByteString
148 -> LBS.ByteString
133 -> Benchmark
149 -> Benchmark
134benchDecodeHM name val = bgroup name
150benchDecodeHM name val = bgroup name
135− [ bench "Text" $ nf (decodeHM proxyText) val
151+ [ bench "Text" $ nf (decodeHMB proxyText) val
136− , bench "Identity" $ nf (decodeHM proxyT1) val
152+ , bench "Identity" $ nf (decodeHMB proxyT1) val
137− , bench "Coerce" $ nf (decodeHM proxyT2) val
153+ , bench "Coerce" $ nf (decodeHMB proxyT2) val
138− , bench "Parser" $ nf (decodeHM proxyT3) val
154+ , bench "Parser" $ nf (decodeHMB proxyT3) val
155+ , bench "aeson-0.11" $ nf (decodeHMA proxyText) val
139− , bench "Tagged Text" $ nf (decodeHM $ proxyTagged proxyText) val
156+ , bench "Tagged Text" $ nf (decodeHMB $ proxyTagged proxyText) val
140− , bench "Tagged Identity" $ nf (decodeHM $ proxyTagged proxyT1) val
157+ , bench "Tagged Identity" $ nf (decodeHMB $ proxyTagged proxyT1) val
141− , bench "Tagged Coerce" $ nf (decodeHM $ proxyTagged proxyT2) val
158+ , bench "Tagged Coerce" $ nf (decodeHMB $ proxyTagged proxyT2) val
142− , bench "Tagged Parser" $ nf (decodeHM $ proxyTagged proxyT3) val
159+ , bench "Tagged Parser" $ nf (decodeHMB $ proxyTagged proxyT3) val
143 ]
160 ]
144
161
145benchDecodeMap
162benchDecodeMap
@@ -147,10 +164,11 @@ benchDecodeMap
147 -> LBS.ByteString
164 -> LBS.ByteString
148 -> Benchmark
165 -> Benchmark
149benchDecodeMap name val = bgroup name
166benchDecodeMap name val = bgroup name
150− [ bench "Text" $ nf (decodeMap proxyText) val
167+ [ bench "Text" $ nf (decodeMapB proxyText) val
151− , bench "Identity" $ nf (decodeMap proxyT1) val
168+ , bench "Identity" $ nf (decodeMapB proxyT1) val
152− , bench "Coerce" $ nf (decodeMap proxyT2) val
169+ , bench "Coerce" $ nf (decodeMapB proxyT2) val
153− , bench "Parser" $ nf (decodeMap proxyT3) val
170+ , bench "Parser" $ nf (decodeMapB proxyT3) val
171+ , bench "aeson-0.11" $ nf (decodeMapA proxyText) val
154 ]
172 ]
155
173
156benchEncodeHM
174benchEncodeHM
@@ -158,7 +176,8 @@ benchEncodeHM
158 -> HM.HashMap T.Text T.Text
176 -> HM.HashMap T.Text T.Text
159 -> Benchmark
177 -> Benchmark
160benchEncodeHM name val = bgroup name
178benchEncodeHM name val = bgroup name
179+ [ bench "Text" $ nf B.encode val
161− [ bench "Text" $ nf encode val
180+ , bench "aeson-0.11" $ nf A.encode val
162 ]
181 ]
163
182
164benchEncodeMap
183benchEncodeMap
@@ -166,7 +185,8 @@ benchEncodeMap
166 -> HM.HashMap T.Text T.Text
185 -> HM.HashMap T.Text T.Text
167 -> Benchmark
186 -> Benchmark
168benchEncodeMap name val = bgroup name
187benchEncodeMap name val = bgroup name
188+ [ bench "Text" $ nf B.encode val'
169− [ bench "Text" $ nf encode val'
189+ , bench "aeson-0.11" $ nf A.encode val'
170 ]
190 ]
171 where
191 where
172 val' :: M.Map T.Text T.Text
192 val' :: M.Map T.Text T.Text
benchmarks/aeson-benchmarks.cabal+2
@@ -180,6 +180,7 @@ executable aeson-benchmark-map
180 main-is: AesonMap.hs
180 main-is: AesonMap.hs
181 ghc-options: -Wall -O2 -rtsopts
181 ghc-options: -Wall -O2 -rtsopts
182 build-depends:
182 build-depends:
183+ aeson == 0.11.*,
183 aeson-benchmarks,
184 aeson-benchmarks,
184 base,
185 base,
185 criterion >= 1.0,
186 criterion >= 1.0,
@@ -195,6 +196,7 @@ executable aeson-benchmark-foldable
195 main-is: AesonFoldable.hs
196 main-is: AesonFoldable.hs
196 ghc-options: -Wall -O2 -rtsopts
197 ghc-options: -Wall -O2 -rtsopts
197 build-depends:
198 build-depends:
199+ aeson,
198 aeson-benchmarks,
200 aeson-benchmarks,
199 base,
201 base,
200 criterion >= 1.0,
202 criterion >= 1.0,
stack-bench.yamlnew file+8
@@ -0,0 +1,8 @@
1+resolver: lts-6.5
2+packages:
3+- benchmarks
4+extra-deps:
5+- semigroups-0.18.2
6+flags:
7+ semigroups:
8+ bytestring-builder: false