on master help sign in
Flesh out the typed benchmarks a bit
1f031daBryan O'Sullivan
benchmarks/Twitter.hs+9
@@ -13,16 +13,21 @@ import Data.Text (Text)
13-- import Data.Time (ZonedTime)
13-- import Data.Time (ZonedTime)
14import GHC.Generics (Generic)
14import GHC.Generics (Generic)
15import Prelude hiding (id)
15import Prelude hiding (id)
16+import Control.DeepSeq
16
17
17data Metadata = Metadata {
18data Metadata = Metadata {
18 result_type :: Text
19 result_type :: Text
19 } deriving (Eq, Show, Typeable, Data, Generic)
20 } deriving (Eq, Show, Typeable, Data, Generic)
20
21
22+instance NFData Metadata
23+
21data Geo = Geo {
24data Geo = Geo {
22 type_ :: Text
25 type_ :: Text
23 , coordinates :: (Double, Double)
26 , coordinates :: (Double, Double)
24 } deriving (Eq, Show, Typeable, Data, Generic)
27 } deriving (Eq, Show, Typeable, Data, Generic)
25
28
29+instance NFData Geo
30+
26data Story = Story {
31data Story = Story {
27 from_user_id_str :: Text
32 from_user_id_str :: Text
28 , profile_image_url :: Text
33 , profile_image_url :: Text
@@ -40,6 +45,8 @@ data Story = Story {
40 , source :: Text
45 , source :: Text
41 } deriving (Show, Typeable, Data, Generic)
46 } deriving (Show, Typeable, Data, Generic)
42
47
48+instance NFData Story
49+
43data Result = Result {
50data Result = Result {
44 results :: [Story]
51 results :: [Story]
45 , max_id :: Int64
52 , max_id :: Int64
@@ -53,3 +60,5 @@ data Result = Result {
53 , max_id_str :: Text
60 , max_id_str :: Text
54 , query :: Text
61 , query :: Text
55 } deriving (Show, Typeable, Data, Generic)
62 } deriving (Show, Typeable, Data, Generic)
63+
64+instance NFData Result
benchmarks/Typed.hs+6−28
@@ -1,33 +1,11 @@
1module Main (main) where
1module Main (main) where
2
2
3−import qualified Data.ByteString.Lazy as L
4import Criterion.Main
3import Criterion.Main
5−import Data.Aeson
4+import qualified Typed.Generic as Generic
6−import Typed.Manual
5+import qualified Typed.Manual as 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)
19
6
20main :: IO ()
7main :: IO ()
21−main = do
22− twitter100 <- deeecode "json-data/twitter100.json"
23− jp100 <- deeecode "json-data/jp100.json"
24− defaultMain [
8+main = defaultMain [
25− bgroup "direct" [
9+ Generic.benchmarks
26− bench "twitter100" $ nf encodeDirect twitter100
10+ , Manual.benchmarks
27− , bench "jp100" $ nf encodeDirect jp100
28− ]
29− , bgroup "viaValue" [
30− bench "twitter100" $ nf encodeViaValue twitter100
31− , bench "jp100" $ nf encodeViaValue jp100
32− ]
33− ]
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
1+module Typed.Manual (benchmarks) where
2− (
3− encodeDirect
4− , encodeViaValue
5− ) where
6
2
7import Twitter.Manual
3import Twitter.Manual
4+import Typed.Common
8import Data.Aeson hiding (Result)
5import Data.Aeson hiding (Result)
9import Data.ByteString.Lazy as L
6import Data.ByteString.Lazy as L
10import Data.ByteString.Builder as B
7import Data.ByteString.Builder as B
8+import Criterion
9+import Control.Applicative
11
10
12encodeDirect :: Result -> L.ByteString
11encodeDirect :: Result -> L.ByteString
13encodeDirect = B.toLazyByteString . fromEncoding . toEncoding
12encodeDirect = B.toLazyByteString . fromEncoding . toEncoding
14
13
15encodeViaValue :: Result -> L.ByteString
14encodeViaValue :: Result -> L.ByteString
16encodeViaValue = encode
15encodeViaValue = 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
67 aeson-benchmarks,
67 aeson-benchmarks,
68 base,
68 base,
69 bytestring,
69 bytestring,
70− criterion,
70+ criterion >= 1.0,
71 deepseq,
71 deepseq,
72 text,
72 text,
73 time
73 time