2{-# LANGUAGE FlexibleContexts #-}
5import qualified Streamly.Prelude as S
6import Control.Concurrent
12durationShouldBe :: (Double, Double) -> IO () -> Expectation
13durationShouldBe d@(tMin, tMax) action = do
14 t0 <- getTime Monotonic
16 t1 <- getTime Monotonic
17 let t = fromIntegral (toNanoSecs (t1 - t0)) / 1e9
18 -- tMax = fromNanoSecs (round $ d*10^9*1.2)
19 -- tMin = fromNanoSecs (round $ d*10^9*0.8)
20 putStrLn $ "Expected: " <> show d <> " Took: " <> show t
21 (t <= tMax && t >= tMin) `shouldBe` True
23toMicroSecs :: Num a => a -> a
24toMicroSecs x = x * 10^(6 :: Int)
26measureRate' :: IsStream t
28 -> (t IO Int -> SerialT IO Int)
31 -> Either Double Int -- either rate or count of actions
36measureRate' desc t buffers threads rval consumerDelay producerDelay expectedRange = do
40 Left r -> S.take (round $ 10 * r) . S.repeatM
41 Right n -> S.replicateM n
43 rateDesc = case rval of
44 Left r -> " rate: " <> show r
45 Right n -> " count: " <> show n
48 <> " buffers: " <> show buffers
49 <> " threads: " <> show threads
50 <> ", consumer latency: " <> show consumerDelay
51 <> ", producer latency: " <> show producerDelay)
52 $ durationShouldBe expectedRange $
54 $ (if consumerDelay > 0
56 threadDelay (toMicroSecs consumerDelay) >> return x
61 $ (case rval of {Left r -> avgRate r; Right _ -> rate Nothing})
63 let (t1, t2) = producerDelay
65 then return $ round $ toMicroSecs t1
66 else randomRIO ( round $ toMicroSecs t1
67 , round $ toMicroSecs t2)
69 -- t1 <- getTime Monotonic
71 -- t2 <- getTime Monotonic
72 -- let delta = fromIntegral (toNanoSecs (t2 - t1)) / 1000000000
73 -- putStrLn $ "delay took: " <> show delta
74 -- when (delta > 2) $ do
75 -- putStrLn $ "delay took high: " <> show delta
78measureRateVariable :: IsStream t
80 -> (t IO Int -> SerialT IO Int)
86measureRateVariable desc t rval consumerDelay producerDelay dur =
87 measureRate' desc t (-1) (-1) (Left rval)
88 consumerDelay producerDelay dur
90measureRate :: IsStream t
92 -> (t IO Int -> SerialT IO Int)
98measureRate desc t rval consumerDelay producerDelay dur =
99 let d = fromIntegral producerDelay
100 in measureRateVariable desc t rval consumerDelay (d, d) dur
102measureThreads :: IsStream t
104 -> (t IO Int -> SerialT IO Int)
106 -> Int -- count of actions
108measureThreads desc t threads count = do
112 else fromIntegral count / fromIntegral threads
113 duration = (expectedTime * 0.9, expectedTime * 1.1)
114 measureRate' desc t (-1) threads (Right count) 0 (1,1) duration
116measureBuffers :: IsStream t
118 -> (t IO Int -> SerialT IO Int)
120 -> Int -- count of actions
122measureBuffers desc t buffers count = do
126 else fromIntegral count / fromIntegral buffers
127 duration = (expectedTime * 0.9, expectedTime * 1.1)
128 measureRate' desc t buffers (-1) (Right count) 0 (1,1) duration
133 describe "maxBuffers" $ do
134 measureBuffers "asyncly" asyncly (-1) 5
135 -- XXX this test fails due to a known issue
136 -- measureBuffers "maxBuffers" asyncly 1 5
137 measureBuffers "asyncly" asyncly 5 5
139 describe "maxThreads" $ do
140 measureThreads "asyncly" asyncly (-1) 5
141 measureThreads "asyncly" asyncly 1 5
142 measureThreads "asyncly" asyncly 5 5
144 measureThreads "aheadly" aheadly (-1) 5
145 measureThreads "aheadly" aheadly 1 5
146 measureThreads "aheadly" aheadly 5 5
150 -- Note that because after the last yield we don't wait, the last period
151 -- will be effectively shorter. This becomes significant when the rates are
152 -- lower (1 or lower). For rate 1 we lose 1 second in the end and for rate
154 let rates = [1, 10, 100, 1000, 10000
159 in describe "asyncly no consumer delay no producer delay" $
160 forM_ rates (\r -> measureRate "asyncly" asyncly r 0 0 range)
162 -- XXX try staggering the dispatches to achieve higher rates
163 let rates = [1, 10, 100, 1000
168 in describe "asyncly no consumer delay and 1 sec producer delay" $
169 forM_ rates (\r -> measureRate "asyncly" asyncly r 0 1 range)
171 -- At lower rates (1/10) this is likely to vary quite a bit depending on
172 -- the spread of random producer latencies generated.
173 let rates = [1, 10, 100, 1000
178 in describe "asyncly no consumer delay and variable producer delay" $
180 measureRateVariable "asyncly" asyncly r 0 (0.1, 3) range
182 let rates = [1, 10, 100, 1000, 10000
187 in describe "wAsyncly no consumer delay no producer delay" $
188 forM_ rates (\r -> measureRate "wAsyncly" wAsyncly r 0 0 range)
190 let rates = [1, 10, 100, 1000
195 in describe "wAsyncly no consumer delay and 1 sec producer delay" $
196 forM_ rates (\r -> measureRate "wAsyncly" wAsyncly r 0 1 range)
198 let rates = [1, 10, 100, 1000, 10000
203 in describe "aheadly no consumer delay no producer delay" $
204 forM_ rates (\r -> measureRate "aheadly" aheadly r 0 0 range)
206 -- XXX after the change to stop workers when the heap is clearing
207 -- thi does not work well at a 25000 ops per second, need to fix.
208 let rates = [1, 10, 100, 1000
213 in describe "aheadly no consumer delay and 1 sec producer delay" $
214 forM_ rates (\r -> measureRate "aheadly" aheadly r 0 1 range)
216 describe "asyncly with 1 sec producer delay and some consumer delay" $ do
217 -- ideally it should take 10 x 1 + 1 seconds
218 forM_ [1] (\r -> measureRate "asyncly" asyncly r 1 1 (11, 16))
219 -- ideally it should take 10 x 2 + 1 seconds
220 forM_ [1] (\r -> measureRate "asyncly" asyncly r 2 1 (21, 23))
221 -- ideally it should take 10 x 3 + 1 seconds
222 forM_ [1] (\r -> measureRate "asyncly" asyncly r 3 1 (31, 33))
224 describe "aheadly with 1 sec producer delay and some consumer delay" $ do
225 forM_ [1] (\r -> measureRate "aheadly" aheadly r 1 1 (11, 16))
226 forM_ [1] (\r -> measureRate "aheadly" aheadly r 2 1 (21, 23))
227 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