Skip to content

Commit

Permalink
Add benchmark where we parse Aeson.Value with hermes
Browse files Browse the repository at this point in the history
  • Loading branch information
phadej committed Feb 11, 2023
1 parent 535dc55 commit 06871c8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
3 changes: 2 additions & 1 deletion benchmarks/aeson-benchmarks.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,11 @@ executable aeson-benchmark-suite
other-modules: Compare.JsonBuilder
build-depends: json-builder

if impl(ghc >=8.10)
if impl(ghc >=8.8)
build-depends: hermes-json >=0.2.0.1
other-modules:
CompareWithHermes
Data.Hermes.Aeson
Twitter.Hermes

if !flag(text2)
Expand Down
11 changes: 11 additions & 0 deletions benchmarks/bench/CompareWithHermes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import qualified Data.Aeson.Parser.Internal as I
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
import qualified Data.Hermes as H
import qualified Data.Hermes.Aeson as H.A
import qualified Twitter as T
import Twitter.Manual () -- fair comparison with manual Hermes decoders
import Twitter.Hermes
Expand Down Expand Up @@ -40,6 +41,14 @@ decodeH s = case H.decodeEither twitterResultDecoder s of
Right result -> result
Left err -> error (show err)

-- decode to A.Value, and then use A.FromJSON instance.
decodeHA :: BS.ByteString -> T.Result
decodeHA s = case H.decodeEither H.A.valueDecoder s of
Left err -> error (show err)
Right v -> case A.fromJSON v of
A.Success x -> x
A.Error err -> error (show err)

benchmark :: Benchmark
benchmark =
env (readL enFile) $ \enA ->
Expand All @@ -55,12 +64,14 @@ benchmark =
, bench "aeson/parser" $ nf decodeIP enA
, bench "aeson/tokens/strict" $ nf decodeTS enS
, bench "hermes" $ nf decodeH enS
, bench "hermes/aeson" $ nf decodeHA enS
]
, bgroup "jp" [
bench "aeson" $ nf decode jpA
, bench "aeson/stricter" $ nf decodeS jpS
, bench "aeson/tokens/strict" $ nf decodeTS jpS
, bench "hermes" $ nf decodeH jpS
, bench "hermes/aeson" $ nf decodeHA jpS
]
]
]
Expand Down
18 changes: 18 additions & 0 deletions benchmarks/bench/Data/Hermes/Aeson.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Data.Hermes.Aeson where

import Control.Applicative ((<|>))

import qualified Data.Hermes as H
import qualified Data.Aeson as A
import qualified Data.Vector as V
import qualified Data.Aeson.Key as K
import qualified Data.Aeson.KeyMap as KM

valueDecoder :: H.Value -> H.Decoder A.Value
valueDecoder v =
A.String <$> H.text v <|>
A.Number <$> H.scientific v <|>
A.Bool <$> H.bool v <|>
A.Object . KM.fromList <$> H.objectAsKeyValues (pure . K.fromText) valueDecoder v <|>
A.Array . V.fromList <$> H.list valueDecoder v <|>
A.Null <$ H.nullable (\_ -> fail "notnull") v
10 changes: 2 additions & 8 deletions benchmarks/bench/aeson-benchmark-suite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ import qualified AesonMap
import qualified AutoCompare
import qualified Compare
import qualified CompareWithJSON
<<<<<<< HEAD:benchmarks/bench/aeson-benchmark-suite.hs
=======
#ifdef MIN_VERSION_hermes_json
import qualified CompareWithHermes
#endif
>>>>>>> Use hermes only with GHC > 8.8:benchmarks/bench/Suite.hs
import qualified Dates
import qualified GitHub
import qualified Issue673
Expand All @@ -48,7 +42,7 @@ import Utils
import qualified UnescapePureText1 as Text1
#endif

#if __GLASGOW_HASKELL__ >=810
#if __GLASGOW_HASKELL__ >=808
import qualified CompareWithHermes
#endif

Expand Down Expand Up @@ -146,6 +140,6 @@ main = do
]
++ Compare.benchmarks -- compares to different libs (encoding)
++ [ CompareWithJSON.benchmark ]
#if __GLASGOW_HASKELL__ >=810
#if __GLASGOW_HASKELL__ >=808
++ [ CompareWithHermes.benchmark ]
#endif

0 comments on commit 06871c8

Please sign in to comment.