on master help sign in
Add benchmarks for exception handling operations
1305145Harendra Kumar
benchmark/FileIO.hs+26
@@ -88,6 +88,15 @@ main = do
88 , mkBench "cat" href $ do
88 , mkBench "cat" href $ do
89 Handles inh _ <- readIORef href
89 Handles inh _ <- readIORef href
90 BFA.cat devNull inh
90 BFA.cat devNull inh
91+ , mkBench "catBracket" href $ do
92+ Handles inh _ <- readIORef href
93+ BFA.catBracket devNull inh
94+ , mkBench "catBracketStream" href $ do
95+ Handles inh _ <- readIORef href
96+ BFA.catBracketStream devNull inh
97+ , mkBench "catOnException" href $ do
98+ Handles inh _ <- readIORef href
99+ BFA.catOnException devNull inh
91 , mkBench "read-utf8" href $ do
100 , mkBench "read-utf8" href $ do
92 Handles inh _ <- readIORef href
101 Handles inh _ <- readIORef href
93 BFA.decodeUtf8Lenient inh
102 BFA.decodeUtf8Lenient inh
@@ -117,6 +126,23 @@ main = do
117 , mkBench "catStream" href $ do
126 , mkBench "catStream" href $ do
118 Handles inh _ <- readIORef href
127 Handles inh _ <- readIORef href
119 BFS.catStreamWrite devNull inh
128 BFS.catStreamWrite devNull inh
129+#ifdef DEVBUILD
130+ , mkBench "catOnException" href $ do
131+ Handles inh _ <- readIORef href
132+ BFS.catOnException devNull inh
133+ , mkBench "catHandle" href $ do
134+ Handles inh _ <- readIORef href
135+ BFS.catHandle devNull inh
136+ , mkBench "catFinally" href $ do
137+ Handles inh _ <- readIORef href
138+ BFS.catFinally devNull inh
139+ , mkBench "catBracketStream" href $ do
140+ Handles inh _ <- readIORef href
141+ BFS.catBracketStream devNull inh
142+ , mkBench "catBracket" href $ do
143+ Handles inh _ <- readIORef href
144+ BFS.catBracket devNull inh
145+#endif
120 , mkBench "read-word8" href $ do
146 , mkBench "read-word8" href $ do
121 Handles inh _ <- readIORef href
147 Handles inh _ <- readIORef href
122 BFS.readWord8 inh
148 BFS.readWord8 inh
src/Streamly/Benchmark/FileIO/Array.hs+44−1
@@ -26,6 +26,9 @@ module Streamly.Benchmark.FileIO.Array
26 , countWords
26 , countWords
27 , sumBytes
27 , sumBytes
28 , cat
28 , cat
29+ , catOnException
30+ , catBracket
31+ , catBracketStream
29 , copy
32 , copy
30 , linesUnlinesCopy
33 , linesUnlinesCopy
31 , wordsUnwordsCopy
34 , wordsUnwordsCopy
@@ -36,7 +39,7 @@ where
36
39
37import Data.Functor.Identity (runIdentity)
40import Data.Functor.Identity (runIdentity)
38import Data.Word (Word8)
41import Data.Word (Word8)
39−import System.IO (Handle)
42+import System.IO (Handle, hClose)
40import Prelude hiding (last)
43import Prelude hiding (last)
41
44
42import qualified Streamly.FileSystem.Handle as FH
45import qualified Streamly.FileSystem.Handle as FH
@@ -48,6 +51,7 @@ import qualified Streamly.Internal.FileSystem.Handle as IFH
48import qualified Streamly.Internal.Prelude as Internal
51import qualified Streamly.Internal.Prelude as Internal
49import qualified Streamly.Internal.Memory.Array as IA
52import qualified Streamly.Internal.Memory.Array as IA
50import qualified Streamly.Internal.Memory.ArrayStream as AS
53import qualified Streamly.Internal.Memory.ArrayStream as AS
54+import qualified Streamly.Internal.Data.Unfold as IUF
51
55
52#ifdef INSPECTION
56#ifdef INSPECTION
53import Foreign.Storable (Storable)
57import Foreign.Storable (Storable)
@@ -127,6 +131,45 @@ inspect $ hasNoTypeClasses 'cat
127inspect $ 'cat `hasNoType` ''Step
131inspect $ 'cat `hasNoType` ''Step
128#endif
132#endif
129
133
134+-- | Send the file contents to /dev/null with exception handling
135+{-# INLINE catBracket #-}
136+catBracket :: Handle -> Handle -> IO ()
137+catBracket devNull inh =
138+ let readEx = IUF.bracket (return ()) (\_ -> hClose inh)
139+ (\_ -> IUF.first FH.readArraysOf (256*1024))
140+ in IUF.fold readEx (IFH.writeArrays devNull) inh
141+
142+#ifdef INSPECTION
143+inspect $ hasNoTypeClasses 'catBracket
144+-- inspect $ 'catBracket `hasNoType` ''Step
145+#endif
146+
147+-- | Send the file contents to /dev/null with exception handling
148+{-# INLINE catBracketStream #-}
149+catBracketStream :: Handle -> Handle -> IO ()
150+catBracketStream devNull inh =
151+ let readEx = S.bracket (return ()) (\_ -> hClose inh)
152+ (\_ -> IFH.toStreamArraysOf (256*1024) inh)
153+ in S.fold (IFH.writeArrays devNull) $ readEx
154+
155+#ifdef INSPECTION
156+inspect $ hasNoTypeClasses 'catBracketStream
157+-- inspect $ 'catBracketStream `hasNoType` ''Step
158+#endif
159+
160+-- | Send the file contents to /dev/null with exception handling
161+{-# INLINE catOnException #-}
162+catOnException :: Handle -> Handle -> IO ()
163+catOnException devNull inh =
164+ let readEx = IUF.onException (hClose inh)
165+ (IUF.first FH.readArraysOf (256*1024))
166+ in IUF.fold readEx (IFH.writeArrays devNull) inh
167+
168+#ifdef INSPECTION
169+inspect $ hasNoTypeClasses 'catOnException
170+-- inspect $ 'catOnException `hasNoType` ''Step
171+#endif
172+
130-- | Copy file
173-- | Copy file
131{-# INLINE copy #-}
174{-# INLINE copy #-}
132copy :: Handle -> Handle -> IO ()
175copy :: Handle -> Handle -> IO ()
src/Streamly/Benchmark/FileIO/Stream.hs+78−1
@@ -8,6 +8,7 @@
8-- Portability : GHC
8-- Portability : GHC
9
9
10{-# LANGUAGE CPP #-}
10{-# LANGUAGE CPP #-}
11+{-# LANGUAGE ScopedTypeVariables #-}
11
12
12#ifdef __HADDOCK_VERSION__
13#ifdef __HADDOCK_VERSION__
13#undef INSPECTION
14#undef INSPECTION
@@ -29,6 +30,11 @@ module Streamly.Benchmark.FileIO.Stream
29 , sumBytes
30 , sumBytes
30 , cat
31 , cat
31 , catStreamWrite
32 , catStreamWrite
33+ , catBracket
34+ , catBracketStream
35+ , catOnException
36+ , catHandle
37+ , catFinally
32 , copy
38 , copy
33 , linesUnlinesCopy
39 , linesUnlinesCopy
34 , wordsUnwordsCopyWord8
40 , wordsUnwordsCopyWord8
@@ -50,9 +56,10 @@ module Streamly.Benchmark.FileIO.Stream
50 )
56 )
51where
57where
52
58
59+import Control.Exception (SomeException)
53import Data.Char (ord, chr)
60import Data.Char (ord, chr)
54import Data.Word (Word8)
61import Data.Word (Word8)
55−import System.IO (Handle)
62+import System.IO (Handle, hClose)
56import Prelude hiding (last, length)
63import Prelude hiding (last, length)
57
64
58import qualified Streamly.FileSystem.Handle as FH
65import qualified Streamly.FileSystem.Handle as FH
@@ -179,6 +186,76 @@ inspect $ 'catStreamWrite `hasNoType` ''AT.FlattenState
179inspect $ 'catStreamWrite `hasNoType` ''D.ConcatMapUState
186inspect $ 'catStreamWrite `hasNoType` ''D.ConcatMapUState
180#endif
187#endif
181
188
189+-- | Send the file contents to /dev/null with exception handling
190+{-# INLINE catBracket #-}
191+catBracket :: Handle -> Handle -> IO ()
192+catBracket devNull inh =
193+ let readEx = IUF.bracket (return ()) (\_ -> hClose inh) (\_ -> FH.read)
194+ in S.fold (FH.write devNull) $ S.unfold readEx inh
195+
196+#ifdef INSPECTION
197+inspect $ hasNoTypeClasses 'catBracket
198+-- inspect $ 'catBracket `hasNoType` ''Step
199+-- inspect $ 'catBracket `hasNoType` ''AT.FlattenState
200+-- inspect $ 'catBracket `hasNoType` ''D.ConcatMapUState
201+#endif
202+
203+-- | Send the file contents to /dev/null with exception handling
204+{-# INLINE catBracketStream #-}
205+catBracketStream :: Handle -> Handle -> IO ()
206+catBracketStream devNull inh =
207+ let readEx = S.bracket (return ()) (\_ -> hClose inh)
208+ (\_ -> IFH.toStream inh)
209+ in IFH.fromStream devNull $ readEx
210+
211+#ifdef INSPECTION
212+inspect $ hasNoTypeClasses 'catBracketStream
213+-- inspect $ 'catBracketStream `hasNoType` ''Step
214+#endif
215+
216+-- | Send the file contents to /dev/null with exception handling
217+{-# INLINE catOnException #-}
218+catOnException :: Handle -> Handle -> IO ()
219+catOnException devNull inh =
220+ let readEx = IUF.onException (hClose inh) FH.read
221+ in S.fold (FH.write devNull) $ S.unfold readEx inh
222+
223+#ifdef INSPECTION
224+inspect $ hasNoTypeClasses 'catOnException
225+-- inspect $ 'catOnException `hasNoType` ''Step
226+-- inspect $ 'catOnException `hasNoType` ''AT.FlattenState
227+-- inspect $ 'catOnException `hasNoType` ''D.ConcatMapUState
228+#endif
229+
230+-- | Send the file contents to /dev/null with exception handling
231+{-# INLINE catFinally #-}
232+catFinally :: Handle -> Handle -> IO ()
233+catFinally devNull inh =
234+ let readEx = IUF.finally (hClose inh) FH.read
235+ in S.fold (FH.write devNull) $ S.unfold readEx inh
236+
237+#ifdef INSPECTION
238+inspect $ hasNoTypeClasses 'catFinally
239+-- inspect $ 'catFinally `hasNoType` ''Step
240+-- inspect $ 'catFinally `hasNoType` ''AT.FlattenState
241+-- inspect $ 'catFinally `hasNoType` ''D.ConcatMapUState
242+#endif
243+
244+-- | Send the file contents to /dev/null with exception handling
245+{-# INLINE catHandle #-}
246+catHandle :: Handle -> Handle -> IO ()
247+catHandle devNull inh =
248+ let readEx = IUF.handle (\(_e :: SomeException) -> hClose inh >> return 10)
249+ FH.read
250+ in S.fold (FH.write devNull) $ S.unfold readEx inh
251+
252+#ifdef INSPECTION
253+inspect $ hasNoTypeClasses 'catHandle
254+-- inspect $ 'catHandle `hasNoType` ''Step
255+-- inspect $ 'catHandle `hasNoType` ''AT.FlattenState
256+-- inspect $ 'catHandle `hasNoType` ''D.ConcatMapUState
257+#endif
258+
182-- | Copy file
259-- | Copy file
183{-# INLINE copy #-}
260{-# INLINE copy #-}
184copy :: Handle -> Handle -> IO ()
261copy :: Handle -> Handle -> IO ()