on 0.5.0.0 help sign in
typeable/aeson/benchmarks/CompareWithJSON.hs
70 lines 2.0 kB history raw
1{-# OPTIONS_GHC -fno-warn-orphans #-}
3import Blaze.ByteString.Builder (toLazyByteString)
4import Blaze.ByteString.Builder.Char.Utf8 (fromString)
5import Control.DeepSeq (NFData(rnf))
6import Criterion.Main
7import qualified Data.Aeson as A
8import qualified Data.ByteString.Lazy as BL
9import qualified Data.ByteString.Lazy.Internal as BL
10import qualified Text.JSON as J
12instance (NFData v) => NFData (J.JSObject v) where
13 rnf o = rnf (J.fromJSObject o)
15instance NFData J.JSValue where
16 rnf J.JSNull = ()
17 rnf (J.JSBool b) = rnf b
18 rnf (J.JSRational a b) = rnf a `seq` rnf b `seq` ()
19 rnf (J.JSString s) = rnf (J.fromJSString s)
20 rnf (J.JSArray lst) = rnf lst
21 rnf (J.JSObject o) = rnf o
23instance NFData BL.ByteString where
24 rnf (BL.Chunk _ bs) = rnf bs
25 rnf BL.Empty = ()
27decodeJ :: String -> J.JSValue
28decodeJ s =
29 case J.decodeStrict s of
30 J.Ok v -> v
31 J.Error _ -> error "fail to parse via JSON"
33decodeA :: BL.ByteString -> A.Value
34decodeA s = case A.decode s of
35 Just v -> v
36 Nothing -> error "fail to parse via Aeson"
38encodeJ :: J.JSValue -> BL.ByteString
39encodeJ = toLazyByteString . fromString . J.encode
41main :: IO ()
42main = do
43 let enFile = "json-data/twitter100.json"
44 jpFile = "json-data/jp100.json"
45 enA <- BL.readFile enFile
46 enJ <- readFile enFile
47 jpA <- BL.readFile jpFile
48 jpJ <- readFile jpFile
49 defaultMain [
50 bgroup "decode" [
51 bgroup "en" [
52 bench "aeson" $ nf decodeA enA
53 , bench "json" $ nf decodeJ enJ
54 ]
55 , bgroup "jp" [
56 bench "aeson" $ nf decodeA jpA
57 , bench "json" $ nf decodeJ jpJ
58 ]
59 ]
60 , bgroup "encode" [
61 bgroup "en" [
62 bench "aeson" $ nf A.encode (decodeA enA)
63 , bench "json" $ nf encodeJ (decodeJ enJ)
64 ]
65 , bgroup "jp" [
66 bench "aeson" $ nf A.encode (decodeA jpA)
67 , bench "json" $ nf encodeJ (decodeJ jpJ)
68 ]
69 ]
70 ]