Bryan O'Sullivan avatar Bryan O'Sullivan committed 92d62a8

Make the benchmarks fairer.

Comments (0)

Files changed (1)


-{-# OPTIONS_GHC -fno-warn-orphans#-}
+{-# OPTIONS_GHC -fno-warn-orphans #-}
+import Control.DeepSeq (NFData(rnf))
 import Criterion.Main
-import Control.DeepSeq
-import Data.Int
+import qualified Data.Aeson as A
 import qualified Data.ByteString.Lazy as BL
+import qualified Data.ByteString.Lazy.Internal as BL
 import qualified Text.JSON as J
-import qualified Data.Aeson as A
 instance (NFData v) => NFData (J.JSObject v) where
   rnf o = rnf (J.fromJSObject o)
   rnf (J.JSArray lst) = rnf lst
   rnf (J.JSObject o) = rnf o
-encodeJ :: J.JSValue -> Int
-encodeJ = length . J.encode
-encodeA :: A.Value -> Int64
-encodeA = BL.length . A.encode
+instance NFData BL.ByteString where
+  rnf (BL.Chunk _ bs) = rnf bs
+  rnf BL.Empty        = ()
 decodeJ :: String -> J.JSValue
 decodeJ s =
     J.Error _ -> error "fail to parse via JSON"
 decodeA :: BL.ByteString -> A.Value
-decodeA s = case A.decode' s of
+decodeA s = case A.decode s of
+              Just v -> v
               Nothing -> error "fail to parse via Aeson"
-              Just v -> v
 jsonData :: FilePath
 jsonData = "json-data/jp100.json"
         bgroup "decode" [ bench "json"  $ nf decodeJ js
                         , bench "aeson" $ nf decodeA as
-        bgroup "encode" [ bench "json"  $ nf encodeJ jdata
-                        , bench "aeson" $ nf encodeA adata
+        bgroup "encode" [ bench "json"  $ nf J.encode jdata
+                        , bench "aeson" $ nf A.encode adata
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.