on 1.3.1.0 help sign in
typeable/aeson/benchmarks/AutoCompare.hs
73 lines 2.0 kB history raw
1{-# LANGUAGE RankNTypes #-}
2{-# LANGUAGE ScopedTypeVariables #-}
4module Main (main) where
6import Control.DeepSeq
7import Control.Monad
8import Criterion.Main
9import Data.Aeson
11import qualified Auto.T.D as T
12import qualified Auto.T.BigRecord as T
13import qualified Auto.T.BigProduct as T
14import qualified Auto.T.BigSum as T
15import qualified Auto.G.D as G
16import qualified Auto.G.BigRecord as G
17import qualified Auto.G.BigProduct as G
18import qualified Auto.G.BigSum as G
20--------------------------------------------------------------------------------
22runBench :: IO ()
23runBench = defaultMain
24 [ compareBench "D" T.d G.d
25 , compareBench "BigRecord" T.bigRecord G.bigRecord
26 , compareBench "BigProduct" T.bigProduct G.bigProduct
27 , compareBench "BigSum" T.bigSum G.bigSum
28 ]
30group :: String -> Benchmarkable -> Benchmarkable -> Benchmark
31group n th gen = bgroup n [ bench "th" th
32 , bench "generic" gen
33 ]
35compareBench
36 :: forall a b
37 . (ToJSON a, FromJSON a, NFData a, ToJSON b, FromJSON b, NFData b)
38 => String -> a -> b -> Benchmark
39compareBench name a b = v `deepseq` bgroup name
40 [ group "toJSON" (nf toJSON a)
41 (nf toJSON b)
42 , group "encode" (nf encode a)
43 (nf encode b)
44 , group "fromJSON" (nf (fromJSON :: Value -> Result a) v)
45 (nf (fromJSON :: Value -> Result b) v)
46 ] where
47 v = toJSON a -- == toJSON b
49sanityCheck :: IO ()
50sanityCheck = do
51 check T.d
52 check G.d
53 check T.bigRecord
54 check G.bigRecord
55 check T.bigProduct
56 check G.bigProduct
57 check T.bigSum
58 check G.bigSum
60check :: (Show a, Eq a, FromJSON a, ToJSON a)
61 => a -> IO ()
62check x = do
63 unless (Success x == (fromJSON . toJSON) x) $ fail $ "toJSON: " ++ show x
64 unless (Success x == (decode_ . encode) x) $ fail $ "encode: " ++ show x
65 where
66 decode_ s = case decode s of
67 Just v -> fromJSON v
68 Nothing -> fail ""
70main :: IO ()
71main = do
72 sanityCheck
73 runBench