on 3336bb1 help sign in
112 lines 3.0 kB history raw
1-- |
2-- Module : Streamly.Benchmark.FileIO.Array
3-- Copyright : (c) 2019 Composewell Technologies
4--
5-- License : BSD3
6-- Maintainer : harendra.kumar@gmail.com
7-- Stability : experimental
8-- Portability : GHC
10{-# LANGUAGE TemplateHaskell #-}
12{-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-}
14module Streamly.Benchmark.FileIO.Array
15 (
16 last
17 , countBytes
18 , countLines
19 , sumBytes
20 , cat
21 , copy
22 , linesUnlinesCopy
23 )
24where
26import Data.Functor.Identity (runIdentity)
27import Data.Word (Word8)
28import System.IO (Handle)
29import Prelude hiding (last)
31import Streamly.Benchmark.Inspection (hinspect)
32import Streamly.Streams.StreamD.Type (Step(..))
34import qualified Streamly.FileSystem.Handle as FH
35import qualified Streamly.Memory.Array as A
36import qualified Streamly.Prelude as S
37import qualified Streamly.Internal as Internal
39import Test.Inspection
41-- | Get the last byte from a file bytestream.
42{-# INLINE last #-}
43last :: Handle -> IO (Maybe Word8)
44last inh = do
45 let s = FH.readArrays inh
46 larr <- S.last s
47 return $ case larr of
48 Nothing -> Nothing
49 Just arr -> A.readIndex arr (A.length arr - 1)
51hinspect $ hasNoTypeClasses 'last
52hinspect $ 'last `hasNoType` ''Step
54-- | Count the number of bytes in a file.
55{-# INLINE countBytes #-}
56countBytes :: Handle -> IO Int
57countBytes inh =
58 let s = FH.readArrays inh
59 in S.sum (S.map A.length s)
61hinspect $ hasNoTypeClasses 'countBytes
62hinspect $ 'countBytes `hasNoType` ''Step
64-- | Count the number of lines in a file.
65{-# INLINE countLines #-}
66countLines :: Handle -> IO Int
67countLines = S.length . A.splitOn 10 . FH.readArrays
69hinspect $ hasNoTypeClasses 'countLines
70hinspect $ 'countLines `hasNoType` ''Step
72-- | Sum the bytes in a file.
73{-# INLINE sumBytes #-}
74sumBytes :: Handle -> IO Word8
75sumBytes inh = do
76 let foldlArr' f z = runIdentity . S.foldl' f z . A.read
77 let s = FH.readArrays inh
78 S.foldl' (\acc arr -> acc + foldlArr' (+) 0 arr) 0 s
80hinspect $ hasNoTypeClasses 'sumBytes
81hinspect $ 'sumBytes `hasNoType` ''Step
83-- | Send the file contents to /dev/null
84{-# INLINE cat #-}
85cat :: Handle -> Handle -> IO ()
86cat devNull inh =
87 S.runFold (FH.writeArrays devNull) $ FH.readArraysOf (256*1024) inh
89hinspect $ hasNoTypeClasses 'cat
90hinspect $ 'cat `hasNoType` ''Step
92-- | Copy file
93{-# INLINE copy #-}
94copy :: Handle -> Handle -> IO ()
95copy inh outh =
96 let s = FH.readArrays inh
97 in S.runFold (FH.writeArrays outh) s
99hinspect $ hasNoTypeClasses 'copy
100hinspect $ 'copy `hasNoType` ''Step
102-- | Lines and unlines
103{-# INLINE linesUnlinesCopy #-}
104linesUnlinesCopy :: Handle -> Handle -> IO ()
105linesUnlinesCopy inh outh =
106 S.runFold (FH.writeArraysInChunksOf (1024*1024) outh)
107 $ Internal.insertAfterEach (return $ A.fromList [10])
108 $ A.splitOn 10
109 $ FH.readArraysOf (1024*1024) inh
111-- hinspect $ hasNoTypeClasses 'linesUnlinesCopy
112-- hinspect $ 'linesUnlinesCopy `hasNoType` ''Step