2import qualified Streamly.Prelude as S
3import Streamly.Internal.Data.Time.Clock (getTime, Clock(..))
4import Streamly.Internal.Data.Time.Units
5 (NanoSecond64, diffAbsTime64, fromRelTime64)
7import Control.Concurrent
12durationShouldBe :: (Double, Double) -> IO () -> Expectation
13durationShouldBe d@(tMin, tMax) action = do
14 t0 <- getTime Monotonic
16 t1 <- getTime Monotonic
17 let diff = fromRelTime64 (diffAbsTime64 t1 t0) :: NanoSecond64
18 let t = fromIntegral diff / 1e9
19 putStrLn $ "Expected: " <> show d <> " Took: " <> show t
20 (t <= tMax && t >= tMin) `shouldBe` True
22toMicroSecs :: Num a => a -> a
23toMicroSecs x = x * 10^(6 :: Int)
25measureRate' :: IsStream t
27 -> (t IO Int -> SerialT IO Int)
30 -> Either Double Int -- either rate or count of actions
35measureRate' desc t buffers threads rval consumerDelay producerDelay expectedRange = do
39 Left r -> S.take (round $ 10 * r) . S.repeatM
40 Right n -> S.replicateM n
42 rateDesc = case rval of
43 Left r -> " rate: " <> show r
44 Right n -> " count: " <> show n
47 <> " buffers: " <> show buffers
48 <> " threads: " <> show threads
49 <> ", consumer latency: " <> show consumerDelay
50 <> ", producer latency: " <> show producerDelay)
51 $ durationShouldBe expectedRange $
53 $ (if consumerDelay > 0
55 threadDelay (toMicroSecs consumerDelay) >> return x
60 $ (case rval of {Left r -> avgRate r; Right _ -> rate Nothing})
62 let (t1, t2) = producerDelay
64 then return $ round $ toMicroSecs t1
65 else randomRIO ( round $ toMicroSecs t1
66 , round $ toMicroSecs t2)
68 -- t1 <- getTime Monotonic
70 -- t2 <- getTime Monotonic
71 -- let delta = fromIntegral (toNanoSecs (t2 - t1)) / 1000000000
72 -- putStrLn $ "delay took: " <> show delta
73 -- when (delta > 2) $ do
74 -- putStrLn $ "delay took high: " <> show delta
77measureRateVariable :: IsStream t
79 -> (t IO Int -> SerialT IO Int)
85measureRateVariable desc t rval consumerDelay producerDelay dur =
86 measureRate' desc t (-1) (-1) (Left rval)
87 consumerDelay producerDelay dur
89measureRate :: IsStream t
91 -> (t IO Int -> SerialT IO Int)
97measureRate desc t rval consumerDelay producerDelay dur =
98 let d = fromIntegral producerDelay
99 in measureRateVariable desc t rval consumerDelay (d, d) dur
101measureThreads :: IsStream t
103 -> (t IO Int -> SerialT IO Int)
105 -> Int -- count of actions
107measureThreads desc t threads count = do
111 else fromIntegral count / fromIntegral threads
112 duration = (expectedTime * 0.9, expectedTime * 1.1)
113 measureRate' desc t (-1) threads (Right count) 0 (1,1) duration
115measureBuffers :: IsStream t
117 -> (t IO Int -> SerialT IO Int)
119 -> Int -- count of actions
121measureBuffers desc t buffers count = do
125 else fromIntegral count / fromIntegral buffers
126 duration = (expectedTime * 0.9, expectedTime * 1.1)
127 measureRate' desc t buffers (-1) (Right count) 0 (1,1) duration
132 describe "maxBuffers" $ do
133 measureBuffers "asyncly" asyncly (-1) 5
134 -- XXX this test fails due to a known issue
135 -- measureBuffers "maxBuffers" asyncly 1 5
136 measureBuffers "asyncly" asyncly 5 5
138 describe "maxThreads" $ do
139 measureThreads "asyncly" asyncly (-1) 5
140 measureThreads "asyncly" asyncly 1 5
141 measureThreads "asyncly" asyncly 5 5
143 measureThreads "aheadly" aheadly (-1) 5
144 measureThreads "aheadly" aheadly 1 5
145 measureThreads "aheadly" aheadly 5 5
149 -- Note that because after the last yield we don't wait, the last period
150 -- will be effectively shorter. This becomes significant when the rates are
151 -- lower (1 or lower). For rate 1 we lose 1 second in the end and for rate
153 let rates = [1, 10, 100, 1000, 10000
158 in describe "asyncly no consumer delay no producer delay" $
159 forM_ rates (\r -> measureRate "asyncly" asyncly r 0 0 range)
161 -- XXX try staggering the dispatches to achieve higher rates
162 let rates = [1, 10, 100, 1000
167 in describe "asyncly no consumer delay and 1 sec producer delay" $
168 forM_ rates (\r -> measureRate "asyncly" asyncly r 0 1 range)
170 -- At lower rates (1/10) this is likely to vary quite a bit depending on
171 -- the spread of random producer latencies generated.
172 let rates = [1, 10, 100, 1000
177 in describe "asyncly no consumer delay and variable producer delay" $
179 measureRateVariable "asyncly" asyncly r 0 (0.1, 3) range
181 let rates = [1, 10, 100, 1000, 10000
186 in describe "wAsyncly no consumer delay no producer delay" $
187 forM_ rates (\r -> measureRate "wAsyncly" wAsyncly r 0 0 range)
189 let rates = [1, 10, 100, 1000
194 in describe "wAsyncly no consumer delay and 1 sec producer delay" $
195 forM_ rates (\r -> measureRate "wAsyncly" wAsyncly r 0 1 range)
197 let rates = [1, 10, 100, 1000, 10000
202 in describe "aheadly no consumer delay no producer delay" $
203 forM_ rates (\r -> measureRate "aheadly" aheadly r 0 0 range)
205 -- XXX after the change to stop workers when the heap is clearing
206 -- thi does not work well at a 25000 ops per second, need to fix.
207 let rates = [1, 10, 100, 1000
212 in describe "aheadly no consumer delay and 1 sec producer delay" $
213 forM_ rates (\r -> measureRate "aheadly" aheadly r 0 1 range)
215 describe "asyncly with 1 sec producer delay and some consumer delay" $ do
216 -- ideally it should take 10 x 1 + 1 seconds
217 forM_ [1] (\r -> measureRate "asyncly" asyncly r 1 1 (11, 16))
218 -- ideally it should take 10 x 2 + 1 seconds
219 forM_ [1] (\r -> measureRate "asyncly" asyncly r 2 1 (21, 23))
220 -- ideally it should take 10 x 3 + 1 seconds
221 forM_ [1] (\r -> measureRate "asyncly" asyncly r 3 1 (31, 33))
223 describe "aheadly with 1 sec producer delay and some consumer delay" $ do
224 forM_ [1] (\r -> measureRate "aheadly" aheadly r 1 1 (11, 16))
225 forM_ [1] (\r -> measureRate "aheadly" aheadly r 2 1 (21, 23))
226 forM_ [1] (\r -> measureRate "aheadly" aheadly r 3 1 (31, 33))
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list