on f390e53 help sign in
213 lines 6.9 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.System.IO (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.Unboxed 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.Unboxed.Mut.Stream as MAS
44import qualified Streamly.Internal.Data.Array.Unboxed.Type as AT
46import Test.Inspection
47#endif
49-------------------------------------------------------------------------------
50-- copy chunked
51-------------------------------------------------------------------------------
53-- | Copy file
54copyChunks :: Handle -> Handle -> IO ()
55copyChunks inh outh = S.fold (IFH.writeChunks outh) $ IFH.getChunks inh
57#ifdef INSPECTION
58inspect $ hasNoTypeClasses 'copyChunks
59inspect $ 'copyChunks `hasNoType` ''Step
60#endif
62o_1_space_copy_chunked :: BenchEnv -> [Benchmark]
63o_1_space_copy_chunked env =
64 [ bgroup "copy/getChunks"
65 [ mkBench "toNull" env $ \inH _ ->
66 copyChunks inH (nullH env)
67 , mkBench "raw" env $ \inH outH ->
68 copyChunks inH outH
69 ]
70 ]
72-------------------------------------------------------------------------------
73-- copy unfold
74-------------------------------------------------------------------------------
76-- | Copy file
77copyStream :: Handle -> Handle -> IO ()
78copyStream inh outh = S.fold (FH.write outh) (S.unfold FH.read inh)
80#ifdef INSPECTION
81inspect $ hasNoTypeClasses 'copyStream
82inspect $ 'copyStream `hasNoType` ''Step -- S.unfold
83inspect $ 'copyStream `hasNoType` ''IUF.ConcatState -- FH.read/UF.many
84inspect $ 'copyStream `hasNoType` ''AT.ArrayUnsafe -- FH.write/writeNUnsafe
85 -- FH.read/A.read
86inspect $ 'copyStream `hasNoType` ''Strict.Tuple3' -- FH.write/chunksOf
87#endif
89o_1_space_copy_read :: BenchEnv -> [Benchmark]
90o_1_space_copy_read env =
91 [ bgroup "copy/read"
92 [ mkBench "rawToNull" env $ \inh _ ->
93 copyStream inh (nullH env)
94 , mkBench "rawToFile" env $ \inh outh ->
95 copyStream inh outh
96 ]
97 ]
99-------------------------------------------------------------------------------
100-- copy stream
101-------------------------------------------------------------------------------
103-- | Send the file contents to /dev/null
104readFromBytesNull :: Handle -> Handle -> IO ()
105readFromBytesNull inh devNull = IFH.putBytes devNull $ S.unfold FH.read inh
107#ifdef INSPECTION
108inspect $ hasNoTypeClasses 'readFromBytesNull
109inspect $ 'readFromBytesNull `hasNoType` ''Step
110inspect $ 'readFromBytesNull `hasNoType` ''MAS.SpliceState
111inspect $ 'readFromBytesNull `hasNoType` ''AT.ArrayUnsafe -- FH.fromBytes/S.arraysOf
112inspect $ 'readFromBytesNull `hasNoType` ''D.FoldMany
113#endif
115-- | Send the file contents ('defaultChunkSize') to /dev/null
116readWithFromBytesNull :: Handle -> Handle -> IO ()
117readWithFromBytesNull inh devNull =
118 IFH.putBytes devNull
119 $ S.unfold FH.readWith (defaultChunkSize, inh)
121#ifdef INSPECTION
122inspect $ hasNoTypeClasses 'readWithFromBytesNull
123inspect $ 'readWithFromBytesNull `hasNoType` ''Step
124inspect $ 'readWithFromBytesNull `hasNoType` ''MAS.SpliceState
125inspect $ 'readWithFromBytesNull `hasNoType` ''AT.ArrayUnsafe -- FH.fromBytes/S.arraysOf
126inspect $ 'readWithFromBytesNull `hasNoType` ''D.FoldMany
127#endif
129-- | Send the chunk content ('defaultChunkSize') to /dev/null
130-- Implicitly benchmarked via 'readFromBytesNull'
131_readChunks :: Handle -> Handle -> IO ()
132_readChunks inh devNull = IUF.fold fld unf inh
134 where
136 fld = FH.write devNull
137 unf = IUF.many A.read FH.readChunks
139-- | Send the chunk content to /dev/null
140-- Implicitly benchmarked via 'readWithFromBytesNull'
141_readChunksWith :: Handle -> Handle -> IO ()
142_readChunksWith inh devNull = IUF.fold fld unf (defaultChunkSize, inh)
144 where
146 fld = FH.write devNull
147 unf = IUF.many A.read FH.readChunksWith
149o_1_space_copy_fromBytes :: BenchEnv -> [Benchmark]
150o_1_space_copy_fromBytes env =
151 [ bgroup "copy/putBytes"
152 [ mkBench "rawToNull" env $ \inh _ ->
153 readFromBytesNull inh (nullH env)
154 , mkBench "FH.readWith" env $ \inh _ ->
155 readWithFromBytesNull inh (nullH env)
156 ]
157 ]
159-- | Send the file contents ('defaultChunkSize') to /dev/null
160writeReadWith :: Handle -> Handle -> IO ()
161writeReadWith inh devNull = IUF.fold fld unf (defaultChunkSize, inh)
163 where
165 fld = FH.writeWith defaultChunkSize devNull
166 unf = FH.readWith
168#ifdef INSPECTION
169inspect $ hasNoTypeClasses 'writeReadWith
170inspect $ 'writeReadWith `hasNoType` ''Step
171inspect $ 'writeReadWith `hasNoType` ''IUF.ConcatState -- FH.read/UF.many
172inspect $ 'writeReadWith `hasNoType` ''AT.ArrayUnsafe -- FH.write/writeNUnsafe
173 -- FH.read/A.read
174#endif
176-- | Send the file contents ('AT.defaultChunkSize') to /dev/null
177writeRead :: Handle -> Handle -> IO ()
178writeRead inh devNull = IUF.fold fld unf inh
180 where
182 fld = FH.write devNull
183 unf = FH.read
185#ifdef INSPECTION
186inspect $ hasNoTypeClasses 'writeRead
187inspect $ 'writeRead `hasNoType` ''Step
188inspect $ 'writeRead `hasNoType` ''IUF.ConcatState -- FH.read/UF.many
189inspect $ 'writeRead `hasNoType` ''AT.ArrayUnsafe -- FH.write/writeNUnsafe
190 -- FH.read/A.read
191#endif
193o_1_space_copy :: BenchEnv -> [Benchmark]
194o_1_space_copy env =
195 [ bgroup "copy"
196 [ mkBench "FH.write . FH.read" env $ \inh _ ->
197 writeRead inh (nullH env)
198 , mkBench "FH.writeWith . FH.readWith" env $ \inh _ ->
199 writeReadWith inh (nullH env)
200 ]
201 ]
203-------------------------------------------------------------------------------
204--
205-------------------------------------------------------------------------------
207allBenchmarks :: BenchEnv -> [Benchmark]
208allBenchmarks env = Prelude.concat
209 [ o_1_space_copy_chunked env
210 , o_1_space_copy_read env
211 , o_1_space_copy_fromBytes env
212 , o_1_space_copy env
213 ]