on 7cb856b help sign in
215 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.Stream as MAS
46import qualified Streamly.Internal.Data.Array as AT
48import Test.Inspection
49#endif
51-------------------------------------------------------------------------------
52-- copy chunked
53-------------------------------------------------------------------------------
55-- | Copy file
56copyChunks :: Handle -> Handle -> IO ()
57copyChunks inh outh = S.fold (IFH.writeChunks outh) $ IFH.readChunks inh
59#ifdef INSPECTION
60inspect $ hasNoTypeClasses 'copyChunks
61inspect $ 'copyChunks `hasNoType` ''Step
62#endif
64o_1_space_copy_chunked :: BenchEnv -> [Benchmark]
65o_1_space_copy_chunked env =
66 [ bgroup "copy/getChunks"
67 [ mkBench "toNull" env $ \inH _ ->
68 copyChunks inH (nullH env)
69 , mkBench "raw" env $ \inH outH ->
70 copyChunks inH outH
71 ]
72 ]
74-------------------------------------------------------------------------------
75-- copy unfold
76-------------------------------------------------------------------------------
78-- | Copy file
79copyStream :: Handle -> Handle -> IO ()
80copyStream inh outh = S.fold (FH.write outh) (S.unfold FH.reader inh)
82#ifdef INSPECTION
83inspect $ hasNoTypeClasses 'copyStream
84inspect $ 'copyStream `hasNoType` ''Step -- S.unfold
85inspect $ 'copyStream `hasNoType` ''IUF.ConcatState -- FH.read/UF.many
86inspect $ 'copyStream `hasNoType` ''AT.ArrayUnsafe -- FH.write/writeNUnsafe
87 -- FH.read/A.read
88inspect $ 'copyStream `hasNoType` ''Strict.Tuple3' -- FH.write/chunksOf
89#endif
91o_1_space_copy_read :: BenchEnv -> [Benchmark]
92o_1_space_copy_read env =
93 [ bgroup "copy/read"
94 [ mkBench "rawToNull" env $ \inh _ ->
95 copyStream inh (nullH env)
96 , mkBench "rawToFile" env $ \inh outh ->
97 copyStream inh outh
98 ]
99 ]
101-------------------------------------------------------------------------------
102-- copy stream
103-------------------------------------------------------------------------------
105-- | Send the file contents to /dev/null
106readFromBytesNull :: Handle -> Handle -> IO ()
107readFromBytesNull inh devNull = IFH.putBytes devNull $ S.unfold FH.reader inh
109#ifdef INSPECTION
110inspect $ hasNoTypeClasses 'readFromBytesNull
111inspect $ 'readFromBytesNull `hasNoType` ''Step
112inspect $ 'readFromBytesNull `hasNoType` ''MAS.SpliceState
113inspect $ 'readFromBytesNull `hasNoType` ''AT.ArrayUnsafe -- FH.fromBytes/S.chunksOf
114inspect $ 'readFromBytesNull `hasNoType` ''D.FoldMany
115#endif
117-- | Send the file contents ('defaultChunkSize') to /dev/null
118readWithFromBytesNull :: Handle -> Handle -> IO ()
119readWithFromBytesNull inh devNull =
120 IFH.putBytes devNull
121 $ S.unfold FH.readerWith (defaultChunkSize, inh)
123#ifdef INSPECTION
124inspect $ hasNoTypeClasses 'readWithFromBytesNull
125inspect $ 'readWithFromBytesNull `hasNoType` ''Step
126inspect $ 'readWithFromBytesNull `hasNoType` ''MAS.SpliceState
127inspect $ 'readWithFromBytesNull `hasNoType` ''AT.ArrayUnsafe -- FH.fromBytes/S.chunksOf
128inspect $ 'readWithFromBytesNull `hasNoType` ''D.FoldMany
129#endif
131-- | Send the chunk content ('defaultChunkSize') to /dev/null
132-- Implicitly benchmarked via 'readFromBytesNull'
133_readChunks :: Handle -> Handle -> IO ()
134_readChunks inh devNull = IUF.fold fld unf inh
136 where
138 fld = FH.write devNull
139 unf = IUF.many A.reader FH.chunkReader
141-- | Send the chunk content to /dev/null
142-- Implicitly benchmarked via 'readWithFromBytesNull'
143_readChunksWith :: Handle -> Handle -> IO ()
144_readChunksWith inh devNull = IUF.fold fld unf (defaultChunkSize, inh)
146 where
148 fld = FH.write devNull
149 unf = IUF.many A.reader FH.chunkReaderWith
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.readWith" env $ \inh _ ->
157 readWithFromBytesNull inh (nullH env)
158 ]
159 ]
161-- | Send the file contents ('defaultChunkSize') to /dev/null
162writeReadWith :: Handle -> Handle -> IO ()
163writeReadWith inh devNull = IUF.fold fld unf (defaultChunkSize, inh)
165 where
167 fld = FH.writeWith defaultChunkSize devNull
168 unf = FH.readerWith
170#ifdef INSPECTION
171inspect $ hasNoTypeClasses 'writeReadWith
172inspect $ 'writeReadWith `hasNoType` ''Step
173inspect $ 'writeReadWith `hasNoType` ''IUF.ConcatState -- FH.read/UF.many
174inspect $ 'writeReadWith `hasNoType` ''AT.ArrayUnsafe -- FH.write/writeNUnsafe
175 -- FH.read/A.read
176#endif
178-- | Send the file contents ('AT.defaultChunkSize') to /dev/null
179writeRead :: Handle -> Handle -> IO ()
180writeRead inh devNull = IUF.fold fld unf inh
182 where
184 fld = FH.write devNull
185 unf = FH.reader
187#ifdef INSPECTION
188inspect $ hasNoTypeClasses 'writeRead
189inspect $ 'writeRead `hasNoType` ''Step
190inspect $ 'writeRead `hasNoType` ''IUF.ConcatState -- FH.read/UF.many
191inspect $ 'writeRead `hasNoType` ''AT.ArrayUnsafe -- FH.write/writeNUnsafe
192 -- FH.read/A.read
193#endif
195o_1_space_copy :: BenchEnv -> [Benchmark]
196o_1_space_copy env =
197 [ bgroup "copy"
198 [ mkBench "FH.write . FH.read" env $ \inh _ ->
199 writeRead inh (nullH env)
200 , mkBench "FH.writeWith . FH.readWith" env $ \inh _ ->
201 writeReadWith inh (nullH env)
202 ]
203 ]
205-------------------------------------------------------------------------------
206--
207-------------------------------------------------------------------------------
209allBenchmarks :: BenchEnv -> [Benchmark]
210allBenchmarks env = Prelude.concat
211 [ o_1_space_copy_chunked env
212 , o_1_space_copy_read env
213 , o_1_space_copy_fromBytes env
214 , o_1_space_copy env
215 ]