on master help sign in
Flesh out the typed benchmarks a bit
commit1f031daccc03fffbb7191c03ffc28efef67a9edc authorBryan O'Sullivan <bos@serpentine.com> dateTue 26 May 2015, 21:00 (11 years ago) parent41da853
6 files changed+79−34
benchmarks/Twitter.hs+9 benchmarks/Typed.hs+6−28 benchmarks/Typed/Common.hs+16 benchmarks/Typed/Generic.hs+29 benchmarks/Typed/Manual.hs+18−5 benchmarks/aeson-benchmarks.cabal+1−1
benchmarks/Twitter.hs+9
@@ -13,16 +13,21 @@ import Data.Text (Text)
1313-- import Data.Time (ZonedTime)
1414import GHC.Generics (Generic)
1515import Prelude hiding (id)
16+import Control.DeepSeq
1617
1718data Metadata = Metadata {
1819 result_type :: Text
1920 } deriving (Eq, Show, Typeable, Data, Generic)
2021
22+instance NFData Metadata
23+
2124data Geo = Geo {
2225 type_ :: Text
2326 , coordinates :: (Double, Double)
2427 } deriving (Eq, Show, Typeable, Data, Generic)
2528
29+instance NFData Geo
30+
2631data Story = Story {
2732 from_user_id_str :: Text
2833 , profile_image_url :: Text
@@ -40,6 +45,8 @@ data Story = Story {
4045 , source :: Text
4146 } deriving (Show, Typeable, Data, Generic)
4247
48+instance NFData Story
49+
4350data Result = Result {
4451 results :: [Story]
4552 , max_id :: Int64
@@ -53,3 +60,5 @@ data Result = Result {
5360 , max_id_str :: Text
5461 , query :: Text
5562 } deriving (Show, Typeable, Data, Generic)
63+
64+instance NFData Result
benchmarks/Typed.hs+6−28
@@ -1,33 +1,11 @@
11module Main (main) where
22
3−import qualified Data.ByteString.Lazy as L
43import Criterion.Main
5−import Data.Aeson
6−import Typed.Manual
7−import Control.Applicative ((<$>))
8−import System.Exit
9−import System.IO
10−
11−deeecode :: FromJSON a => FilePath -> IO a
12−deeecode fileName = do
13− mv <- eitherDecode' <$> L.readFile fileName
14− case mv of
15− Right v -> return v
16− Left err -> do
17− hPutStrLn stderr $ fileName ++ ": JSON decode failed - " ++ err
18− exitWith (ExitFailure 1)
4+import qualified Typed.Generic as Generic
5+import qualified Typed.Manual as Manual
196
207main :: IO ()
21−main = do
22− twitter100 <- deeecode "json-data/twitter100.json"
23− jp100 <- deeecode "json-data/jp100.json"
24− defaultMain [
25− bgroup "direct" [
26− bench "twitter100" $ nf encodeDirect twitter100
27− , bench "jp100" $ nf encodeDirect jp100
28− ]
29− , bgroup "viaValue" [
30− bench "twitter100" $ nf encodeViaValue twitter100
31− , bench "jp100" $ nf encodeViaValue jp100
32− ]
33− ]
8+main = defaultMain [
9+ Generic.benchmarks
10+ , Manual.benchmarks
11+ ]
benchmarks/Typed/Common.hsnew file+16
@@ -0,0 +1,16 @@
1+module Typed.Common (load) where
2+
3+import Data.Aeson hiding (Result)
4+import Data.ByteString.Lazy as L
5+import Control.Applicative
6+import System.IO
7+import System.Exit
8+
9+load :: FromJSON a => FilePath -> IO a
10+load fileName = do
11+ mv <- eitherDecode' <$> L.readFile fileName
12+ case mv of
13+ Right v -> return v
14+ Left err -> do
15+ hPutStrLn stderr $ fileName ++ ": JSON decode failed - " ++ err
16+ exitWith (ExitFailure 1)
benchmarks/Typed/Generic.hsnew file+29
@@ -0,0 +1,29 @@
1+module Typed.Generic (benchmarks) where
2+
3+import Typed.Common
4+import Twitter.Generic
5+import Data.Aeson hiding (Result)
6+import Data.ByteString.Lazy as L
7+import Data.ByteString.Builder as B
8+import Criterion
9+import Control.Applicative
10+
11+encodeDirect :: Result -> L.ByteString
12+encodeDirect = B.toLazyByteString . fromEncoding . toEncoding
13+
14+encodeViaValue :: Result -> L.ByteString
15+encodeViaValue = encode
16+
17+benchmarks :: Benchmark
18+benchmarks =
19+ env ((,) <$> load "json-data/twitter100.json" <*> load "json-data/jp100.json") $ \ ~(twitter100, jp100) ->
20+ bgroup "generic" [
21+ bgroup "direct" [
22+ bench "twitter100" $ nf encodeDirect twitter100
23+ , bench "jp100" $ nf encodeDirect jp100
24+ ]
25+ , bgroup "viaValue" [
26+ bench "twitter100" $ nf encodeViaValue twitter100
27+ , bench "jp100" $ nf encodeViaValue jp100
28+ ]
29+ ]
benchmarks/Typed/Manual.hs+18−5
@@ -1,16 +1,29 @@
1−module Typed.Manual
2− (
3− encodeDirect
4− , encodeViaValue
5− ) where
1+module Typed.Manual (benchmarks) where
62
73import Twitter.Manual
4+import Typed.Common
85import Data.Aeson hiding (Result)
96import Data.ByteString.Lazy as L
107import Data.ByteString.Builder as B
8+import Criterion
9+import Control.Applicative
1110
1211encodeDirect :: Result -> L.ByteString
1312encodeDirect = B.toLazyByteString . fromEncoding . toEncoding
1413
1514encodeViaValue :: Result -> L.ByteString
1615encodeViaValue = encode
16+
17+benchmarks :: Benchmark
18+benchmarks =
19+ env ((,) <$> load "json-data/twitter100.json" <*> load "json-data/jp100.json") $ \ ~(twitter100, jp100) ->
20+ bgroup "manual" [
21+ bgroup "direct" [
22+ bench "twitter100" $ nf encodeDirect twitter100
23+ , bench "jp100" $ nf encodeDirect jp100
24+ ]
25+ , bgroup "viaValue" [
26+ bench "twitter100" $ nf encodeViaValue twitter100
27+ , bench "jp100" $ nf encodeViaValue jp100
28+ ]
29+ ]
benchmarks/aeson-benchmarks.cabal+1−1
@@ -67,7 +67,7 @@ executable aeson-benchmark-typed
6767 aeson-benchmarks,
6868 base,
6969 bytestring,
70− criterion,
70+ criterion >= 1.0,
7171 deepseq,
7272 text,
7373 time