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
|
module Benchmarks(Benchmark, benchmark, prepareBenchmarkCleanup,
withPrepare, withCleanup,
iterateBench, (*>), (*>-),
mapBench, pairBench, diffBench, (.-.),
(>!>=), returnBM,
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 IO
import IOExts
import List
import Profile
import ReadShowTerm
import Char
import System
import Float
import Maybe(sequenceMaybe)
data Benchmark a = BM (IO ()) (IO ()) (IO a)
benchmark :: IO a -> Benchmark a
benchmark bench = BM done done bench
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 =
BM done done (execBench (BM pre post bench))
iterateBench :: ([a] -> b) -> Int -> Benchmark a -> Benchmark b
iterateBench average n (BM pre post action) = BM pre post $
mapIO (\_ -> action) [1..n] >>= \rs -> return (average rs)
(*>) :: Int -> Benchmark Float -> Benchmark Float
n *> floatbench = iterateBench floatAverage n floatbench
(*>-) :: Int -> Benchmark (Maybe Float) -> Benchmark (Maybe Float)
n *>- mbfloatbench = iterateBench maybeFloatAverage n mbfloatbench
where maybeFloatAverage = maybe Nothing (Just . floatAverage) . sequenceMaybe
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 = BM done done $
execBench bench1 >>= \x -> execBench bench2 >>= \y -> return (x,y)
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
(>!>=) :: Benchmark a -> (a -> Benchmark b) -> Benchmark b
bench1 >!>= abench2 = BM done done $
execBench bench1 >>= \x -> execBench (abench2 x)
returnBM :: a -> Benchmark a
returnBM x = benchmark (return x)
runOn :: (a -> Benchmark b) -> [a] -> Benchmark [(a,b)]
runOn bench xs = BM done done $
mapIO (\x -> execBench (bench x) >>= \y -> return (x,y)) xs
runUntilOn :: (a -> Benchmark b) -> (b -> Bool) -> [a] -> Benchmark [(a,b)]
runUntilOn _ _ [] = returnBM []
runUntilOn bench stop (x:xs) =
bench x >!>= \bmresult ->
if stop bmresult then returnBM []
else runUntilOn bench stop xs >!>= \results ->
returnBM ((x,bmresult):results)
runUntilNothingOn :: (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) done
pi2 <- getProcessInfos
garbageCollectorOn
let rtime = maybe 0 id (lookup RunTime pi2)
- maybe 0 id (lookup RunTime pi1)
return (i2f rtime /. 1000.0)
benchCommandOutput :: String -> Benchmark (Int,String,String)
benchCommandOutput cmd = benchmark $ evalCmd cmd [] ""
data CmdResult = CD String Int Float Float Float Int
cmdResultAverage :: [CmdResult] -> CmdResult
cmdResultAverage cds =
CD (if null cds then "" else (cmdString (head cds)))
(if all (==0) (map exitStatus cds) then 0 else 1)
(floatAverage (map elapsedTime cds))
(floatAverage (map cpuTime cds))
(floatAverage (map systemTime cds))
(intAverage (map maxResidentMemory cds))
cmdString :: CmdResult -> String
cmdString (CD cs _ _ _ _ _) = cs
exitStatus :: CmdResult -> Int
exitStatus (CD _ s _ _ _ _) = s
elapsedTime :: CmdResult -> Float
elapsedTime (CD cs s e _ _ _) =
if s==0 then e
else error ("Benchmark command '"++cs++"' has exit status "++show s)
cpuTime :: CmdResult -> Float
cpuTime (CD cs s _ c _ _) =
if s==0 then c
else error ("Benchmark command '"++cs++"' has exit status "++show s)
systemTime :: CmdResult -> Float
systemTime (CD cs s _ _ st _) =
if s==0 then st
else error ("Benchmark command '"++cs++"' has exit status "++show s)
maxResidentMemory :: CmdResult -> Int
maxResidentMemory (CD cs s _ _ _ m) =
if s==0 then m
else error ("Benchmark command '"++cs++"' has exit status "++show s)
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
let (etime,ctime,stime,maxmem) = readQTerm (extractTimeInOutput bmout)
system $ "rm -f "++timefile
return (CD cmd status etime ctime stime maxmem)
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
intAverage :: [Int] -> Int
intAverage xs = foldr (+) 0 xs `div` (length xs)
floatAverage :: [Float] -> Float
floatAverage xs = foldr (+.) 0.0 xs /. (i2f (length xs))
strip :: String -> String
strip = reverse . dropWhile isSpace . reverse . dropWhile isSpace
|