on wip/yuras/scanner help sign in
typeable/aeson/benchmarks/CompareWithJSON.hs
102 lines 3.3 kB history raw
1{-# LANGUAGE NoImplicitPrelude #-}
3{-# OPTIONS_GHC -fno-warn-orphans #-}
5module Main (main) where
7import Prelude.Compat
9import Blaze.ByteString.Builder (toLazyByteString)
10import Blaze.ByteString.Builder.Char.Utf8 (fromString)
11import Control.DeepSeq (NFData(rnf))
12import Criterion.Main
13import Data.Maybe (fromMaybe)
14import qualified Data.Aeson as A
15import qualified Data.Aeson.Text as A
16import qualified Data.Aeson.Parser.Internal as I
17import qualified Data.ByteString as BS
18import qualified Data.ByteString.Lazy as BL
19import qualified Data.Text.Lazy as TL
20import qualified Data.Text.Lazy.Builder as TLB
21import qualified Data.Text.Lazy.Encoding as TLE
22import qualified Text.JSON as J
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
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"
41decode :: BL.ByteString -> A.Value
42decode s = fromMaybe (error "fail to parse via Aeson") $ A.decode s
44decode' :: BL.ByteString -> A.Value
45decode' s = fromMaybe (error "fail to parse via Aeson") $ A.decode' s
47decodeS :: BS.ByteString -> A.Value
48decodeS s = fromMaybe (error "fail to parse via Aeson") $ A.decodeStrict' s
50decodeIP :: BL.ByteString -> A.Value
51decodeIP s = fromMaybe (error "fail to parse via Parser.decodeWith") $
52 I.decodeWith I.jsonEOF A.fromJSON s
54encodeJ :: J.JSValue -> BL.ByteString
55encodeJ = toLazyByteString . fromString . J.encode
57encodeToText :: A.Value -> TL.Text
58encodeToText = TLB.toLazyText . A.encodeToTextBuilder . A.toJSON
60encodeViaText :: A.Value -> BL.ByteString
61encodeViaText = TLE.encodeUtf8 . encodeToText
63main :: IO ()
64main = do
65 let enFile = "json-data/twitter100.json"
66 jpFile = "json-data/jp100.json"
67 enA <- BL.readFile enFile
68 enS <- BS.readFile enFile
69 enJ <- readFile enFile
70 jpA <- BL.readFile jpFile
71 jpS <- BS.readFile jpFile
72 jpJ <- readFile jpFile
73 defaultMain [
74 bgroup "decode" [
75 bgroup "en" [
76 bench "aeson/lazy" $ nf decode enA
77 , bench "aeson/strict" $ nf decode' enA
78 , bench "aeson/stricter" $ nf decodeS enS
79 , bench "aeson/parser" $ nf decodeIP enA
80 , bench "json" $ nf decodeJ enJ
81 ]
82 , bgroup "jp" [
83 bench "aeson" $ nf decode jpA
84 , bench "aeson/stricter" $ nf decodeS jpS
85 , bench "json" $ nf decodeJ jpJ
86 ]
87 ]
88 , bgroup "encode" [
89 bgroup "en" [
90 bench "aeson-to-bytestring" $ nf A.encode (decode enA)
91 , bench "aeson-via-text-to-bytestring" $ nf encodeViaText (decode enA)
92 , bench "aeson-to-text" $ nf encodeToText (decode enA)
93 , bench "json" $ nf encodeJ (decodeJ enJ)
94 ]
95 , bgroup "jp" [
96 bench "aeson-to-bytestring" $ nf A.encode (decode jpA)
97 , bench "aeson-via-text-to-bytestring" $ nf encodeViaText (decode jpA)
98 , bench "aeson-to-text" $ nf encodeToText (decode jpA)
99 , bench "json" $ nf encodeJ (decodeJ jpJ)
100 ]
101 ]
102 ]