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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
module Test.Benchmark
( Benchmark, benchmark, prepareBenchmarkCleanup,
withPrepare, withCleanup,
iterateBench, (**>),
mapBench, pairBench, diffBench, (.-.),
runOn, runUntilOn, runUntilNothingOn, execBench,
benchTimeNF, benchCommandOutput,
CmdResult, cmdResultAverage,
exitStatus, elapsedTime, cpuTime, systemTime,
maxResidentMemory,
benchCommand, benchCommandWithLimit,
elapsedTime4Command, cpuTime4Command,
getHostName, getOS, getSystemID, getSystemRelease,
getSystemDescription, getCoreNumber, getCPUModel
)
where
import Data.List ( isPrefixOf )
import System.IO ( hGetContents )
import System.IOExts ( connectToCommand, evalCmd, readCompleteFile )
import System.Process ( getPID, system )
import Debug.Profile
data Benchmark a = BM (IO ()) (IO ()) (IO a)
benchmark :: IO a -> Benchmark a
benchmark bench = BM (return ()) (return ()) bench
instance Functor Benchmark where
fmap f (BM pre post action) = BM pre post $ action >>= return . f
instance Applicative Benchmark where
pure x = benchmark (return x)
fbench <*> abench = benchmark $
execBench fbench >>= \f -> execBench abench >>= \x -> return (f x)
instance Monad Benchmark where
bench1 >>= abench2 =
benchmark $ execBench bench1 >>= \x -> execBench (abench2 x)
mapBench :: (a -> b) -> Benchmark a -> Benchmark b
mapBench f (BM pre post action) = BM pre post $ action >>= return . f
pairBench :: Benchmark a -> Benchmark b -> Benchmark (a,b)
pairBench bench1 bench2 = benchmark $
execBench bench1 >>= \x -> execBench bench2 >>= \y -> return (x,y)
withPrepare :: Benchmark a -> IO () -> Benchmark a
withPrepare (BM pre post bench) newpre = BM (newpre >> pre) post bench
withCleanup :: Benchmark a -> IO () -> Benchmark a
withCleanup (BM pre post bench) newpost = BM pre (post >> newpost) bench
prepareBenchmarkCleanup :: IO () -> IO a -> IO () -> Benchmark a
prepareBenchmarkCleanup pre bench post =
benchmark $ execBench (BM pre post bench)
class MultiRunnable a where
average :: [a] -> a
instance MultiRunnable Int where
average xs = foldr (+) 0 xs `div` (length xs)
instance MultiRunnable Float where
average xs = foldr (+) 0.0 xs / fromInt (length xs)
instance MultiRunnable a => MultiRunnable (Maybe a) where
average = maybe Nothing (Just . average) . sequence
(**>) :: MultiRunnable a => Int -> Benchmark a -> Benchmark a
num **> (BM pre post action) = BM pre post $
mapM (\_ -> action) [1 .. num] >>= \rs -> return (average rs)
iterateBench :: ([a] -> b) -> Int -> Benchmark a -> Benchmark b
iterateBench averagef n (BM pre post action) = BM pre post $
mapM (\_ -> action) [1..n] >>= \rs -> return (averagef rs)
diffBench :: (a -> a -> a) -> Benchmark a -> Benchmark a -> Benchmark a
diffBench minus bench1 bench2 =
mapBench (uncurry minus) (pairBench bench1 bench2)
(.-.) :: Benchmark Float -> Benchmark Float -> Benchmark Float
bench1 .-. bench2 = diffBench (-) bench1 bench2
runOn :: (a -> Benchmark b) -> [a] -> Benchmark [(a,b)]
runOn bench xs =
benchmark $ mapM (\x -> execBench (bench x) >>= \y -> return (x,y)) xs
runUntilOn :: (a -> Benchmark b) -> (b -> Bool) -> [a] -> Benchmark [(a,b)]
runUntilOn _ _ [] = return []
runUntilOn bench stop (x:xs) = do
bmresult <- bench x
if stop bmresult
then return []
else do results <- runUntilOn bench stop xs
return ((x,bmresult):results)
runUntilNothingOn :: Eq b => (a -> Benchmark (Maybe b)) -> [a]
-> Benchmark [(a,b)]
runUntilNothingOn bench benchdata =
mapBench (map (\ (x,Just y) -> (x,y)))
(runUntilOn bench (==Nothing) benchdata)
execBench :: Benchmark a -> IO a
execBench (BM pre post action) = do
pre
result <- action
post
return result
benchTimeNF :: IO a -> Benchmark Float
benchTimeNF getexp = benchmark $ do
garbageCollect
garbageCollectorOff
pi1 <- getProcessInfos
exp <- getexp
seq (id $!! exp) (return ())
pi2 <- getProcessInfos
garbageCollectorOn
let rtime = maybe 0 id (lookup RunTime pi2)
- maybe 0 id (lookup RunTime pi1)
return (fromInt rtime / 1000.0)
benchCommandOutput :: String -> Benchmark (Int,String,String)
benchCommandOutput cmd = benchmark $ evalCmd cmd [] ""
data CmdResult = CD String Int Float Float Float Int
instance MultiRunnable CmdResult where
average cds =
CD (if null cds then "" else (cmdString (head cds)))
(if all (==0) (map exitStatus cds) then 0 else 1)
(average (map elapsedTime cds))
(average (map cpuTime cds))
(average (map systemTime cds))
(average (map maxResidentMemory cds))
cmdResultAverage :: [CmdResult] -> CmdResult
cmdResultAverage = average
cmdString :: CmdResult -> String
cmdString (CD cs _ _ _ _ _) = cs
exitStatus :: CmdResult -> Int
exitStatus (CD _ s _ _ _ _) = s
checkExitStatus :: String -> Int -> a -> a
checkExitStatus cmd s x =
if s == 0
then x
else error $ "Benchmark command '" ++ cmd ++ "' has exit status " ++ show s
elapsedTime :: CmdResult -> Float
elapsedTime (CD cs s e _ _ _) = checkExitStatus cs s e
cpuTime :: CmdResult -> Float
cpuTime (CD cs s _ c _ _) = checkExitStatus cs s c
systemTime :: CmdResult -> Float
systemTime (CD cs s _ _ st _) = checkExitStatus cs s st
maxResidentMemory :: CmdResult -> Int
maxResidentMemory (CD cs s _ _ _ m) = checkExitStatus cs s m
benchCommand :: String -> Benchmark CmdResult
benchCommand cmd = benchmark $ do
pid <- getPID
let timefile = ".time" ++ show pid
timecmd = "/usr/bin/time -q --format=\"BENCHMARKTIME=(%e,%U,%S,%M)\" -o "
++ timefile ++ " " ++ cmd
status <- system timecmd
bmout <- readCompleteFile timefile
case reads (extractTimeInOutput bmout) of
[((etime,ctime,stime,maxmem),_)] -> do
system $ "rm -f " ++ timefile
return (CD cmd status etime ctime stime maxmem)
_ -> error $ "Cannot read output of time command:\n" ++ bmout
where
extractTimeInOutput =
tail . snd . break (=='=') . head . filter ("BENCHMARKTIME" `isPrefixOf`)
. lines
elapsedTime4Command :: String -> Benchmark Float
elapsedTime4Command = mapBench elapsedTime . benchCommand
cpuTime4Command :: String -> Benchmark Float
cpuTime4Command = mapBench cpuTime . benchCommand
benchCommandWithLimit :: String -> Float -> Benchmark (Maybe CmdResult)
benchCommandWithLimit cmd tlimit =
mapBench (\cd -> if exitStatus cd == 0 then Just cd else Nothing)
(benchCommand $ "/usr/bin/timeout " ++ show tlimit ++ "s " ++ cmd)
getHostName :: IO String
getHostName = runCmd "hostname"
getOS :: IO String
getOS = runCmd "uname -o"
getSystemDescription :: IO String
getSystemDescription = runCmd "lsb_release -s -d"
getSystemID :: IO String
getSystemID = runCmd "lsb_release -s -i"
getSystemRelease :: IO String
getSystemRelease = runCmd "lsb_release -s -r"
getCoreNumber :: IO String
getCoreNumber =
readFile "/proc/cpuinfo" >>=
return . show . length . filter (\ s -> take 9 s == "processor") . lines
getCPUModel :: IO String
getCPUModel = do
cpuinfo <- readFile "/proc/cpuinfo"
let modelnames = filter (\ s -> take 2 (words s) == ["model","name"])
(lines cpuinfo)
return (if null modelnames
then "???"
else let (_,mname) = break (==':') (head modelnames)
in if null mname then "???"
else strip (delCRTM (tail mname)))
where
delCRTM s = case s of
[] -> []
'(':'R':')':xs -> delCRTM xs
'(':'T':'M':')':xs -> delCRTM xs
x:xs -> x : delCRTM xs
runCmd :: String -> IO String
runCmd cmd = connectToCommand cmd >>= hGetContents >>= return . strip
strip :: String -> String
strip = reverse . dropWhile isSpace . reverse . dropWhile isSpace
|