on 1.3.1.0 help sign in
typeable/aeson/benchmarks/CompareWithJSON.hs
101 lines 3.3 kB history raw
1{-# OPTIONS_GHC -fno-warn-orphans #-}
3module Main (main) where
5import Prelude ()
6import Prelude.Compat
8import Blaze.ByteString.Builder (toLazyByteString)
9import Blaze.ByteString.Builder.Char.Utf8 (fromString)
10import Control.DeepSeq (NFData(rnf))
11import Criterion.Main
12import Data.Maybe (fromMaybe)
13import qualified Data.Aeson as A
14import qualified Data.Aeson.Text as A
15import qualified Data.Aeson.Parser.Internal as I
16import qualified Data.ByteString as BS
17import qualified Data.ByteString.Lazy as BL
18import qualified Data.Text.Lazy as TL
19import qualified Data.Text.Lazy.Builder as TLB
20import qualified Data.Text.Lazy.Encoding as TLE
21import qualified Text.JSON as J
23instance (NFData v) => NFData (J.JSObject v) where
24 rnf o = rnf (J.fromJSObject o)
26instance NFData J.JSValue where
27 rnf J.JSNull = ()
28 rnf (J.JSBool b) = rnf b
29 rnf (J.JSRational a b) = rnf a `seq` rnf b `seq` ()
30 rnf (J.JSString s) = rnf (J.fromJSString s)
31 rnf (J.JSArray lst) = rnf lst
32 rnf (J.JSObject o) = rnf o
34decodeJ :: String -> J.JSValue
35decodeJ s =
36 case J.decodeStrict s of
37 J.Ok v -> v
38 J.Error _ -> error "fail to parse via JSON"
40decode :: BL.ByteString -> A.Value
41decode s = fromMaybe (error "fail to parse via Aeson") $ A.decode s
43decode' :: BL.ByteString -> A.Value
44decode' s = fromMaybe (error "fail to parse via Aeson") $ A.decode' s
46decodeS :: BS.ByteString -> A.Value
47decodeS s = fromMaybe (error "fail to parse via Aeson") $ A.decodeStrict' s
49decodeIP :: BL.ByteString -> A.Value
50decodeIP s = fromMaybe (error "fail to parse via Parser.decodeWith") $
51 I.decodeWith I.jsonEOF A.fromJSON s
53encodeJ :: J.JSValue -> BL.ByteString
54encodeJ = toLazyByteString . fromString . J.encode
56encodeToText :: A.Value -> TL.Text
57encodeToText = TLB.toLazyText . A.encodeToTextBuilder . A.toJSON
59encodeViaText :: A.Value -> BL.ByteString
60encodeViaText = TLE.encodeUtf8 . encodeToText
62main :: IO ()
63main = do
64 let enFile = "json-data/twitter100.json"
65 jpFile = "json-data/jp100.json"
66 enA <- BL.readFile enFile
67 enS <- BS.readFile enFile
68 enJ <- readFile enFile
69 jpA <- BL.readFile jpFile
70 jpS <- BS.readFile jpFile
71 jpJ <- readFile jpFile
72 defaultMain [
73 bgroup "decode" [
74 bgroup "en" [
75 bench "aeson/lazy" $ nf decode enA
76 , bench "aeson/strict" $ nf decode' enA
77 , bench "aeson/stricter" $ nf decodeS enS
78 , bench "aeson/parser" $ nf decodeIP enA
79 , bench "json" $ nf decodeJ enJ
80 ]
81 , bgroup "jp" [
82 bench "aeson" $ nf decode jpA
83 , bench "aeson/stricter" $ nf decodeS jpS
84 , bench "json" $ nf decodeJ jpJ
85 ]
86 ]
87 , bgroup "encode" [
88 bgroup "en" [
89 bench "aeson-to-bytestring" $ nf A.encode (decode enA)
90 , bench "aeson-via-text-to-bytestring" $ nf encodeViaText (decode enA)
91 , bench "aeson-to-text" $ nf encodeToText (decode enA)
92 , bench "json" $ nf encodeJ (decodeJ enJ)
93 ]
94 , bgroup "jp" [
95 bench "aeson-to-bytestring" $ nf A.encode (decode jpA)
96 , bench "aeson-via-text-to-bytestring" $ nf encodeViaText (decode jpA)
97 , bench "aeson-to-text" $ nf encodeToText (decode jpA)
98 , bench "json" $ nf encodeJ (decodeJ jpJ)
99 ]
100 ]
101 ]