@@ -15,31 +15,14 @@ module Main where
15
15
import Control.DeepSeq
16
16
import qualified Data.ByteString as BS
17
17
import qualified Data.ByteString.Lazy as LBS
18
-
19
- -- import Data.Monoid ((<>))
18
+ import qualified Data.Text as T
19
+ import qualified Data.Text.Encoding as T
20
20
import Data.Typeable
21
-
22
- -- import Data.Word
23
- -- import Criterion.IO
24
21
import Criterion.Types
25
-
26
- -- import Data.Bifunctor
27
22
import qualified Data.Binary as B
28
-
29
- -- import Data.Binary.Serialise.CBOR as CBOR
30
23
import Codec.Serialise as CBOR
31
-
32
- -- import Data.List
33
- -- import Data.Ord
34
24
import GHC.Generics
35
-
36
- -- import Statistics.Resampling.Bootstrap
37
25
import System.Mem (performMajorGC )
38
-
39
- -- import System.Random
40
- -- import Text.Printf
41
- -- import Data.Binary.Serialise.CBOR.Decoding as CBOR
42
- -- import Data.Binary.Serialise.CBOR.Encoding as CBOR
43
26
import qualified Data.Flat as F
44
27
import qualified Data.Serialize as C
45
28
import qualified Data.Store as S
@@ -58,7 +41,7 @@ data BinTree a
58
41
= Tree (BinTree a )
59
42
(BinTree a )
60
43
| Leaf a
61
- deriving (Show , Eq , Typeable , Generic )
44
+ deriving (Show , Read , Eq , Typeable , Generic )
62
45
63
46
-- General instances
64
47
instance {-# OVERLAPPABLE #-} F. Flat a => F. Flat (BinTree a )
@@ -88,6 +71,10 @@ instance {-# OVERLAPPABLE #-} CBOR.Serialise a =>
88
71
-- instance {-# OVERLAPPING #-} CBOR.Serialise [Direction]
89
72
-- instance {-# OVERLAPPING #-} CBOR.Serialise (BinTree Direction)
90
73
-- instance {-# OVERLAPPING #-} CBOR.Serialise (BinTree Int)
74
+
75
+ -- instance {-# OVERLAPPING #-} F.Flat [Car]
76
+ -- instance {-# OVERLAPPING #-} F.Flat [Iris]
77
+
91
78
instance NFData a => NFData (BinTree a ) where
92
79
rnf (Leaf a) = rnf a `seq` ()
93
80
rnf (Tree left right) = rnf left `seq` rnf right `seq` ()
@@ -178,6 +165,9 @@ data PkgFlat =
178
165
data PkgStore =
179
166
PkgStore
180
167
168
+ data PkgShow =
169
+ PkgShow
170
+
181
171
class Serialize lib a where
182
172
serialize :: lib -> a -> IO BS. ByteString
183
173
deserialize :: lib -> BS. ByteString -> IO a
@@ -220,6 +210,12 @@ instance (F.Flat a, NFData a) => Serialize PkgFlat a where
220
210
{-# NOINLINE deserialize #-}
221
211
deserialize _ = return . force . fromRight . F. unflat
222
212
213
+ instance (Show a , Read a , NFData a ) => Serialize PkgShow a where
214
+ {-# NOINLINE serialize #-}
215
+ serialize _ = return . force . T. encodeUtf8 . T. pack . show
216
+ {-# NOINLINE deserialize #-}
217
+ deserialize _ = return . force . read . T. unpack . T. decodeUtf8
218
+
223
219
pkgs ::
224
220
( NFData a
225
221
, C. Serialize a
@@ -228,15 +224,22 @@ pkgs ::
228
224
, S. Store a
229
225
, F. Flat a
230
226
, B. Binary a
227
+ , Show a
228
+ , Read a
231
229
)
232
230
=> [(String , a -> IO BS. ByteString , BS. ByteString -> IO a )]
231
+ -- pkgs =
232
+ -- [ ("flat-ser", serialize PkgFlat, deserialize PkgFlat)
233
+ -- , ("store-ser", serialize PkgStore, deserialize PkgStore)]
234
+ -- ]
233
235
pkgs =
234
236
[ (" flat" , serialize PkgFlat , deserialize PkgFlat )
235
237
, (" store" , serialize PkgStore , deserialize PkgStore )
236
238
, (" binary" , serialize PkgBinary , deserialize PkgBinary )
237
239
, (" cereal" , serialize PkgCereal , deserialize PkgCereal )
238
240
, (" packman" , serialize PkgPackman , deserialize PkgPackman )
239
241
, (" serialise" , serialize PkgCBOR , deserialize PkgCBOR )
242
+ , (" show" , serialize PkgShow , deserialize PkgShow )
240
243
]
241
244
242
245
prop :: Serialize lib (BinTree Int ) => lib -> Property
@@ -266,6 +269,8 @@ runBench :: IO ()
266
269
runBench
267
270
-- Data structures to (de)serialise
268
271
= do
272
+
273
+ -- datasets
269
274
! intTree <-
270
275
force . (" BinTree Int" , ) <$> (generateBalancedTree 21 :: IO (BinTree Int ))
271
276
! directionTree <-
@@ -277,14 +282,18 @@ runBench
277
282
! carsDataset <- force . (" Cars" , ) <$> carsData
278
283
-- !abaloneDataset <- force . ("Abalone dataset",) <$> abaloneData
279
284
let ! irisDataset = force (" Iris" , irisData)
285
+
280
286
performMajorGC
287
+
281
288
let jsonReport = reportsFile workDir
282
289
let htmlReport = " report.html"
290
+
283
291
let tests =
284
292
benchs directionList ++
285
293
benchs intTree ++
286
294
benchs directionTree ++ benchs carsDataset ++ benchs irisDataset
287
295
-- let tests = []
296
+
288
297
defaultMainWith
289
298
(defaultConfig {jsonFile = Just jsonReport, reportFile = Just htmlReport}) $
290
299
tests
@@ -316,7 +325,7 @@ sizes ::
316
325
, F. Flat t
317
326
, Serialise t
318
327
, C. Serialize t
319
- , S. Store t
328
+ , S. Store t , Show t , Read t
320
329
)
321
330
=> (String , t )
322
331
-> IO ()
@@ -336,15 +345,15 @@ benchs ::
336
345
, F. Flat a
337
346
, Serialise a
338
347
, C. Serialize a
339
- , S. Store a
348
+ , S. Store a , Read a , Show a
340
349
)
341
350
=> (String , a )
342
351
-> [Benchmark ]
343
352
benchs (name, obj) =
344
353
let nm pkg = concat [name, " -" , pkg]
345
354
-- env (return obj) $ \sobj -> bgroup ("serialization (mSecs)") $ map (\(pkg,s,_) -> bench (nm pkg) (nfIO (s sobj))) pkgs
346
355
in [ bgroup " serialization (time)" $
347
- map (\ (pkg, s, _) -> bench (nm pkg) (nfIO (s obj))) pkgs
356
+ map (\ (pkg, s, _) -> bench (nm pkg) (nfIO (BS. length <$> s obj))) pkgs
348
357
-- NOTE: the benchmark time includes the comparison of the deserialised obj with the original
349
358
, bgroup " deserialization (time)" $
350
359
map
0 commit comments