forked from haskell/aeson
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added CompareWithJSON benchmark from issue: haskell#56
- Loading branch information
1 parent
ab1956c
commit dcc6b73
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{-# OPTIONS_GHC -fno-warn-orphans#-} | ||
|
||
import Criterion.Main | ||
import Control.DeepSeq | ||
import Data.Int | ||
import qualified Data.ByteString.Lazy 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) | ||
|
||
instance NFData J.JSValue where | ||
rnf J.JSNull = () | ||
rnf (J.JSBool b) = rnf b | ||
rnf (J.JSRational a b) = rnf a `seq` rnf b `seq` () | ||
rnf (J.JSString s) = rnf (J.fromJSString s) | ||
rnf (J.JSArray lst) = rnf lst | ||
rnf (J.JSObject o) = rnf o | ||
|
||
encodeJ :: J.JSObject J.JSValue -> Int | ||
encodeJ = length . J.encode | ||
|
||
encodeA :: A.Value -> Int64 | ||
encodeA = BL.length . A.encode | ||
|
||
decodeJ :: String -> J.JSObject J.JSValue | ||
decodeJ s = | ||
case J.decodeStrict s of | ||
J.Ok v -> v | ||
J.Error _ -> error "fail to parse via JSON" | ||
|
||
decodeA :: BL.ByteString -> A.Value | ||
decodeA s = case A.decode' s of | ||
Nothing -> error "fail to parse via Aeson" | ||
Just v -> v | ||
|
||
jsonData :: FilePath | ||
jsonData = "benchmarks/json-data/jp100.json" | ||
|
||
main :: IO () | ||
main = do | ||
js <- readFile jsonData | ||
as <- BL.readFile jsonData | ||
let jdata = decodeJ js | ||
adata = decodeA as | ||
(jdata, adata) `deepseq` defaultMain [ | ||
bgroup "decode" [ bench "json" $ nf decodeJ js | ||
, bench "aeson" $ nf decodeA as | ||
], | ||
bgroup "encode" [ bench "json" $ nf encodeJ jdata | ||
, bench "aeson" $ nf encodeA adata | ||
] | ||
] |