@@ -1,282 +0,0 @@
1−{-# LANGUAGE DeriveGeneric #-}
2−{-# LANGUAGE FlexibleContexts #-}
3−{-# LANGUAGE TemplateHaskell #-}
5−module Main (main) where
11−import Control.DeepSeq (NFData, rnf, deepseq)
12−import Criterion.Main hiding (defaultOptions)
14−import Data.Aeson.Encoding
16−import Data.Aeson.Types
17−import Data.ByteString.Lazy (ByteString)
18−import Data.Data (Data)
19−import Data.Typeable (Typeable)
20−import GHC.Generics (Generic, Rep)
23−toBS :: Encoding -> ByteString
24−toBS = encodingToLazyByteString
26−gEncode :: (Generic a, GToEncoding Zero (Rep a)) => a -> ByteString
27−gEncode = toBS . genericToEncoding opts
29−--------------------------------------------------------------------------------
33− | Product String Char a
34− | Record { testOne :: Double
42−instance NFData a => NFData (D a) where
44− rnf (Unary n) = rnf n
45− rnf (Product s c x) = s `deepseq` c `deepseq` rnf x
46− rnf (Record d b y) = d `deepseq` b `deepseq` rnf y
52− { testOne = 1234.56789
54− , testThree = Product "Hello World!" 'a'
56− { testOne = 9876.54321
58− , testThree = Product "Yeehaa!!!" '\n' Nullary
62−--------------------------------------------------------------------------------
66− | Product' String Char a
67− | Record' { testOne' :: Double
71− deriving (Show, Eq, Generic)
73−instance ToJSON a => ToJSON (D' a) where
74− toJSON = genericToJSON opts
76−instance FromJSON a => FromJSON (D' a) where
77− parseJSON = genericParseJSON opts
79−instance NFData a => NFData (D' a) where
81− rnf (Unary' n) = rnf n
82− rnf (Product' s c x) = s `deepseq` c `deepseq` rnf x
83− rnf (Record' d b y) = d `deepseq` b `deepseq` rnf y
85−type T' = D' (D' (D' ()))
89− { testOne' = 1234.56789
91− , testThree' = Product' "Hello World!" 'a'
93− { testOne' = 9876.54321
95− , testThree' = Product' "Yeehaa!!!" '\n' Nullary'
99−--------------------------------------------------------------------------------
101−data BigRecord = BigRecord
102− { field01 :: !Int, field02 :: !Int, field03 :: !Int, field04 :: !Int, field05 :: !Int
103− , field06 :: !Int, field07 :: !Int, field08 :: !Int, field09 :: !Int, field10 :: !Int
104− , field11 :: !Int, field12 :: !Int, field13 :: !Int, field14 :: !Int, field15 :: !Int
105− , field16 :: !Int, field17 :: !Int, field18 :: !Int, field19 :: !Int, field20 :: !Int
106− , field21 :: !Int, field22 :: !Int, field23 :: !Int, field24 :: !Int, field25 :: !Int
107− } deriving (Show, Eq, Generic)
109−instance NFData BigRecord
111−bigRecord = BigRecord 1 2 3 4 5
119−gBigRecordToJSON :: BigRecord -> Value
120−gBigRecordToJSON = genericToJSON opts
122−gBigRecordEncode :: BigRecord -> ByteString
123−gBigRecordEncode = gEncode
125−gBigRecordFromJSON :: Value -> Result BigRecord
126−gBigRecordFromJSON = parse $ genericParseJSON opts
128−thBigRecordToJSON :: BigRecord -> Value
129−thBigRecordToJSON = $(mkToJSON opts ''BigRecord)
131−thBigRecordEncode :: BigRecord -> ByteString
132−thBigRecordEncode = toBS . $(mkToEncoding opts ''BigRecord)
134−thBigRecordFromJSON :: Value -> Result BigRecord
135−thBigRecordFromJSON = parse $(mkParseJSON opts ''BigRecord)
137−--------------------------------------------------------------------------------
139−data BigProduct = BigProduct
140− !Int !Int !Int !Int !Int
141− !Int !Int !Int !Int !Int
142− !Int !Int !Int !Int !Int
143− !Int !Int !Int !Int !Int
144− !Int !Int !Int !Int !Int
145− deriving (Show, Eq, Generic)
147−instance NFData BigProduct
149−bigProduct = BigProduct 1 2 3 4 5
157−gBigProductToJSON :: BigProduct -> Value
158−gBigProductToJSON = genericToJSON opts
160−gBigProductEncode :: BigProduct -> ByteString
161−gBigProductEncode = gEncode
163−gBigProductFromJSON :: Value -> Result BigProduct
164−gBigProductFromJSON = parse $ genericParseJSON opts
166−thBigProductToJSON :: BigProduct -> Value
167−thBigProductToJSON = $(mkToJSON opts ''BigProduct)
169−thBigProductEncode :: BigProduct -> ByteString
170−thBigProductEncode = toBS . $(mkToEncoding opts ''BigProduct)
172−thBigProductFromJSON :: Value -> Result BigProduct
173−thBigProductFromJSON = parse $(mkParseJSON opts ''BigProduct)
175−--------------------------------------------------------------------------------
177−data BigSum = F01 | F02 | F03 | F04 | F05
178− | F06 | F07 | F08 | F09 | F10
179− | F11 | F12 | F13 | F14 | F15
180− | F16 | F17 | F18 | F19 | F20
181− | F21 | F22 | F23 | F24 | F25
182− deriving (Show, Eq, Generic)
184−instance NFData BigSum
190−gBigSumToJSON :: BigSum -> Value
191−gBigSumToJSON = genericToJSON opts
193−gBigSumEncode :: BigSum -> ByteString
194−gBigSumEncode = gEncode
196−gBigSumFromJSON :: Value -> Result BigSum
197−gBigSumFromJSON = parse $ genericParseJSON opts
199−thBigSumToJSON :: BigSum -> Value
200−thBigSumToJSON = $(mkToJSON opts ''BigSum)
202−thBigSumEncode :: BigSum -> ByteString
203−thBigSumEncode = toBS . $(mkToEncoding opts ''BigSum)
205−thBigSumFromJSON :: Value -> Result BigSum
206−thBigSumFromJSON = parse $(mkParseJSON opts ''BigSum)
208−--------------------------------------------------------------------------------
210−type FJ a = Value -> Result a
213−runBench = defaultMain
215− in (d, d', v) `deepseq`
217− [ group "toJSON" (nf toJSON d)
219− , group "encode" (nf encode d)
221− , group "fromJSON" (nf ( fromJSON :: FJ T ) v)
222− (nf ( fromJSON :: FJ T') v)
224− , let v = thBigRecordToJSON bigRecord
225− in bigRecord `deepseq` v `deepseq`
227− [ group "toJSON" (nf thBigRecordToJSON bigRecord)
228− (nf gBigRecordToJSON bigRecord)
229− , group "encode" (nf thBigRecordEncode bigRecord)
230− (nf gBigRecordEncode bigRecord)
231− , group "fromJSON" (nf (thBigRecordFromJSON :: FJ BigRecord) v)
232− (nf ( gBigRecordFromJSON :: FJ BigRecord) v)
234− , let v = thBigProductToJSON bigProduct
235− in bigProduct `deepseq` v `deepseq`
237− [ group "toJSON" (nf thBigProductToJSON bigProduct)
238− (nf gBigProductToJSON bigProduct)
239− , group "encode" (nf thBigProductEncode bigProduct)
240− (nf gBigProductEncode bigProduct)
241− , group "fromJSON" (nf (thBigProductFromJSON :: FJ BigProduct) v)
242− (nf (gBigProductFromJSON :: FJ BigProduct) v)
244− , let v = thBigSumToJSON bigSum
245− in bigSum `deepseq` v `deepseq`
247− [ group "toJSON" (nf thBigSumToJSON bigSum)
248− (nf gBigSumToJSON bigSum)
249− , group "encode" (nf thBigSumEncode bigSum)
250− (nf gBigSumEncode bigSum)
251− , group "fromJSON" (nf (thBigSumFromJSON :: FJ BigSum) v)
252− (nf (gBigSumFromJSON :: FJ BigSum) v)
256−group n th gen = bgroup n [ bench "th" th
257− , bench "generic" gen
261− check d toJSON fromJSON encode
262− check d' toJSON fromJSON encode
263− check bigRecord thBigRecordToJSON thBigRecordFromJSON thBigRecordEncode
264− check bigRecord gBigRecordToJSON gBigRecordFromJSON gBigRecordEncode
265− check bigProduct thBigProductToJSON thBigProductFromJSON thBigProductEncode
266− check bigProduct gBigProductToJSON gBigProductFromJSON gBigProductEncode
267− check bigSum thBigSumToJSON thBigSumFromJSON thBigSumEncode
268− check bigSum gBigSumToJSON gBigSumFromJSON gBigSumEncode
270−check :: (Show a, Eq a)
271− => a -> (a -> Value) -> (Value -> Result a) -> (a -> ByteString) -> IO ()
272−check x toJSON fromJSON encode = do
273− unless (Success x == (fromJSON . toJSON) x) $ fail $ "toJSON: " ++ show x
274− unless (Success x == (decode' . encode) x) $ fail $ "encode: " ++ show x
276− decode' s = case decode s of
277− Just v -> fromJSON v