This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPerformance.hs
More file actions
113 lines (100 loc) · 2.88 KB
/
Performance.hs
File metadata and controls
113 lines (100 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import qualified Control.Monad.Concurrent as C
import Control.Monad (forever, replicateM_)
import Deli (Channel, Deli, JobTiming(..))
import Deli.Printer (printResults)
import System.Random
import qualified Deli
singleQueue
:: Channel JobTiming
-> Deli JobTiming ()
singleQueue queue =
forever $ do
job <- Deli.readChannel queue
Deli.runJob job
singleQueueExample :: IO ()
singleQueueExample = do
gen <- newStdGen
let durations = repeat 0.5
count = 1000 * 100
times = [0,1..(count - 1)]
jobs = zipWith JobTiming times durations
res = Deli.simulate gen jobs singleQueue
printResults res
chainedQueues
:: Channel JobTiming
-> Deli JobTiming ()
chainedQueues queue = do
middleChan <- Deli.newChannel Nothing
Deli.fork $ forever $ do
job <- Deli.readChannel middleChan
Deli.runJob job
forever $ do
job <- Deli.readChannel queue
Deli.writeChannel middleChan job
chainedQueueExample :: IO ()
chainedQueueExample = do
gen <- newStdGen
let durations = repeat 0.5
count = 1000 * 100
times = [0,1..(count - 1)]
jobs = zipWith JobTiming times durations
res = Deli.simulate gen jobs chainedQueues
printResults res
oneThread
:: Channel JobTiming
-> Deli JobTiming ()
oneThread queue = do
middleChan <- Deli.newChannel (Just 1)
forever $ do
jobA <- Deli.readChannel queue
Deli.writeChannel middleChan jobA
jobB <- Deli.readChannel middleChan
Deli.runJob jobB
oneThreadExample :: IO ()
oneThreadExample = do
gen <- newStdGen
let durations = repeat 0.5
count = 1000 * 1000
times = [0,1..(count - 1)]
jobs = zipWith JobTiming times durations
res = Deli.simulate gen jobs oneThread
printResults res
concurrentSingleExample
:: IO ()
concurrentSingleExample =
C.runConcurrentT $ do
chan <- C.newChannel (Just 1)
C.fork $ forever $
C.readChannel chan >> return ()
replicateM_ (1000 * 100 * 10) $ do
C.writeChannel chan True
concurrentChainedExample
:: IO ()
concurrentChainedExample =
C.runConcurrentT $ do
chanOne <- C.newChannel (Just 1)
chanTwo <- C.newChannel (Just 1)
C.fork $ forever $ do
val <- C.readChannel chanOne
C.writeChannel chanTwo val
C.fork $ forever $
C.readChannel chanTwo >> return ()
replicateM_ (1000 * 100 * 10) $ do
C.writeChannel chanOne True
main :: IO ()
main = do
newline
putStrLn "## singleQueueExample ##"
singleQueueExample
newline
newline
putStrLn "## chainedQueueExample ##"
chainedQueueExample
newline
newline
putStrLn "## oneThreadExample ##"
oneThreadExample
newline
concurrentSingleExample
concurrentChainedExample
where newline = putStrLn "\n"