on eaa837d help sign in
217 lines 7.3 kB history raw
1-- |
2-- Module : Streamly.Benchmark.FileSystem.Handle
3-- Copyright : (c) 2019 Composewell Technologies
4-- License : BSD3-3-Clause
5-- Maintainer : streamly@composewell.com
6-- Stability : experimental
7-- Portability : GHC
9{-# LANGUAGE CPP #-}
10{-# LANGUAGE ScopedTypeVariables #-}
12#ifdef __HADDOCK_VERSION__
13#undef INSPECTION
14#endif
16#ifdef INSPECTION
17{-# LANGUAGE TemplateHaskell #-}
18{-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-}
19#endif
21module Handle.ReadWrite
22 (allBenchmarks)
23where
25import System.IO (Handle)
26import Prelude hiding (last, length)
27import Streamly.Internal.Data.Array.Foreign.Type (defaultChunkSize)
29import qualified Streamly.FileSystem.Handle as FH
30import qualified Streamly.Internal.Data.Unfold as IUF
31import qualified Streamly.Internal.FileSystem.Handle as IFH
32import qualified Streamly.Data.Array.Foreign as A
33import qualified Streamly.Prelude as S
35import Gauge hiding (env)
36import Streamly.Benchmark.Common.Handle
38#ifdef INSPECTION
39import Streamly.Internal.Data.Stream.StreamD.Type (Step(..))
41import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
42import qualified Streamly.Internal.Data.Tuple.Strict as Strict
43import qualified Streamly.Internal.Data.Array.Stream.Mut.Foreign as MAS
44import qualified Streamly.Internal.Data.Array.Foreign.Type as AT
45import qualified Streamly.Internal.Data.Array.Foreign.Mut.Type as MA
47import Test.Inspection
48#endif
50-------------------------------------------------------------------------------
51-- copy chunked
52-------------------------------------------------------------------------------
54-- | Copy file
55copyChunks :: Handle -> Handle -> IO ()
56copyChunks inh outh = S.fold (IFH.writeChunks outh) $ IFH.toChunks inh
58#ifdef INSPECTION
59inspect $ hasNoTypeClasses 'copyChunks
60inspect $ 'copyChunks `hasNoType` ''Step
61#endif
63o_1_space_copy_chunked :: BenchEnv -> [Benchmark]
64o_1_space_copy_chunked env =
65 [ bgroup "copy/toChunks"
66 [ mkBench "toNull" env $ \inH _ ->
67 copyChunks inH (nullH env)
68 , mkBench "raw" env $ \inH outH ->
69 copyChunks inH outH
70 ]
71 ]
73-------------------------------------------------------------------------------
74-- copy unfold
75-------------------------------------------------------------------------------
77-- | Copy file
78copyStream :: Handle -> Handle -> IO ()
79copyStream inh outh = S.fold (FH.write outh) (S.unfold FH.read inh)
81#ifdef INSPECTION
82inspect $ hasNoTypeClasses 'copyStream
83inspect $ 'copyStream `hasNoType` ''Step -- S.unfold
84inspect $ 'copyStream `hasNoType` ''IUF.ConcatState -- FH.read/UF.many
85inspect $ 'copyStream `hasNoType` ''MA.ReadUState -- FH.read/A.read
86inspect $ 'copyStream `hasNoType` ''AT.ArrayUnsafe -- FH.write/writeNUnsafe
87inspect $ 'copyStream `hasNoType` ''Strict.Tuple3' -- FH.write/chunksOf
88#endif
90o_1_space_copy_read :: BenchEnv -> [Benchmark]
91o_1_space_copy_read env =
92 [ bgroup "copy/read"
93 [ mkBench "rawToNull" env $ \inh _ ->
94 copyStream inh (nullH env)
95 , mkBench "rawToFile" env $ \inh outh ->
96 copyStream inh outh
97 ]
98 ]
100-------------------------------------------------------------------------------
101-- copy stream
102-------------------------------------------------------------------------------
104-- | Send the file contents to /dev/null
105readFromBytesNull :: Handle -> Handle -> IO ()
106readFromBytesNull inh devNull = IFH.putBytes devNull $ S.unfold FH.read inh
108#ifdef INSPECTION
109inspect $ hasNoTypeClasses 'readFromBytesNull
110inspect $ 'readFromBytesNull `hasNoType` ''Step
111inspect $ 'readFromBytesNull `hasNoType` ''MAS.SpliceState
112inspect $ 'readFromBytesNull `hasNoType` ''AT.ArrayUnsafe -- FH.fromBytes/S.arraysOf
113inspect $ 'readFromBytesNull `hasNoType` ''D.FoldMany
114#endif
116-- | Send the file contents ('defaultChunkSize') to /dev/null
117readWithBufferOfFromBytesNull :: Handle -> Handle -> IO ()
118readWithBufferOfFromBytesNull inh devNull =
119 IFH.putBytes devNull
120 $ S.unfold FH.readWithBufferOf (defaultChunkSize, inh)
122#ifdef INSPECTION
123inspect $ hasNoTypeClasses 'readWithBufferOfFromBytesNull
124inspect $ 'readWithBufferOfFromBytesNull `hasNoType` ''Step
125inspect $ 'readWithBufferOfFromBytesNull `hasNoType` ''MAS.SpliceState
126inspect $ 'readWithBufferOfFromBytesNull `hasNoType` ''AT.ArrayUnsafe -- FH.fromBytes/S.arraysOf
127inspect $ 'readWithBufferOfFromBytesNull `hasNoType` ''D.FoldMany
128#endif
130-- | Send the chunk content ('defaultChunkSize') to /dev/null
131-- Implicitly benchmarked via 'readFromBytesNull'
132_readChunks :: Handle -> Handle -> IO ()
133_readChunks inh devNull = IUF.fold fld unf inh
135 where
137 fld = FH.write devNull
138 unf = IUF.many FH.readChunks A.read
140-- | Send the chunk content to /dev/null
141-- Implicitly benchmarked via 'readWithBufferOfFromBytesNull'
142_readChunksWithBufferOf :: Handle -> Handle -> IO ()
143_readChunksWithBufferOf inh devNull = IUF.fold fld unf (defaultChunkSize, inh)
145 where
147 fld = FH.write devNull
148 unf = IUF.many FH.readChunksWithBufferOf A.read
151o_1_space_copy_fromBytes :: BenchEnv -> [Benchmark]
152o_1_space_copy_fromBytes env =
153 [ bgroup "copy/putBytes"
154 [ mkBench "rawToNull" env $ \inh _ ->
155 readFromBytesNull inh (nullH env)
156 , mkBench "FH.readWithBufferOf" env $ \inh _ ->
157 readWithBufferOfFromBytesNull inh (nullH env)
158 ]
159 ]
161-- | Send the file contents ('defaultChunkSize') to /dev/null
162{-# NOINLINE writeReadWithBufferOf #-}
163writeReadWithBufferOf :: Handle -> Handle -> IO ()
164writeReadWithBufferOf inh devNull = IUF.fold fld unf (defaultChunkSize, inh)
166 where
168 fld = FH.writeWithBufferOf defaultChunkSize devNull
169 unf = FH.readWithBufferOf
171#ifdef INSPECTION
172inspect $ hasNoTypeClasses 'writeReadWithBufferOf
173inspect $ 'writeReadWithBufferOf `hasNoType` ''Step
174inspect $ 'writeReadWithBufferOf `hasNoType` ''IUF.ConcatState -- FH.read/UF.many
175inspect $ 'writeReadWithBufferOf `hasNoType` ''MA.ReadUState -- FH.read/A.read
176inspect $ 'writeReadWithBufferOf `hasNoType` ''AT.ArrayUnsafe -- FH.write/writeNUnsafe
177#endif
179-- | Send the file contents ('AT.defaultChunkSize') to /dev/null
180{-# NOINLINE writeRead #-}
181writeRead :: Handle -> Handle -> IO ()
182writeRead inh devNull = IUF.fold fld unf inh
184 where
186 fld = FH.write devNull
187 unf = FH.read
189#ifdef INSPECTION
190inspect $ hasNoTypeClasses 'writeRead
191inspect $ 'writeRead `hasNoType` ''Step
192inspect $ 'writeRead `hasNoType` ''IUF.ConcatState -- FH.read/UF.many
193inspect $ 'writeRead `hasNoType` ''MA.ReadUState -- FH.read/A.read
194inspect $ 'writeRead `hasNoType` ''AT.ArrayUnsafe -- FH.write/writeNUnsafe
195#endif
197o_1_space_copy :: BenchEnv -> [Benchmark]
198o_1_space_copy env =
199 [ bgroup "copy"
200 [ mkBench "FH.write . FH.read" env $ \inh _ ->
201 writeRead inh (nullH env)
202 , mkBench "FH.writeWithBufferOf . FH.readWithBufferOf" env $ \inh _ ->
203 writeReadWithBufferOf inh (nullH env)
204 ]
205 ]
207-------------------------------------------------------------------------------
208--
209-------------------------------------------------------------------------------
211allBenchmarks :: BenchEnv -> [Benchmark]
212allBenchmarks env = Prelude.concat
213 [ o_1_space_copy_chunked env
214 , o_1_space_copy_read env
215 , o_1_space_copy_fromBytes env
216 , o_1_space_copy env
217 ]