on attoparsec-iso8601-1.0.0.0-r1 help sign in
214 lines 6.2 kB history raw
1{-# LANGUAGE FlexibleContexts #-}
2{-# LANGUAGE RankNTypes #-}
4{-# OPTIONS_GHC -fno-warn-orphans #-}
6module Main (main) where
8import Prelude ()
9import Prelude.Compat
11import Control.DeepSeq
12import Criterion.Main
13import Data.Hashable
14import Data.Proxy (Proxy (..))
15import Data.Tagged (Tagged (..))
16import Data.Aeson
17import Data.Aeson.Types (fromJSONKeyCoerce)
18import qualified Data.ByteString.Lazy as LBS
19import qualified Data.HashMap.Strict as HM
20import qualified Data.Map as M
21import qualified Data.Text as T
23value :: Int -> HM.HashMap T.Text T.Text
24value n = HM.fromList $ map f [1..n]
25 where
26 f m = let t = T.pack (show m) in (t, t)
28-------------------------------------------------------------------------------
29-- Orphans
30-------------------------------------------------------------------------------
32instance Hashable b => Hashable (Tagged a b) where
33 hashWithSalt salt (Tagged a) = hashWithSalt salt a
35-------------------------------------------------------------------------------
36-- Text
37-------------------------------------------------------------------------------
39newtype T1 = T1 T.Text
40 deriving (Eq, Ord)
42instance NFData T1 where
43 rnf (T1 t) = rnf t
44instance Hashable T1 where
45 hashWithSalt salt (T1 t) = hashWithSalt salt t
47instance FromJSON T1 where
48 parseJSON = withText "T1" $ pure . T1
49instance FromJSONKey T1 where
50 fromJSONKey = FromJSONKeyText T1
52-------------------------------------------------------------------------------
53-- Coerce
54-------------------------------------------------------------------------------
56newtype T2 = T2 T.Text
57 deriving (Eq, Ord)
59instance NFData T2 where
60 rnf (T2 t) = rnf t
61instance Hashable T2 where
62 hashWithSalt salt (T2 t) = hashWithSalt salt t
64instance FromJSON T2 where
65 parseJSON = withText "T2" $ pure . T2
66instance FromJSONKey T2 where
67 fromJSONKey = fromJSONKeyCoerce
69-------------------------------------------------------------------------------
70-- TextParser
71-------------------------------------------------------------------------------
73newtype T3 = T3 T.Text
74 deriving (Eq, Ord)
76instance NFData T3 where
77 rnf (T3 t) = rnf t
78instance Hashable T3 where
79 hashWithSalt salt (T3 t) = hashWithSalt salt t
81instance FromJSON T3 where
82 parseJSON = withText "T3" $ pure . T3
83instance FromJSONKey T3 where
84 fromJSONKey = FromJSONKeyTextParser (pure . T3)
86-------------------------------------------------------------------------------
87-- Values
88-------------------------------------------------------------------------------
90value10, value100, value1000, value10000 :: HM.HashMap T.Text T.Text
91value10 = value 10
92value100 = value 100
93value1000 = value 1000
94value10000 = value 10000
96encodedValue10 :: LBS.ByteString
97encodedValue10 = encode value10
99encodedValue100 :: LBS.ByteString
100encodedValue100 = encode value100
102encodedValue1000 :: LBS.ByteString
103encodedValue1000 = encode value1000
105encodedValue10000 :: LBS.ByteString
106encodedValue10000 = encode value10000
108-------------------------------------------------------------------------------
109-- Helpers
110-------------------------------------------------------------------------------
112decodeHM
113 :: (FromJSON (HM.HashMap k T.Text), Eq k, Hashable k)
114 => Proxy k -> LBS.ByteString -> Maybe (HM.HashMap k T.Text)
115decodeHM _ = decode
117decodeMap
118 :: (FromJSON (M.Map k T.Text), Ord k)
119 => Proxy k -> LBS.ByteString -> Maybe (M.Map k T.Text)
120decodeMap _ = decode
122proxyText :: Proxy T.Text
123proxyText = Proxy
125proxyT1 :: Proxy T1
126proxyT1 = Proxy
128proxyT2 :: Proxy T2
129proxyT2 = Proxy
131proxyT3 :: Proxy T3
132proxyT3 = Proxy
134proxyTagged :: Proxy a -> Proxy (Tagged () a)
135proxyTagged _ = Proxy
137-------------------------------------------------------------------------------
138-- Main
139-------------------------------------------------------------------------------
141benchDecodeHM
142 :: String
143 -> LBS.ByteString
144 -> Benchmark
145benchDecodeHM name val = bgroup name
146 [ bench "Text" $ nf (decodeHM proxyText) val
147 , bench "Identity" $ nf (decodeHM proxyT1) val
148 , bench "Coerce" $ nf (decodeHM proxyT2) val
149 , bench "Parser" $ nf (decodeHM proxyT3) val
150 , bench "Tagged Text" $ nf (decodeHM $ proxyTagged proxyText) val
151 , bench "Tagged Identity" $ nf (decodeHM $ proxyTagged proxyT1) val
152 , bench "Tagged Coerce" $ nf (decodeHM $ proxyTagged proxyT2) val
153 , bench "Tagged Parser" $ nf (decodeHM $ proxyTagged proxyT3) val
154 ]
156benchDecodeMap
157 :: String
158 -> LBS.ByteString
159 -> Benchmark
160benchDecodeMap name val = bgroup name
161 [ bench "Text" $ nf (decodeMap proxyText) val
162 , bench "Identity" $ nf (decodeMap proxyT1) val
163 , bench "Coerce" $ nf (decodeMap proxyT2) val
164 , bench "Parser" $ nf (decodeMap proxyT3) val
165 ]
167benchEncodeHM
168 :: String
169 -> HM.HashMap T.Text T.Text
170 -> Benchmark
171benchEncodeHM name val = bgroup name
172 [ bench "Text" $ nf encode val
173 ]
175benchEncodeMap
176 :: String
177 -> HM.HashMap T.Text T.Text
178 -> Benchmark
179benchEncodeMap name val = bgroup name
180 [ bench "Text" $ nf encode val'
181 ]
182 where
183 val' :: M.Map T.Text T.Text
184 val' = M.fromList . HM.toList $ val
186main :: IO ()
187main = defaultMain
188 [ bgroup "decode"
189 [ bgroup "HashMap"
190 [ benchDecodeHM "10" encodedValue10
191 , benchDecodeHM "100" encodedValue100
192 , benchDecodeHM "1000" encodedValue1000
193 , benchDecodeHM "10000" encodedValue10000
194 ]
195 , bgroup "Map"
196 [ benchDecodeMap "10" encodedValue10
197 , benchDecodeMap "100" encodedValue100
198 , benchDecodeMap "1000" encodedValue1000
199 , benchDecodeMap "10000" encodedValue10000
200 ]
201 ]
202 , bgroup "encode"
203 [ bgroup "HashMap"
204 [ benchEncodeHM "100" value100
205 , benchEncodeHM "1000" value1000
206 , benchEncodeHM "10000" value10000
207 ]
208 , bgroup "Map"
209 [ benchEncodeMap "100" value100
210 , benchEncodeMap "1000" value1000
211 , benchEncodeMap "10000" value10000
212 ]
213 ]
214 ]