Skip to content

Commit 8a0f0b8

Browse files
authored
Merge pull request #835 from phadej/auto-compare-typed-bench
Add AutoCompare and Typed benchmarks to suite
2 parents 9e6a81d + f573b25 commit 8a0f0b8

File tree

11 files changed

+105
-144
lines changed

11 files changed

+105
-144
lines changed

benchmarks/aeson-benchmarks.cabal

Lines changed: 16 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -113,19 +113,6 @@ executable aeson-benchmark-suite
113113
, time
114114
, vector
115115

116-
other-modules:
117-
GitHub
118-
Twitter
119-
Twitter.Manual
120-
Issue673
121-
122-
-- Benchmarks below are haven't been worked out yet
123-
124-
executable aeson-benchmark-auto-compare
125-
default-language: Haskell2010
126-
main-is: AutoCompare.hs
127-
hs-source-dirs: bench
128-
ghc-options: -Wall -O2 -rtsopts
129116
other-modules:
130117
Auto.G.BigProduct
131118
Auto.G.BigRecord
@@ -135,14 +122,22 @@ executable aeson-benchmark-auto-compare
135122
Auto.T.BigRecord
136123
Auto.T.BigSum
137124
Auto.T.D
125+
AutoCompare
126+
GitHub
127+
Issue673
138128
Options
129+
Twitter
130+
Twitter.Generic
131+
Twitter.Manual
132+
Twitter.Options
133+
Twitter.TH
134+
Typed
135+
Typed.Generic
136+
Typed.Manual
137+
Typed.TH
138+
Utils
139139

140-
build-depends:
141-
aeson-benchmarks
142-
, base
143-
, criterion
144-
, deepseq
145-
, template-haskell
140+
-- Benchmarks below are haven't been worked out yet
146141

147142
executable aeson-benchmark-compare
148143
default-language: Haskell2010
@@ -154,7 +149,7 @@ executable aeson-benchmark-compare
154149
Compare.JsonBench
155150
Twitter
156151
Twitter.Manual
157-
Typed.Common
152+
Utils
158153

159154
build-depends:
160155
aeson-benchmarks
@@ -164,6 +159,7 @@ executable aeson-benchmark-compare
164159
, bytestring
165160
, criterion >=1.0
166161
, deepseq
162+
, filepath
167163
, ghc-prim
168164
, text
169165

@@ -191,36 +187,6 @@ executable aeson-benchmark-micro
191187
, ghc-prim
192188
, text
193189

194-
executable aeson-benchmark-typed
195-
default-language: Haskell2010
196-
main-is: Typed.hs
197-
hs-source-dirs: bench examples/src
198-
ghc-options: -Wall -O2 -rtsopts
199-
other-modules:
200-
Twitter
201-
Twitter.Generic
202-
Twitter.Manual
203-
Twitter.Options
204-
Twitter.TH
205-
Typed.Common
206-
Typed.Generic
207-
Typed.Manual
208-
Typed.TH
209-
210-
build-depends:
211-
aeson-benchmarks
212-
, base
213-
, base-compat-batteries
214-
, bytestring
215-
, criterion >=1.0
216-
, deepseq
217-
, ghc-prim
218-
, text
219-
, time
220-
221-
if impl(ghc <8.0)
222-
build-depends: semigroups
223-
224190
executable aeson-benchmark-compare-with-json
225191
default-language: Haskell2010
226192
main-is: CompareWithJSON.hs

