@@ -0,0 +1,144 @@
1+{-# LANGUAGE TemplateHaskell #-}
5+{-# OPTIONS_GHC -ddump-simpl -ddump-to-file -dsuppress-all #-}
9+-- Module : Streamly.Benchmark.Data.Serialize
10+-- Copyright : (c) 2023 Composewell
11+-- License : BSD-3-Clause
12+-- Maintainer : streamly@composewell.com
14+module Main (main) where
16+-------------------------------------------------------------------------------
18+-------------------------------------------------------------------------------
20+import Control.DeepSeq (NFData(..))
21+import Control.Exception (assert)
22+import Control.Monad (replicateM_)
23+import GHC.Generics (Generic)
24+import System.Random (randomRIO)
26+import Streamly.Internal.Data.Unbox (newBytes)
27+import Streamly.Internal.Data.Serialize
30+import Streamly.Internal.Data.Serialize.TH
34+import Streamly.Benchmark.Common
36+-------------------------------------------------------------------------------
38+-------------------------------------------------------------------------------
44+ deriving (Generic, Show, Eq)
47+instance Serialize CustomDT1
49+$(deriveSerialize ''CustomDT1)
52+-------------------------------------------------------------------------------
54+-------------------------------------------------------------------------------
56+{-# INLINE pokeTimes #-}
57+pokeTimes :: forall a. Serialize a => a -> Int -> IO ()
58+pokeTimes val times = do
60+ case size :: Size a of
64+ replicateM_ times $ do
67+{-# INLINE peekTimes #-}
68+peekTimes :: forall a. Serialize a => a -> Int -> IO ()
69+peekTimes val times = do
71+ case size :: Size a of
75+ _ <- serialize 0 arr val
76+ replicateM_ times $ do
77+ (_, _ :: a) <- deserialize 0 arr
80+{-# INLINE roundtrip #-}
81+roundtrip :: forall a. (Eq a, Serialize a) => a -> Int -> IO ()
82+roundtrip val times = do
84+ case size :: Size a of
88+ replicateM_ times $ do
89+ _ <- serialize 0 arr val
90+ (_, val1) <- deserialize 0 arr
91+ assert (val == val1) (pure ())
93+benchSink :: NFData b => String -> Int -> (Int -> IO b) -> Benchmark
94+benchSink name times f = bench name (nfIO (randomRIO (times, times) >>= f))
96+-------------------------------------------------------------------------------
98+-------------------------------------------------------------------------------
100+allBenchmarks :: Int -> [Benchmark]
101+allBenchmarks times =
103+ [ benchSink "C1" times
104+ (pokeTimes (CDT1C1 :: CustomDT1))
105+ , benchSink "C2" times
106+ (pokeTimes ((CDT1C2 (5 :: Int)) :: CustomDT1))
107+ , benchSink "C3" times
108+ (pokeTimes ((CDT1C3 (5 :: Int) True) :: CustomDT1))
111+ [ benchSink "C1" times
112+ (peekTimes (CDT1C1 :: CustomDT1))
113+ , benchSink "C2" times
114+ (peekTimes ((CDT1C2 (5 :: Int)) :: CustomDT1))
115+ , benchSink "C3" times
116+ (peekTimes ((CDT1C3 (5 :: Int) True) :: CustomDT1))
118+ , bgroup "roundtrip"
119+ [ benchSink "C1" times
120+ (roundtrip (CDT1C1 :: CustomDT1))
121+ , benchSink "C2" times
122+ (roundtrip ((CDT1C2 (5 :: Int)) :: CustomDT1))
123+ , benchSink "C3" times
124+ (roundtrip ((CDT1C3 (5 :: Int) True) :: CustomDT1))
128+-------------------------------------------------------------------------------
130+-------------------------------------------------------------------------------
135+ runWithCLIOpts defaultStreamSize allBenchmarks
137+ -- Enable FUSION_CHECK macro at the beginning of the file
138+ -- Enable one benchmark below, and run the benchmark
139+ -- Check the .dump-simpl output
141+ -- peekTimes ((CDT1C2 (5 :: Int)) :: CustomDT1) value
142+ roundtrip ((CDT1C2 (5 :: Int)) :: CustomDT1) value