-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Main.hs
140 lines (136 loc) · 4.43 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Main where
import Autodocodec
import Autodocodec.Usage
import Control.DeepSeq
import Control.Monad
import Criterion.Main as Criterion
import qualified Data.Aeson as Aeson
import qualified Data.Aeson as JSON
import qualified Data.Aeson.Types as Aeson
import qualified Data.ByteString.Lazy as LB
import Data.GenValidity.Containers ()
import Data.GenValidity.Text ()
import Data.GenValidity.Time ()
import Data.Int
import Data.List.NonEmpty (NonEmpty)
import Data.Map (Map)
import Data.Maybe
import Data.Set (Set)
import Data.Text (Text)
import qualified Data.Text.Lazy as LT
import Data.Time
import Data.Typeable
import Data.Vector (Vector)
import Data.Word
import Numeric.Natural
import Test.QuickCheck.Gen
import Test.QuickCheck.Random
import Test.Syd.Validity
import Test.Syd.Validity.Utils
main :: IO ()
main =
Criterion.defaultMain
[ comparisonBench @NullUnit,
comparisonBench @Bool,
comparisonBench @Ordering,
comparisonBench @Text,
comparisonBench @LT.Text,
-- Do not roundtrip
-- comparisonBench @Char,
-- comparisonBench @String,
-- comparisonBench @Scientific,
-- comparisonBench @JSON.Object,
-- comparisonBench @JSON.Value,
comparisonBench @Int,
comparisonBench @Int8,
comparisonBench @Int16,
comparisonBench @Int32,
comparisonBench @Int64,
comparisonBench @Integer,
comparisonBench @Word,
comparisonBench @Word8,
comparisonBench @Word16,
comparisonBench @Word32,
comparisonBench @Word64,
comparisonBench @Natural,
comparisonBench @(Maybe Text),
comparisonBench @(Either Bool Text),
comparisonBench @(Either (Either Bool [Text]) Text),
comparisonBench @(Vector Text),
comparisonBench @[Text],
comparisonBench @(NonEmpty Text),
comparisonBench @(Set Text),
comparisonBench @(Map Text Int),
comparisonBench @Day,
comparisonBench @LocalTime,
comparisonBench @UTCTime,
comparisonBench @TimeOfDay,
comparisonBench @DiffTime,
comparisonBench @NominalDiffTime,
comparisonBench @Fruit,
comparisonBench @Shape,
comparisonBench @Example,
comparisonBench @Recursive,
comparisonBench @Via,
comparisonBench @VeryComment,
comparisonBench @LegacyValue,
comparisonBench @LegacyObject
]
comparisonBench ::
forall a.
( GenValid a,
Typeable a,
HasCodec a,
Aeson.FromJSON a,
Aeson.ToJSON a,
NFData a
) =>
Benchmark
comparisonBench =
let genAs =
pure (unGen (replicateM 100 genValid) (mkQCGen 42) 30 :: [a])
in env genAs $ \as ->
bgroup
(nameOf @a)
[ bgroup
"encoding"
[ bgroup
"toJSON"
[ bench "toJSON (Autodocodec)" $
nf toJSONViaCodec as,
bench "toJSON (Aeson)" $
nf Aeson.toJSON as
],
bgroup
"toByteString"
[ bench "encode (Autodocodec)" $
nf encodeJSONViaCodec as,
bench "encode (Aeson)" $
nf Aeson.encode as
]
],
let genBS = pure $ LB.fromStrict . LB.toStrict . Aeson.encode $ as
in env genBS $ \bs ->
bgroup
"decoding"
[ let decodeToValue = pure $ fromJust $ Aeson.decode bs
in env decodeToValue $ \val ->
bgroup
"parseJSON"
[ bench "parseJSON (Autodocodec)" $
nf (Aeson.parseMaybe parseJSONViaCodec :: JSON.Value -> Maybe a) val,
bench "parseJSON (Aeson)" $
nf (Aeson.parseMaybe Aeson.parseJSON :: JSON.Value -> Maybe a) val
],
bgroup
"parseByteString"
[ bench "decode (Autodocodec)" $
nf (eitherDecodeJSONViaCodec :: LB.ByteString -> Either String a) bs,
bench "decode (Aeson)" $
nf (Aeson.eitherDecode :: LB.ByteString -> Either String a) bs
]
]
]