on 5d096df help sign in
214 lines 6.9 kB history raw
1{-# OPTIONS_GHC -Wno-deprecations #-}
3-- |
4-- Module : Streamly.Benchmark.FileSystem.Handle
5-- Copyright : (c) 2019 Composewell Technologies
6-- License : BSD3-3-Clause
7-- Maintainer : streamly@composewell.com
8-- Stability : experimental
9-- Portability : GHC
11{-# LANGUAGE CPP #-}
12{-# LANGUAGE ScopedTypeVariables #-}
14#ifdef __HADDOCK_VERSION__
15#undef INSPECTION
16#endif
18#ifdef INSPECTION
19{-# LANGUAGE TemplateHaskell #-}
20{-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-}
21#endif
23module Handle.ReadWrite
24 (allBenchmarks)
25where
27import System.IO (Handle)
28import Prelude hiding (last, length)
29import Streamly.Internal.System.IO (defaultChunkSize)
31import qualified Streamly.FileSystem.Handle as FH
32import qualified Streamly.Internal.Data.Unfold as IUF
33import qualified Streamly.Internal.FileSystem.Handle as IFH
34import qualified Streamly.Data.Array as A
35import qualified Streamly.Data.Stream.Prelude as S
37import Test.Tasty.Bench hiding (env)
38import Streamly.Benchmark.Common.Handle
40#ifdef INSPECTION
41import Streamly.Internal.Data.Stream (Step(..))
43import qualified Streamly.Internal.Data.Stream as D
44import qualified Streamly.Internal.Data.Tuple.Strict as Strict
45import qualified Streamly.Internal.Data.MutArray as MutArray
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.readChunks 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/getChunks"
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.reader 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` ''MutArray.ArrayUnsafe -- FH.write/writeNUnsafe
86 -- FH.read/A.read
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.reader inh
108#ifdef INSPECTION
109inspect $ hasNoTypeClasses 'readFromBytesNull
110inspect $ 'readFromBytesNull `hasNoType` ''Step
111inspect $ 'readFromBytesNull `hasNoType` ''MutArray.SpliceState
112inspect $ 'readFromBytesNull `hasNoType` ''MutArray.ArrayUnsafe -- FH.fromBytes/S.chunksOf
113inspect $ 'readFromBytesNull `hasNoType` ''D.FoldMany
114#endif
116-- | Send the file contents ('defaultChunkSize') to /dev/null
117readWithFromBytesNull :: Handle -> Handle -> IO ()
118readWithFromBytesNull inh devNull =
119 IFH.putBytes devNull
120 $ S.unfold FH.readerWith (defaultChunkSize, inh)
122#ifdef INSPECTION
123inspect $ hasNoTypeClasses 'readWithFromBytesNull
124inspect $ 'readWithFromBytesNull `hasNoType` ''Step
125inspect $ 'readWithFromBytesNull `hasNoType` ''MutArray.SpliceState
126inspect $ 'readWithFromBytesNull `hasNoType` ''MutArray.ArrayUnsafe -- FH.fromBytes/S.chunksOf
127inspect $ 'readWithFromBytesNull `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 A.reader FH.chunkReader
140-- | Send the chunk content to /dev/null
141-- Implicitly benchmarked via 'readWithFromBytesNull'
142_readChunksWith :: Handle -> Handle -> IO ()
143_readChunksWith inh devNull = IUF.fold fld unf (defaultChunkSize, inh)
145 where
147 fld = FH.write devNull
148 unf = IUF.many A.reader FH.chunkReaderWith
150o_1_space_copy_fromBytes :: BenchEnv -> [Benchmark]
151o_1_space_copy_fromBytes env =
152 [ bgroup "copy/putBytes"
153 [ mkBench "rawToNull" env $ \inh _ ->
154 readFromBytesNull inh (nullH env)
155 , mkBench "FH.readWith" env $ \inh _ ->
156 readWithFromBytesNull inh (nullH env)
157 ]
158 ]
160-- | Send the file contents ('defaultChunkSize') to /dev/null
161writeReadWith :: Handle -> Handle -> IO ()
162writeReadWith inh devNull = IUF.fold fld unf (defaultChunkSize, inh)
164 where
166 fld = FH.writeWith defaultChunkSize devNull
167 unf = FH.readerWith
169#ifdef INSPECTION
170inspect $ hasNoTypeClasses 'writeReadWith
171inspect $ 'writeReadWith `hasNoType` ''Step
172inspect $ 'writeReadWith `hasNoType` ''IUF.ConcatState -- FH.read/UF.many
173inspect $ 'writeReadWith `hasNoType` ''MutArray.ArrayUnsafe -- FH.write/writeNUnsafe
174 -- FH.read/A.read
175#endif
177-- | Send the file contents ('AT.defaultChunkSize') to /dev/null
178writeRead :: Handle -> Handle -> IO ()
179writeRead inh devNull = IUF.fold fld unf inh
181 where
183 fld = FH.write devNull
184 unf = FH.reader
186#ifdef INSPECTION
187inspect $ hasNoTypeClasses 'writeRead
188inspect $ 'writeRead `hasNoType` ''Step
189inspect $ 'writeRead `hasNoType` ''IUF.ConcatState -- FH.read/UF.many
190inspect $ 'writeRead `hasNoType` ''MutArray.ArrayUnsafe -- FH.write/writeNUnsafe
191 -- FH.read/A.read
192#endif
194o_1_space_copy :: BenchEnv -> [Benchmark]
195o_1_space_copy env =
196 [ bgroup "copy"
197 [ mkBench "FH.write . FH.read" env $ \inh _ ->
198 writeRead inh (nullH env)
199 , mkBench "FH.writeWith . FH.readWith" env $ \inh _ ->
200 writeReadWith inh (nullH env)
201 ]
202 ]
204-------------------------------------------------------------------------------
205--
206-------------------------------------------------------------------------------
208allBenchmarks :: BenchEnv -> [Benchmark]
209allBenchmarks env = Prelude.concat
210 [ o_1_space_copy_chunked env
211 , o_1_space_copy_read env
212 , o_1_space_copy_fromBytes env
213 , o_1_space_copy env
214 ]