-
Notifications
You must be signed in to change notification settings - Fork 4
/
bench.nim
34 lines (27 loc) · 997 Bytes
/
bench.nim
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
import times, strutils, os, osproc, posix, streams
type Rusage {.importc: "struct rusage", header: "<sys/resource.h>",
final, pure.} = object
ru_utime, ru_stime: Timeval
ru_maxrss, ru_ixrss, ru_idrss, ru_isrss, ru_minflt, ru_majflt, ru_nswap,
ru_inblock, ru_oublock, ru_msgsnd, ru_msgrcv, ru_nsignals, ru_nvcsw,
ru_nivcsw: clong
proc wait4(pid: Pid, status: ptr cint, options: cint, rusage: ptr Rusage): Pid
{.importc, header: "<sys/wait.h>", discardable.}
const
maxlen = 30
buflen = 8192
let
name = paramStr(1)
params = commandLineParams()
t0 = epochTime()
var
rusage: Rusage
buffer: array[buflen, char]
p = startProcess("./" & name, args = params[1 .. ^1])
outp = p.outputStream
while outp.readData(addr buffer[0], buflen) > 0: discard
wait4(Pid(p.processID), nil, 0, addr rusage)
let t1 = epochTime()
echo "| ", name, spaces(maxlen - name.len), " | ",
formatFloat(t1-t0, ffDecimal, 2).align(8), " | ",
align($rusage.ru_maxrss, 11), " |"