Skip to content

Commit

Permalink
Make aeson-benchmark-typed compare to old aeson
Browse files Browse the repository at this point in the history
  • Loading branch information
phadej committed Jul 15, 2016
1 parent 5459273 commit 0849c2d
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 34 deletions.
3 changes: 2 additions & 1 deletion benchmarks/AesonParse.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{-# LANGUAGE BangPatterns, OverloadedStrings #-}
{-# LANGUAGE PackageImports #-}

import Control.Exception
import Control.Monad
import Data.Aeson
import "aeson-benchmarks" Data.Aeson
import Data.Attoparsec.ByteString (IResult(..), parseWith)
import Data.Time.Clock
import System.Environment (getArgs)
Expand Down
3 changes: 2 additions & 1 deletion benchmarks/Compare.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE PackageImports #-}

module Main (main) where

Expand All @@ -11,7 +12,7 @@ import Twitter
import Twitter.Manual ()
import Typed.Common
import qualified Compare.JsonBench as JsonBench
import qualified Data.Aeson as Aeson
import qualified "aeson-benchmarks" Data.Aeson as Aeson

main :: IO ()
main =
Expand Down
7 changes: 7 additions & 0 deletions benchmarks/Typed/Common.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
{-# LANGUAGE CPP, PackageImports #-}
module Typed.Common (load) where

#ifndef HAS_BOTH_AESON_AND_BENCHMARKS
import Data.Aeson hiding (Result)
#else
import "aeson" Data.Aeson hiding (Result)
import qualified "aeson-benchmarks" Data.Aeson as B
#endif

import Data.ByteString.Lazy as L
import Control.Applicative
import System.IO
Expand Down
32 changes: 22 additions & 10 deletions benchmarks/Typed/Generic.hs
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
{-# LANGUAGE PackageImports #-}
module Typed.Generic (benchmarks) where

import "aeson" Data.Aeson hiding (Result)
import qualified "aeson-benchmarks" Data.Aeson as B

import Control.Applicative
import Criterion
import Data.Aeson hiding (Result)
import Data.ByteString.Builder as B
import Data.ByteString.Lazy as L
import Twitter.TH
import Typed.Common

encodeDirect :: Result -> L.ByteString
encodeDirect = encode
encodeDirectA :: Result -> L.ByteString
encodeDirectA = encode

encodeViaValueA :: Result -> L.ByteString
encodeViaValueA = encode . toJSON

encodeDirectB :: Result -> L.ByteString
encodeDirectB = B.encode

encodeViaValue :: Result -> L.ByteString
encodeViaValue = encode . toJSON
encodeViaValueB :: Result -> L.ByteString
encodeViaValueB = B.encode . B.toJSON

benchmarks :: Benchmark
benchmarks =
env ((,) <$> load "json-data/twitter100.json" <*> load "json-data/jp100.json") $ \ ~(twitter100, jp100) ->
bgroup "generic" [
bgroup "direct" [
bench "twitter100" $ nf encodeDirect twitter100
, bench "jp100" $ nf encodeDirect jp100
bench "twitter100" $ nf encodeDirectB twitter100
, bench "jp100" $ nf encodeDirectB jp100
, bench "twitter100 baseline" $ nf encodeDirectA twitter100
, bench "jp100 baseline" $ nf encodeDirectA jp100
]
, bgroup "viaValue" [
bench "twitter100" $ nf encodeViaValue twitter100
, bench "jp100" $ nf encodeViaValue jp100
bench "twitter100" $ nf encodeViaValueB twitter100
, bench "jp100" $ nf encodeViaValueB jp100
, bench "twitter100 baseline" $ nf encodeViaValueA twitter100
, bench "jp100 baseline" $ nf encodeViaValueA jp100
]
]
32 changes: 22 additions & 10 deletions benchmarks/Typed/Manual.hs
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
{-# LANGUAGE PackageImports #-}
module Typed.Manual (benchmarks) where

import "aeson" Data.Aeson hiding (Result)
import qualified "aeson-benchmarks" Data.Aeson as B

import Control.Applicative
import Criterion
import Data.Aeson hiding (Result)
import Data.ByteString.Builder as B
import Data.ByteString.Lazy as L
import Twitter.Manual
import Typed.Common

encodeDirect :: Result -> L.ByteString
encodeDirect = encode
encodeDirectA :: Result -> L.ByteString
encodeDirectA = encode

encodeViaValueA :: Result -> L.ByteString
encodeViaValueA = encode . toJSON

encodeDirectB :: Result -> L.ByteString
encodeDirectB = B.encode

encodeViaValue :: Result -> L.ByteString
encodeViaValue = encode . toJSON
encodeViaValueB :: Result -> L.ByteString
encodeViaValueB = B.encode . B.toJSON

benchmarks :: Benchmark
benchmarks =
env ((,) <$> load "json-data/twitter100.json" <*> load "json-data/jp100.json") $ \ ~(twitter100, jp100) ->
bgroup "manual" [
bgroup "direct" [
bench "twitter100" $ nf encodeDirect twitter100
, bench "jp100" $ nf encodeDirect jp100
bench "twitter100" $ nf encodeDirectB twitter100
, bench "jp100" $ nf encodeDirectB jp100
, bench "twitter100 baseline" $ nf encodeDirectA twitter100
, bench "jp100 baseline" $ nf encodeDirectA jp100
]
, bgroup "viaValue" [
bench "twitter100" $ nf encodeViaValue twitter100
, bench "jp100" $ nf encodeViaValue jp100
bench "twitter100" $ nf encodeViaValueB twitter100
, bench "jp100" $ nf encodeViaValueB jp100
, bench "twitter100 baseline" $ nf encodeViaValueA twitter100
, bench "jp100 baseline" $ nf encodeViaValueA jp100
]
]
31 changes: 22 additions & 9 deletions benchmarks/Typed/TH.hs
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
{-# LANGUAGE PackageImports #-}
module Typed.TH (benchmarks) where

import "aeson" Data.Aeson hiding (Result)
import qualified "aeson-benchmarks" Data.Aeson as B

import Control.Applicative
import Criterion
import Data.Aeson hiding (Result)
import Data.ByteString.Builder as B
import Data.ByteString.Lazy as L
import Twitter.TH
import Typed.Common

encodeDirect :: Result -> L.ByteString
encodeDirect = encode
encodeDirectA :: Result -> L.ByteString
encodeDirectA = encode

encodeViaValueA :: Result -> L.ByteString
encodeViaValueA = encode . toJSON

encodeDirectB :: Result -> L.ByteString
encodeDirectB = B.encode

encodeViaValue :: Result -> L.ByteString
encodeViaValue = encode . toJSON
encodeViaValueB :: Result -> L.ByteString
encodeViaValueB = B.encode . B.toJSON

benchmarks :: Benchmark
benchmarks =
env ((,) <$> load "json-data/twitter100.json" <*> load "json-data/jp100.json") $ \ ~(twitter100, jp100) ->
bgroup "th" [
bgroup "direct" [
bench "twitter100" $ nf encodeDirect twitter100
, bench "jp100" $ nf encodeDirect jp100
bench "twitter100" $ nf encodeDirectB twitter100
, bench "jp100" $ nf encodeDirectB jp100
, bench "twitter100 baseline" $ nf encodeDirectA twitter100
, bench "jp100 baseline" $ nf encodeDirectA jp100
]
, bgroup "viaValue" [
bench "twitter100" $ nf encodeViaValue twitter100
, bench "jp100" $ nf encodeViaValue jp100
bench "twitter100" $ nf encodeViaValueA twitter100
, bench "jp100" $ nf encodeViaValueA jp100
, bench "twitter100 baseline" $ nf encodeViaValueB twitter100
, bench "jp100 baseline" $ nf encodeViaValueB jp100
]
]
4 changes: 4 additions & 0 deletions benchmarks/aeson-benchmarks.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ executable aeson-benchmark-typed
main-is: Typed.hs
hs-source-dirs: ../examples .
ghc-options: -Wall -O2 -rtsopts
-- We must help ourself in situations when there is both
-- aeson and aeson-benchmakrs
cpp-options: -DHAS_BOTH_AESON_AND_BENCHMARKS
build-depends:
aeson,
aeson-benchmarks,
base,
criterion >= 1.0,
Expand Down
23 changes: 22 additions & 1 deletion examples/Twitter/Generic.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-- Use GHC generics to automatically generate good instances.

{-# LANGUAGE CPP, PackageImports #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

module Twitter.Generic
Expand All @@ -10,9 +11,15 @@ module Twitter.Generic
, Result(..)
) where

import Data.Aeson (ToJSON, FromJSON)
import Twitter

#ifndef HAS_BOTH_AESON_AND_BENCHMARKS
import Data.Aeson (ToJSON, FromJSON)
#else
import "aeson" Data.Aeson (ToJSON, FromJSON)
import qualified "aeson-benchmarks" Data.Aeson as B
#endif

instance ToJSON Metadata
instance FromJSON Metadata

Expand All @@ -24,3 +31,17 @@ instance FromJSON Story

instance ToJSON Result
instance FromJSON Result

#ifdef HAS_BOTH_AESON_AND_BENCHMARKS
instance B.ToJSON Metadata
instance B.FromJSON Metadata

instance B.ToJSON Geo
instance B.FromJSON Geo

instance B.ToJSON Story
instance B.FromJSON Story

instance B.ToJSON Result
instance B.FromJSON Result
#endif
Loading

0 comments on commit 0849c2d

Please sign in to comment.