on 0.7.0.2 help sign in
typeable/aeson/benchmarks/CompareWithJSON.hs
94 lines 2.8 kB history raw
1{-# LANGUAGE CPP #-}
2{-# OPTIONS_GHC -fno-warn-orphans #-}
4import Blaze.ByteString.Builder (toLazyByteString)
5import Blaze.ByteString.Builder.Char.Utf8 (fromString)
6import Control.DeepSeq (NFData(rnf))
7import Criterion.Main
8import qualified Data.Aeson.Encode as A
9import qualified Data.Aeson as A
10import qualified Data.ByteString.Lazy as BL
11import qualified Text.JSON as J
12import qualified Data.Text.Lazy as TL
13import qualified Data.Text.Lazy.Builder as TLB
14import qualified Data.Text.Lazy.Encoding as TLE
16#if !MIN_VERSION_bytestring(0,10,0)
17import qualified Data.ByteString.Lazy.Internal as BL
19instance NFData BL.ByteString where
20 rnf (BL.Chunk _ bs) = rnf bs
21 rnf BL.Empty = ()
22#endif
24instance (NFData v) => NFData (J.JSObject v) where
25 rnf o = rnf (J.fromJSObject o)
27instance NFData J.JSValue where
28 rnf J.JSNull = ()
29 rnf (J.JSBool b) = rnf b
30 rnf (J.JSRational a b) = rnf a `seq` rnf b `seq` ()
31 rnf (J.JSString s) = rnf (J.fromJSString s)
32 rnf (J.JSArray lst) = rnf lst
33 rnf (J.JSObject o) = rnf o
35decodeJ :: String -> J.JSValue
36decodeJ s =
37 case J.decodeStrict s of
38 J.Ok v -> v
39 J.Error _ -> error "fail to parse via JSON"
41decodeA :: BL.ByteString -> A.Value
42decodeA s = case A.decode s of
43 Just v -> v
44 Nothing -> error "fail to parse via Aeson"
46encodeJ :: J.JSValue -> BL.ByteString
47encodeJ = toLazyByteString . fromString . J.encode
49#if MIN_VERSION_bytestring(0,10,4)
50encodeToText :: A.Value -> TL.Text
51encodeToText = TLB.toLazyText . A.encodeToTextBuilder . A.toJSON
53encodeViaText :: A.Value -> BL.ByteString
54encodeViaText = TLE.encodeUtf8 . encodeToText
55#endif
57main :: IO ()
58main = do
59 let enFile = "json-data/twitter100.json"
60 jpFile = "json-data/jp100.json"
61 enA <- BL.readFile enFile
62 enJ <- readFile enFile
63 jpA <- BL.readFile jpFile
64 jpJ <- readFile jpFile
65 defaultMain [
66 bgroup "decode" [
67 bgroup "en" [
68 bench "aeson" $ nf decodeA enA
69 , bench "json" $ nf decodeJ enJ
70 ]
71 , bgroup "jp" [
72 bench "aeson" $ nf decodeA jpA
73 , bench "json" $ nf decodeJ jpJ
74 ]
75 ]
76 , bgroup "encode" [
77 bgroup "en" [
78 bench "aeson-to-bytestring" $ nf A.encode (decodeA enA)
79#if MIN_VERSION_bytestring(0,10,4)
80 , bench "aeson-via-text-to-bytestring" $ nf encodeViaText (decodeA enA)
81 , bench "aeson-to-text" $ nf encodeToText (decodeA enA)
82#endif
83 , bench "json" $ nf encodeJ (decodeJ enJ)
84 ]
85 , bgroup "jp" [
86 bench "aeson-to-bytestring" $ nf A.encode (decodeA jpA)
87#if MIN_VERSION_bytestring(0,10,4)
88 , bench "aeson-via-text-to-bytestring" $ nf encodeViaText (decodeA jpA)
89 , bench "aeson-to-text" $ nf encodeToText (decodeA jpA)
90#endif
91 , bench "json" $ nf encodeJ (decodeJ jpJ)
92 ]
93 ]
94 ]