on c004bd7 help sign in
152 lines 3.9 kB history raw
1-- |
2-- Module : Streamly.Benchmark.FileSystem.Handle
3-- Copyright : (c) 2019 Composewell Technologies
4--
5-- License : BSD3
6-- Maintainer : streamly@composewell.com
7-- Stability : experimental
8-- Portability : GHC
10{-# LANGUAGE CPP #-}
11{-# LANGUAGE ScopedTypeVariables #-}
13#ifdef __HADDOCK_VERSION__
14#undef INSPECTION
15#endif
17#ifdef INSPECTION
18{-# LANGUAGE TemplateHaskell #-}
19{-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-}
20#endif
22module Handle.Common
23 ( BenchEnv (..)
24 , RefHandles (..)
25 , scratchDir
26 , inFileSmall
27 , inFileBig
28 , outfile
29 , mkBench
30 , mkBenchSmall
31 , isSpace
32 , isSp
33 )
34where
36import Control.DeepSeq (NFData(rnf))
37import Data.Char (ord, chr)
38import Data.Word (Word8)
39import System.IO (openFile, IOMode(..), Handle, hClose)
41import Data.IORef
42import Gauge hiding (env)
44scratchDir :: String
45scratchDir = "benchmark-tmp"
47outfile :: String
48outfile = scratchDir ++ "out.txt"
50inFileSmall :: String
51inFileSmall = scratchDir ++ "in-10MB.txt"
53inFileBig :: String
54inFileBig = scratchDir ++ "in-100MB.txt"
56data RefHandles = RefHandles
57 { smallInH :: Handle
58 , bigInH :: Handle
59 , outputH :: Handle
60 }
62data Handles = Handles !Handle !Handle
64instance NFData Handles where
65 rnf _ = ()
67data BenchEnv = BenchEnv
68 { href :: IORef RefHandles
69 , smallSize :: Int
70 , bigSize :: Int
71 , nullH :: Handle
72 }
74withScaling :: BenchEnv -> String -> String
75withScaling env str =
76 let factor = round (fromIntegral (bigSize env)
77 / (fromIntegral (smallSize env) :: Double)) :: Int
78 in if factor == 1
79 then str
80 else str ++ " (1/" ++ show factor ++ ")"
82mkBenchCommon ::
83 NFData b
84 => (RefHandles -> Handles)
85 -> String
86 -> BenchEnv
87 -> (Handle -> Handle -> IO b)
88 -> Benchmark
89mkBenchCommon mkHandles name env action =
90 bench name $ perRunEnv (do
91 r <- readIORef $ href env
93 -- close old handles
94 hClose $ smallInH r
95 hClose $ bigInH r
96 hClose $ outputH r
98 -- reopen
99 smallInHandle <- openFile inFileSmall ReadMode
100 bigInHandle <- openFile inFileBig ReadMode
101 outHandle <- openFile outfile WriteMode
103 let refHandles = RefHandles
104 { smallInH = smallInHandle
105 , bigInH = bigInHandle
106 , outputH = outHandle
107 }
109 -- update
110 writeIORef (href env) $ refHandles
111 return $ mkHandles refHandles
112 )
113 (\(Handles h1 h2) -> action h1 h2)
115mkBench ::
116 NFData b => String -> BenchEnv -> (Handle -> Handle -> IO b) -> Benchmark
117mkBench name env action = mkBenchCommon useBigH name env action
119 where
121 useBigH (RefHandles {bigInH = inh, outputH = outh}) = Handles inh outh
123mkBenchSmall ::
124 NFData b => String -> BenchEnv -> (Handle -> Handle -> IO b) -> Benchmark
125mkBenchSmall name env action =
126 mkBenchCommon useSmallH (withScaling env name) env action
128 where
130 useSmallH (RefHandles {smallInH = inh, outputH = outh}) = Handles inh outh
132foreign import ccall unsafe "u_iswspace"
133 iswspace :: Int -> Int
135-- Code copied from base/Data.Char to INLINE it
136{-# INLINE isSpace #-}
137isSpace :: Char -> Bool
138-- isSpace includes non-breaking space
139-- The magic 0x377 isn't really that magical. As of 2014, all the codepoints
140-- at or below 0x377 have been assigned, so we shouldn't have to worry about
141-- any new spaces appearing below there. It would probably be best to
142-- use branchless ||, but currently the eqLit transformation will undo that,
143-- so we'll do it like this until there's a way around that.
144isSpace c
145 | uc <= 0x377 = uc == 32 || uc - 0x9 <= 4 || uc == 0xa0
146 | otherwise = iswspace (ord c) /= 0
147 where
148 uc = fromIntegral (ord c) :: Word
150{-# INLINE isSp #-}
151isSp :: Word8 -> Bool
152isSp = isSpace . chr . fromIntegral