benchmarks/bench/AutoCompare.hs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{-# LANGUAGE RankNTypes #-}
22
{-# LANGUAGE ScopedTypeVariables #-}
33

4-
module Main (main) where
4+
module AutoCompare (benchmark, sanityCheck) where
55

66
import Control.DeepSeq
77
import Control.Monad
@@ -19,8 +19,8 @@ import qualified Auto.G.BigSum as G
1919

2020
--------------------------------------------------------------------------------
2121

22-
runBench :: IO ()
23-
runBench = defaultMain
22+
benchmark :: Benchmark
23+
benchmark = bgroup "AutoBench"
2424
[ compareBench "D" T.d G.d
2525
, compareBench "BigRecord" T.bigRecord G.bigRecord
2626
, compareBench "BigProduct" T.bigProduct G.bigProduct
@@ -66,8 +66,3 @@ check x = do
6666
decode_ s = case decode s of
6767
Just v -> fromJSON v
6868
Nothing -> fail ""
69-
70-
main :: IO ()
71-
main = do
72-
sanityCheck
73-
runBench

benchmarks/bench/Compare.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Criterion.Main
1111
import Data.BufferBuilder.Json
1212
import Twitter
1313
import Twitter.Manual ()
14-
import Typed.Common
14+
import Utils (readV)
1515
import qualified Data.Aeson as Aeson
1616
import qualified Compare.JsonBench as JsonBench
1717

@@ -23,8 +23,8 @@ import Compare.JsonBuilder ()
2323
main :: IO ()
2424
main =
2525
defaultMain [
26-
env (load "json-data/twitter100.json") $ \ ~(twtr :: Result) ->
27-
bgroup "twitter" [
26+
env (readV "json-data/twitter100.json") $ \ ~(twtr :: Result) ->
27+
bgroup "CompareEncodeTwitter" [
2828
bench "aeson" $ nf Aeson.encode twtr
2929
, bench "buffer-builder" $ nf encodeJson twtr
3030
#ifdef MIN_VERSION_json_builder

benchmarks/bench/Compare/JsonBench.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import Criterion
1919
import Data.Aeson ((.:))
2020
import Data.Monoid ((<>))
2121
import Data.Text (Text)
22-
import Typed.Common (load)
22+
import Utils (readV)
2323
import qualified Control.Monad.Fail as Fail
2424
import qualified Data.Aeson as Aeson
2525
import qualified Data.BufferBuilder.Json as Json
@@ -328,9 +328,9 @@ instance JB.Value User where
328328
#endif
329329

330330
benchmarks :: Benchmark
331-
benchmarks = env (load "json-data/buffer-builder.json") $
331+
benchmarks = env (readV "buffer-builder.json") $
332332
\ ~(parsedUserList :: [User]) ->
333-
bgroup "json-bench" [
333+
bgroup "CompareEncodeUserList" [
334334
bench "aeson" $ nf Aeson.encode parsedUserList
335335
, bench "buffer-builder" $ nf Json.encodeJson parsedUserList
336336
#ifdef MIN_VERSION_json_builder

benchmarks/bench/Suite.hs

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ import Prelude.Compat
1010
import Control.DeepSeq (NFData)
1111
import Criterion.Main (Benchmark, bench, bgroup,
1212
defaultMain, env, nf, whnf)
13-
import Data.Maybe (fromMaybe)
1413
import Data.Proxy (Proxy (..))
1514
import Data.Vector (Vector)
16-
import System.Environment (lookupEnv)
17-
import System.FilePath ((</>))
1815

1916
import qualified Data.Aeson.Encoding.Builder as Aeson.EB
2017
import qualified Data.Aeson.Parser.UnescapeFFI as FFI
@@ -25,10 +22,12 @@ import qualified Data.ByteString.Char8 as BS8
2522
import qualified Data.ByteString.Lazy as LBS
2623
import qualified Data.Text as T
2724

25+
import qualified AutoCompare
2826
import qualified GitHub
2927
import qualified Issue673
30-
import qualified Twitter
31-
import qualified Twitter.Manual ()
28+
import qualified Typed
29+
30+
import Utils
3231

3332
-------------------------------------------------------------------------------
3433
-- Decode bench
@@ -76,33 +75,21 @@ escapeBench = bgroup "Escape"
7675
, bench "pure" $ whnf Pure.unescapeText input
7776
]
7877

79-
-------------------------------------------------------------------------------
80-
-- Helpers
81-
-------------------------------------------------------------------------------
82-
83-
readS :: FilePath -> IO BS.ByteString
84-
readS fp = do
85-
dataDir <- lookupEnv "AESON_BENCH_DATADIR"
86-
BS.readFile $ fromMaybe "json-data" dataDir </> fp
87-
88-
readL :: FilePath -> IO LBS.ByteString
89-
readL fp = do
90-
dataDir <- lookupEnv "AESON_BENCH_DATADIR"
91-
LBS.readFile $ fromMaybe "json-data" dataDir </> fp
92-
9378
-------------------------------------------------------------------------------
9479
-- Main
9580
-------------------------------------------------------------------------------
9681

9782
main :: IO ()
98-
main = defaultMain
99-
[ bgroup "Examples"
100-
[ bgroup "decode"
101-
[ decodeBench "twitter100" "twitter100.json" (Proxy :: Proxy Twitter.Result)
102-
, decodeBench "jp100" "jp100.json" (Proxy :: Proxy Twitter.Result)
103-
, decodeBench "github-issues" "github-issues.json" (Proxy :: Proxy (Vector GitHub.Issue))
83+
main = do
84+
AutoCompare.sanityCheck
85+
defaultMain
86+
[ bgroup "Examples"
87+
[ bgroup "decode"
88+
[ decodeBench "github-issues" "github-issues.json" (Proxy :: Proxy (Vector GitHub.Issue))
89+
]
10490
]
91+
, escapeBench
92+
, Issue673.benchmark
93+
, Typed.benchmark -- Twitter
94+
, AutoCompare.benchmark -- compares Generic and TH
10595
]
106-
, escapeBench
107-
, Issue673.benchmark
108-
]

benchmarks/bench/Typed.hs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
{-# LANGUAGE NoImplicitPrelude #-}
22

3-
module Main (main) where
3+
module Typed (benchmark) where
44

5-
import Prelude.Compat
5+
import Criterion.Main
6+
import qualified Typed.Generic as Generic
7+
import qualified Typed.Manual as Manual
8+
import qualified Typed.TH as TH
69

7-
import Criterion.Main
8-
import qualified Typed.Generic as Generic
9-
import qualified Typed.Manual as Manual
10-
import qualified Typed.TH as TH
11-
12-
main :: IO ()
13-
main = defaultMain [
14-
Generic.benchmarks
15-
, Manual.benchmarks
16-
, TH.benchmarks
17-
, Generic.decodeBenchmarks
18-
, Manual.decodeBenchmarks
19-
, TH.decodeBenchmarks
10+
benchmark :: Benchmark
11+
benchmark = bgroup "Twitter"
12+
[ bgroup "encode"
13+
[ Generic.benchmarks
14+
, Manual.benchmarks
15+
, TH.benchmarks
16+
]
17+
, bgroup "decode"
18+
[ Generic.decodeBenchmarks
19+
, Manual.decodeBenchmarks
20+
, TH.decodeBenchmarks
21+
]
2022
]

benchmarks/bench/Typed/Common.hs

Lines changed: 0 additions & 22 deletions
This file was deleted.

benchmarks/bench/Typed/Generic.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Data.Aeson hiding (Result)
88
import Criterion
99
import Data.ByteString.Lazy as L
1010
import Twitter.Generic
11-
import Typed.Common
11+
import Utils
1212

1313
encodeDirect :: Result -> L.ByteString
1414
encodeDirect = encode
@@ -18,8 +18,8 @@ encodeViaValue = encode . toJSON
1818

1919
benchmarks :: Benchmark
2020
benchmarks =
21-
env ((,) <$> load "json-data/twitter100.json" <*> load "json-data/jp100.json") $ \ ~(twitter100, jp100) ->
22-
bgroup "encodeGeneric" [
21+
env ((,) <$> readV "twitter100.json" <*> readV "jp100.json") $ \ ~(twitter100, jp100) ->
22+
bgroup "Generic" [
2323
bgroup "direct" [
2424
bench "twitter100" $ nf encodeDirect twitter100
2525
, bench "jp100" $ nf encodeDirect jp100
@@ -35,8 +35,8 @@ decodeDirect = decode
3535

3636
decodeBenchmarks :: Benchmark
3737
decodeBenchmarks =
38-
env ((,) <$> L.readFile "json-data/twitter100.json" <*> L.readFile "json-data/jp100.json") $ \ ~(twitter100, jp100) ->
39-
bgroup "decodeGeneric"
38+
env ((,) <$> readL "twitter100.json" <*> readL "jp100.json") $ \ ~(twitter100, jp100) ->
39+
bgroup "Generic"
4040
[ bgroup "direct"
4141
[ bench "twitter100" $ nf decodeDirect twitter100
4242
, bench "jp100" $ nf decodeDirect jp100

benchmarks/bench/Typed/Manual.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Data.Aeson hiding (Result)
88
import Criterion
99
import Data.ByteString.Lazy as L
1010
import Twitter.Manual
11-
import Typed.Common
11+
import Utils
1212

1313
encodeDirect :: Result -> L.ByteString
1414
encodeDirect = encode
@@ -18,8 +18,8 @@ encodeViaValue = encode . toJSON
1818

1919
benchmarks :: Benchmark
2020
benchmarks =
21-
env ((,) <$> load "json-data/twitter100.json" <*> load "json-data/jp100.json") $ \ ~(twitter100, jp100) ->
22-
bgroup "encodeManual" [
21+
env ((,) <$> readV "twitter100.json" <*> readV "jp100.json") $ \ ~(twitter100, jp100) ->
22+
bgroup "Manual" [
2323
bgroup "direct" [
2424
bench "twitter100" $ nf encodeDirect twitter100
2525
, bench "jp100" $ nf encodeDirect jp100
@@ -35,8 +35,8 @@ decodeDirect = decode
3535

3636
decodeBenchmarks :: Benchmark
3737
decodeBenchmarks =
38-
env ((,) <$> L.readFile "json-data/twitter100.json" <*> L.readFile "json-data/jp100.json") $ \ ~(twitter100, jp100) ->
39-
bgroup "decodeManual"
38+
env ((,) <$> readL "twitter100.json" <*> readL "jp100.json") $ \ ~(twitter100, jp100) ->
39+
bgroup "Manual"
4040
[ bgroup "direct"
4141
[ bench "twitter100" $ nf decodeDirect twitter100
4242
, bench "jp100" $ nf decodeDirect jp100

benchmarks/bench/Typed/TH.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Data.Aeson hiding (Result)
88
import Criterion
99
import Data.ByteString.Lazy as L
1010
import Twitter.TH
11-
import Typed.Common
11+
import Utils
1212

1313
encodeDirect :: Result -> L.ByteString
1414
encodeDirect = encode
@@ -18,8 +18,8 @@ encodeViaValue = encode . toJSON
1818

1919
benchmarks :: Benchmark
2020
benchmarks =
21-
env ((,) <$> load "json-data/twitter100.json" <*> load "json-data/jp100.json") $ \ ~(twitter100, jp100) ->
22-
bgroup "encodeTH" [
21+
env ((,) <$> readV "twitter100.json" <*> readV "jp100.json") $ \ ~(twitter100, jp100) ->
22+
bgroup "TH" [
2323
bgroup "direct" [
2424
bench "twitter100" $ nf encodeDirect twitter100
2525
, bench "jp100" $ nf encodeDirect jp100
@@ -35,8 +35,8 @@ decodeDirect = decode
3535

3636
decodeBenchmarks :: Benchmark
3737
decodeBenchmarks =
38-
env ((,) <$> L.readFile "json-data/twitter100.json" <*> L.readFile "json-data/jp100.json") $ \ ~(twitter100, jp100) ->
39-
bgroup "decodeTH"
38+
env ((,) <$> readL "twitter100.json" <*> readL "jp100.json") $ \ ~(twitter100, jp100) ->
39+
bgroup "TH"
4040
[ bgroup "direct"
4141
[ bench "twitter100" $ nf decodeDirect twitter100
4242
, bench "jp100" $ nf decodeDirect jp100

0 commit comments

Comments
 (0)