Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 80 lines (70 sloc) 2.5 kb
d84827f @bos Make the benchmarks fairer.
authored
1 {-# OPTIONS_GHC -fno-warn-orphans #-}
102bdd7 @basvandijk Added CompareWithJSON benchmark from issue: #56
basvandijk authored
2
e358efb @bos Encoding performance: compare like with like.
authored
3 import Blaze.ByteString.Builder (toLazyByteString)
4 import Blaze.ByteString.Builder.Char.Utf8 (fromString)
d84827f @bos Make the benchmarks fairer.
authored
5 import Control.DeepSeq (NFData(rnf))
102bdd7 @basvandijk Added CompareWithJSON benchmark from issue: #56
basvandijk authored
6 import Criterion.Main
8f206be @meiersi Use fast 'encodeUtf8BuilderEscaped' if possible
meiersi authored
7 import qualified Data.Aeson.Encode as A
d84827f @bos Make the benchmarks fairer.
authored
8 import qualified Data.Aeson as A
102bdd7 @basvandijk Added CompareWithJSON benchmark from issue: #56
basvandijk authored
9 import qualified Data.ByteString.Lazy as BL
10 import qualified Text.JSON as J
8f206be @meiersi Use fast 'encodeUtf8BuilderEscaped' if possible
meiersi authored
11 import qualified Data.Text.Lazy as TL
12 import qualified Data.Text.Lazy.Builder as TLB
13 import qualified Data.Text.Lazy.Encoding as TLE
102bdd7 @basvandijk Added CompareWithJSON benchmark from issue: #56
basvandijk authored
14
15 instance (NFData v) => NFData (J.JSObject v) where
16 rnf o = rnf (J.fromJSObject o)
17
18 instance NFData J.JSValue where
19 rnf J.JSNull = ()
20 rnf (J.JSBool b) = rnf b
21 rnf (J.JSRational a b) = rnf a `seq` rnf b `seq` ()
22 rnf (J.JSString s) = rnf (J.fromJSString s)
23 rnf (J.JSArray lst) = rnf lst
24 rnf (J.JSObject o) = rnf o
25
a42161d @basvandijk encode and decode JSValues instead of JSObjects
basvandijk authored
26 decodeJ :: String -> J.JSValue
102bdd7 @basvandijk Added CompareWithJSON benchmark from issue: #56
basvandijk authored
27 decodeJ s =
28 case J.decodeStrict s of
29 J.Ok v -> v
30 J.Error _ -> error "fail to parse via JSON"
31
32 decodeA :: BL.ByteString -> A.Value
d84827f @bos Make the benchmarks fairer.
authored
33 decodeA s = case A.decode s of
102bdd7 @basvandijk Added CompareWithJSON benchmark from issue: #56
basvandijk authored
34 Just v -> v
d84827f @bos Make the benchmarks fairer.
authored
35 Nothing -> error "fail to parse via Aeson"
102bdd7 @basvandijk Added CompareWithJSON benchmark from issue: #56
basvandijk authored
36
e358efb @bos Encoding performance: compare like with like.
authored
37 encodeJ :: J.JSValue -> BL.ByteString
38 encodeJ = toLazyByteString . fromString . J.encode
39
8f206be @meiersi Use fast 'encodeUtf8BuilderEscaped' if possible
meiersi authored
40 encodeToText :: A.Value -> TL.Text
41 encodeToText = TLB.toLazyText . A.encodeToTextBuilder . A.toJSON
42
43 encodeViaText :: A.Value -> BL.ByteString
44 encodeViaText = TLE.encodeUtf8 . encodeToText
45
102bdd7 @basvandijk Added CompareWithJSON benchmark from issue: #56
basvandijk authored
46 main :: IO ()
47 main = do
994b227 @bos Make benchmarks more comprehensive.
authored
48 let enFile = "json-data/twitter100.json"
49 jpFile = "json-data/jp100.json"
50 enA <- BL.readFile enFile
51 enJ <- readFile enFile
52 jpA <- BL.readFile jpFile
53 jpJ <- readFile jpFile
17013cc @bos Build Haskell benchmarks with cabal
authored
54 defaultMain [
994b227 @bos Make benchmarks more comprehensive.
authored
55 bgroup "decode" [
56 bgroup "en" [
57 bench "aeson" $ nf decodeA enA
58 , bench "json" $ nf decodeJ enJ
59 ]
60 , bgroup "jp" [
61 bench "aeson" $ nf decodeA jpA
62 , bench "json" $ nf decodeJ jpJ
63 ]
64 ]
65 , bgroup "encode" [
66 bgroup "en" [
8f206be @meiersi Use fast 'encodeUtf8BuilderEscaped' if possible
meiersi authored
67 bench "aeson-to-bytestring" $ nf A.encode (decodeA enA)
68 , bench "aeson-via-text-to-bytestring" $ nf encodeViaText (decodeA enA)
69 , bench "aeson-to-text" $ nf encodeToText (decodeA enA)
e358efb @bos Encoding performance: compare like with like.
authored
70 , bench "json" $ nf encodeJ (decodeJ enJ)
994b227 @bos Make benchmarks more comprehensive.
authored
71 ]
72 , bgroup "jp" [
8f206be @meiersi Use fast 'encodeUtf8BuilderEscaped' if possible
meiersi authored
73 bench "aeson-to-bytestring" $ nf A.encode (decodeA jpA)
74 , bench "aeson-via-text-to-bytestring" $ nf encodeViaText (decodeA jpA)
75 , bench "aeson-to-text" $ nf encodeToText (decodeA jpA)
e358efb @bos Encoding performance: compare like with like.
authored
76 , bench "json" $ nf encodeJ (decodeJ jpJ)
994b227 @bos Make benchmarks more comprehensive.
authored
77 ]
78 ]
79 ]
Something went wrong with that request. Please try again.