|
| 1 | +-- | |
| 2 | +-- Module : BenchTestLib.DirIO |
| 3 | +-- Copyright : (c) 2019 Composewell Technologies |
| 4 | +-- License : BSD-3-Clause |
| 5 | +-- Maintainer : streamly@composewell.com |
| 6 | +-- Stability : experimental |
| 7 | +-- Portability : GHC |
| 8 | + |
| 9 | +module BenchTestLib.DirIO |
| 10 | + ( createDirStucture |
| 11 | + , listDirUnfoldDfs |
| 12 | + , listDirUnfoldBfs |
| 13 | + , listDirUnfoldBfsRev |
| 14 | + , listDirConcatDfs |
| 15 | + , listDirConcatBfs |
| 16 | + , listDirConcatBfsRev |
| 17 | + , listDirAppend |
| 18 | + , listDirInterleave |
| 19 | + , listDirPar |
| 20 | + , listDirParInterleaved |
| 21 | + , listDirParOrdered |
| 22 | + , listDirChunkDfs |
| 23 | + , listDirChunkBfs |
| 24 | + , listDirChunkBfsRev |
| 25 | + , listDirChunkAppend |
| 26 | + , listDirChunkInterleave |
| 27 | + , listDirChunkPar |
| 28 | + , listDirChunkParInterleaved |
| 29 | + , listDirChunkParOrdered |
| 30 | + , listDirByteChunked |
| 31 | + ) where |
| 32 | + |
| 33 | +-------------------------------------------------------------------------------- |
| 34 | +-- Imports |
| 35 | +-------------------------------------------------------------------------------- |
| 36 | + |
| 37 | +import Data.Maybe (fromJust) |
| 38 | +import Data.Word (Word8) |
| 39 | +import Streamly.Data.Array (Array) |
| 40 | +import Streamly.Data.Stream (Stream) |
| 41 | +import Streamly.Data.Unfold (Unfold) |
| 42 | +import Streamly.FileSystem.Path (Path) |
| 43 | +import System.Process (callCommand) |
| 44 | + |
| 45 | +import qualified Streamly.Data.Stream.Prelude as Stream |
| 46 | +import qualified Streamly.Data.Array as Array |
| 47 | +import qualified Streamly.Internal.Data.Stream as Stream |
| 48 | +import qualified Streamly.Data.StreamK as StreamK |
| 49 | +import qualified Streamly.Internal.Data.StreamK as StreamK |
| 50 | +import qualified Streamly.Data.Unfold as Unfold |
| 51 | +import qualified Streamly.Internal.Data.Unfold as Unfold |
| 52 | +import qualified Streamly.Internal.FileSystem.DirIO as Dir |
| 53 | +import qualified Streamly.FileSystem.Path as Path |
| 54 | +import qualified Streamly.Internal.FileSystem.Path as Path (toChunk) |
| 55 | +#if !defined(mingw32_HOST_OS) && !defined(__MINGW32__) |
| 56 | +import qualified Streamly.Internal.FileSystem.Posix.ReadDir as Dir |
| 57 | +#endif |
| 58 | + |
| 59 | +-------------------------------------------------------------------------------- |
| 60 | +-- Helpers |
| 61 | +-------------------------------------------------------------------------------- |
| 62 | + |
| 63 | +concatIterateWith :: Monad m => |
| 64 | + (a -> Stream m a) |
| 65 | + -> (StreamK.StreamK m a |
| 66 | + -> StreamK.StreamK m a -> StreamK.StreamK m a) |
| 67 | + -> Stream m a |
| 68 | + -> Stream m a |
| 69 | +concatIterateWith nxt f = |
| 70 | + StreamK.toStream |
| 71 | + . StreamK.concatIterateWith f (StreamK.fromStream . nxt) |
| 72 | + . StreamK.fromStream |
| 73 | + |
| 74 | +mergeIterateWith :: Monad m => |
| 75 | + (a -> Stream m a) |
| 76 | + -> (StreamK.StreamK m a |
| 77 | + -> StreamK.StreamK m a -> StreamK.StreamK m a) |
| 78 | + -> Stream m a |
| 79 | + -> Stream m a |
| 80 | +mergeIterateWith nxt f = |
| 81 | + StreamK.toStream |
| 82 | + . StreamK.mergeIterateWith f (StreamK.fromStream . nxt) |
| 83 | + . StreamK.fromStream |
| 84 | + |
| 85 | +streamDir :: Either Path b -> Stream IO (Either Path Path) |
| 86 | +streamDir = either Dir.readEitherPaths (const Stream.nil) |
| 87 | + |
| 88 | +unfoldDir :: Unfold IO (Either Path b) (Either Path Path) |
| 89 | +unfoldDir = Unfold.either Dir.eitherReaderPaths Unfold.nil |
| 90 | + |
| 91 | +streamDirMaybe :: Either Path b -> Maybe (Stream IO (Either Path Path)) |
| 92 | +streamDirMaybe = either (Just . Dir.readEitherPaths) (const Nothing) |
| 93 | + |
| 94 | +_streamDirByteChunked |
| 95 | + :: Either [Path] b -> Stream IO (Either [Path] (Array Word8)) |
| 96 | +_streamDirByteChunked = either Dir.readEitherByteChunks (const Stream.nil) |
| 97 | + |
| 98 | +streamDirByteChunkedMaybe |
| 99 | + :: Either [Path] b -> Maybe (Stream IO (Either [Path] (Array Word8))) |
| 100 | +streamDirByteChunkedMaybe = |
| 101 | + either (Just . Dir.readEitherByteChunks) (const Nothing) |
| 102 | + |
| 103 | +streamDirChunkedMaybe :: Either [Path] b -> Maybe (Stream IO (Either [Path] [Path])) |
| 104 | +streamDirChunkedMaybe = either (Just . Dir.readEitherChunks) (const Nothing) |
| 105 | + |
| 106 | +streamDirChunked :: Either [Path] b -> Stream IO (Either [Path] [Path]) |
| 107 | +streamDirChunked = either Dir.readEitherChunks (const Stream.nil) |
| 108 | + |
| 109 | +-------------------------------------------------------------------------------- |
| 110 | +-- Functions |
| 111 | +-------------------------------------------------------------------------------- |
| 112 | + |
| 113 | +createDirStucture :: FilePath -> IO () |
| 114 | +createDirStucture dirRoot = do |
| 115 | + let cmd = |
| 116 | + "bench-test-lib/create_dir_structure.sh " ++ dirRoot ++ " 5 5" |
| 117 | + callCommand ("mkdir -p " ++ dirRoot) |
| 118 | + callCommand cmd |
| 119 | + |
| 120 | +#if !defined(mingw32_HOST_OS) && !defined(__MINGW32__) |
| 121 | +-- Fastest implementation, only works for posix as of now. |
| 122 | +listDirByteChunked :: FilePath -> Stream IO (Array Word8) |
| 123 | +listDirByteChunked inp = do |
| 124 | + Stream.catRights |
| 125 | + $ Stream.concatIterateDfs streamDirByteChunkedMaybe |
| 126 | + $ Stream.fromPure (Left [fromJust $ Path.fromString inp]) |
| 127 | +#endif |
| 128 | + |
| 129 | +-- Faster than the listDir implementation below |
| 130 | +listDirChunkedWith |
| 131 | + :: (Stream IO (Either [Path] b) -> Stream IO (Either [Path] [Path])) |
| 132 | + -> [Char] -> Stream IO Word8 |
| 133 | +listDirChunkedWith act inp = do |
| 134 | + Stream.unfoldEachEndBy 10 Array.reader |
| 135 | + $ fmap Path.toChunk |
| 136 | + $ Stream.unfoldEach Unfold.fromList |
| 137 | + $ fmap (either id id) |
| 138 | + $ act |
| 139 | + $ Stream.fromPure (Left [fromJust $ Path.fromString inp]) |
| 140 | + |
| 141 | +listDirWith |
| 142 | + :: (Stream IO (Either Path Path) -> Stream IO (Either Path Path)) |
| 143 | + -> [Char] -> Stream IO Word8 |
| 144 | +listDirWith act inp = do |
| 145 | + Stream.unfoldEachEndBy 10 Array.reader |
| 146 | + $ fmap (Path.toChunk . either id id) |
| 147 | + $ act |
| 148 | + $ Stream.fromPure (Left (fromJust $ Path.fromString inp)) |
| 149 | + |
| 150 | +#define DEF_LIST_DIR(x,y); \ |
| 151 | +x :: [Char] -> Stream IO Word8;\ |
| 152 | +x = listDirWith (y) |
| 153 | + |
| 154 | +DEF_LIST_DIR(listDirUnfoldDfs, Stream.unfoldIterateDfs unfoldDir) |
| 155 | +DEF_LIST_DIR(listDirUnfoldBfs, Stream.unfoldIterateBfs unfoldDir) |
| 156 | +DEF_LIST_DIR(listDirUnfoldBfsRev, Stream.unfoldIterateBfsRev unfoldDir) |
| 157 | +DEF_LIST_DIR(listDirConcatDfs, Stream.concatIterateDfs streamDirMaybe) |
| 158 | +DEF_LIST_DIR(listDirConcatBfs, Stream.concatIterateBfs streamDirMaybe) |
| 159 | +DEF_LIST_DIR(listDirConcatBfsRev, Stream.concatIterateBfsRev streamDirMaybe) |
| 160 | +DEF_LIST_DIR(listDirAppend, concatIterateWith streamDir StreamK.append) |
| 161 | +DEF_LIST_DIR(listDirInterleave, mergeIterateWith streamDir StreamK.interleave) |
| 162 | +DEF_LIST_DIR(listDirPar, Stream.parConcatIterate id streamDir) |
| 163 | +DEF_LIST_DIR(listDirParInterleaved, Stream.parConcatIterate (Stream.interleaved True) streamDir) |
| 164 | +DEF_LIST_DIR(listDirParOrdered, Stream.parConcatIterate (Stream.ordered True) streamDir) |
| 165 | + |
| 166 | +#define DEF_LIST_DIR_CHUNKED(x,y); \ |
| 167 | +x :: [Char] -> Stream IO Word8;\ |
| 168 | +x = listDirChunkedWith (y) |
| 169 | + |
| 170 | +DEF_LIST_DIR_CHUNKED(listDirChunkDfs, Stream.concatIterateDfs streamDirChunkedMaybe) |
| 171 | +DEF_LIST_DIR_CHUNKED(listDirChunkBfs, Stream.concatIterateBfs streamDirChunkedMaybe) |
| 172 | +DEF_LIST_DIR_CHUNKED(listDirChunkBfsRev, Stream.concatIterateBfsRev streamDirChunkedMaybe) |
| 173 | +DEF_LIST_DIR_CHUNKED(listDirChunkAppend, concatIterateWith streamDirChunked StreamK.append) |
| 174 | +DEF_LIST_DIR_CHUNKED(listDirChunkInterleave, mergeIterateWith streamDirChunked StreamK.interleave) |
| 175 | +DEF_LIST_DIR_CHUNKED(listDirChunkPar, Stream.parConcatIterate id streamDirChunked) |
| 176 | +DEF_LIST_DIR_CHUNKED(listDirChunkParInterleaved, Stream.parConcatIterate (Stream.interleaved True) streamDirChunked) |
| 177 | +DEF_LIST_DIR_CHUNKED(listDirChunkParOrdered, Stream.parConcatIterate (Stream.ordered True) streamDirChunked) |
0 commit comments