2020-10-01 20:05:27 +03:00
|
|
|
|
module main
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import flag
|
|
|
|
|
import time
|
2020-10-01 23:25:29 +03:00
|
|
|
|
import term
|
|
|
|
|
import math
|
2020-10-01 20:05:27 +03:00
|
|
|
|
import scripting
|
2020-10-01 23:25:29 +03:00
|
|
|
|
import v.util
|
2020-10-01 20:05:27 +03:00
|
|
|
|
|
|
|
|
|
struct CmdResult {
|
|
|
|
|
mut:
|
2020-10-15 16:17:52 +03:00
|
|
|
|
runs int
|
|
|
|
|
cmd string
|
|
|
|
|
icmd int
|
2020-10-01 20:05:27 +03:00
|
|
|
|
outputs []string
|
2020-10-15 16:17:52 +03:00
|
|
|
|
oms map[string][]int
|
2020-10-01 23:25:29 +03:00
|
|
|
|
summary map[string]Aints
|
2020-10-01 20:05:27 +03:00
|
|
|
|
timings []int
|
2020-10-01 23:25:29 +03:00
|
|
|
|
atiming Aints
|
2020-10-01 20:05:27 +03:00
|
|
|
|
}
|
2020-10-15 16:17:52 +03:00
|
|
|
|
|
2020-10-01 20:05:27 +03:00
|
|
|
|
struct Context {
|
|
|
|
|
mut:
|
2020-10-15 16:17:52 +03:00
|
|
|
|
count int
|
|
|
|
|
series int
|
|
|
|
|
warmup int
|
|
|
|
|
show_help bool
|
|
|
|
|
show_output bool
|
2020-12-04 19:11:43 +03:00
|
|
|
|
use_newline bool // use \n instead of \r, so the last line is not overwritten
|
2020-10-02 10:57:58 +03:00
|
|
|
|
fail_on_regress_percent int
|
2020-10-15 16:17:52 +03:00
|
|
|
|
fail_on_maxtime int // in ms
|
|
|
|
|
verbose bool
|
|
|
|
|
commands []string
|
|
|
|
|
results []CmdResult
|
2020-11-29 17:13:45 +03:00
|
|
|
|
cmd_template string // {T} will be substituted with the current command
|
|
|
|
|
cmd_params map[string][]string
|
2020-10-15 16:17:52 +03:00
|
|
|
|
cline string // a terminal clearing line
|
2020-12-04 19:11:43 +03:00
|
|
|
|
cgoback string
|
2020-12-04 20:18:58 +03:00
|
|
|
|
nmins int // number of minimums to discard
|
|
|
|
|
nmaxs int // number of maximums to discard
|
2020-10-01 20:05:27 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-01 23:25:29 +03:00
|
|
|
|
struct Aints {
|
2021-03-08 21:52:13 +03:00
|
|
|
|
values []int
|
2020-10-01 23:25:29 +03:00
|
|
|
|
mut:
|
2020-10-15 16:17:52 +03:00
|
|
|
|
imin int
|
|
|
|
|
imax int
|
2020-10-01 23:25:29 +03:00
|
|
|
|
average f64
|
2020-10-15 16:17:52 +03:00
|
|
|
|
stddev f64
|
2020-12-04 20:18:58 +03:00
|
|
|
|
nmins int // number of discarded fastest results
|
|
|
|
|
nmaxs int // number of discarded slowest results
|
2020-10-01 23:25:29 +03:00
|
|
|
|
}
|
2020-10-15 16:17:52 +03:00
|
|
|
|
|
2020-12-04 20:18:58 +03:00
|
|
|
|
fn new_aints(ovals []int, extreme_mins int, extreme_maxs int) Aints {
|
2020-10-15 16:17:52 +03:00
|
|
|
|
mut res := Aints{
|
2020-12-04 20:18:58 +03:00
|
|
|
|
values: ovals // remember the original values
|
|
|
|
|
nmins: extreme_mins
|
|
|
|
|
nmaxs: extreme_maxs
|
2020-10-15 16:17:52 +03:00
|
|
|
|
}
|
2020-10-01 23:25:29 +03:00
|
|
|
|
mut sum := i64(0)
|
2020-10-15 16:17:52 +03:00
|
|
|
|
mut imin := math.max_i32
|
2020-10-01 23:25:29 +03:00
|
|
|
|
mut imax := -math.max_i32
|
2020-12-04 20:18:58 +03:00
|
|
|
|
// discard the extremes:
|
|
|
|
|
mut vals := ovals.clone()
|
|
|
|
|
vals.sort()
|
|
|
|
|
if vals.len > extreme_mins + extreme_maxs {
|
|
|
|
|
vals = vals[extreme_mins..vals.len - extreme_maxs]
|
|
|
|
|
} else {
|
|
|
|
|
vals = []
|
|
|
|
|
}
|
|
|
|
|
// statistical processing of the remaining values:
|
2020-10-01 23:25:29 +03:00
|
|
|
|
for i in vals {
|
2020-10-15 16:17:52 +03:00
|
|
|
|
sum += i
|
2020-10-01 23:25:29 +03:00
|
|
|
|
if i < imin {
|
|
|
|
|
imin = i
|
|
|
|
|
}
|
|
|
|
|
if i > imax {
|
|
|
|
|
imax = i
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
res.imin = imin
|
|
|
|
|
res.imax = imax
|
|
|
|
|
if vals.len > 0 {
|
|
|
|
|
res.average = sum / f64(vals.len)
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
mut devsum := f64(0.0)
|
|
|
|
|
for i in vals {
|
|
|
|
|
x := f64(i) - res.average
|
|
|
|
|
devsum += (x * x)
|
|
|
|
|
}
|
2020-10-15 16:17:52 +03:00
|
|
|
|
res.stddev = math.sqrt(devsum / f64(vals.len))
|
2020-12-04 20:18:58 +03:00
|
|
|
|
// eprintln('\novals: $ovals\n vals: $vals\n vals.len: $vals.len | res.imin: $res.imin | res.imax: $res.imax | res.average: $res.average | res.stddev: $res.stddev')
|
2020-10-01 23:25:29 +03:00
|
|
|
|
return res
|
|
|
|
|
}
|
2020-10-15 16:17:52 +03:00
|
|
|
|
|
|
|
|
|
fn (a Aints) str() string {
|
2020-12-04 20:18:58 +03:00
|
|
|
|
return util.bold('${a.average:6.2f}') +
|
|
|
|
|
'ms ± σ: ${a.stddev:4.1f}ms, min: ${a.imin:4}ms, max: ${a.imax:4}ms, runs:${a.values.len:3}, nmins:${a.nmins:2}, nmaxs:${a.nmaxs:2}'
|
2020-10-15 16:17:52 +03:00
|
|
|
|
}
|
2020-10-01 23:25:29 +03:00
|
|
|
|
|
2020-10-02 10:57:58 +03:00
|
|
|
|
const (
|
2020-12-04 20:18:58 +03:00
|
|
|
|
max_fail_percent = 100 * 1000
|
2020-10-15 16:17:52 +03:00
|
|
|
|
max_time = 60 * 1000 // ms
|
2020-10-09 11:06:00 +03:00
|
|
|
|
performance_regression_label = 'Performance regression detected, failing since '
|
2020-10-02 10:57:58 +03:00
|
|
|
|
)
|
2020-10-15 16:17:52 +03:00
|
|
|
|
|
|
|
|
|
fn main() {
|
2020-10-01 20:05:27 +03:00
|
|
|
|
mut context := Context{}
|
2020-10-01 20:46:45 +03:00
|
|
|
|
context.parse_options()
|
|
|
|
|
context.run()
|
|
|
|
|
context.show_diff_summary()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn (mut context Context) parse_options() {
|
2020-10-01 20:05:27 +03:00
|
|
|
|
mut fp := flag.new_flag_parser(os.args)
|
|
|
|
|
fp.application(os.file_name(os.executable()))
|
|
|
|
|
fp.version('0.0.1')
|
2020-10-02 13:28:05 +03:00
|
|
|
|
fp.description('Repeat command(s) and collect statistics. NB: you have to quote each command, if it contains spaces.')
|
2020-10-01 20:05:27 +03:00
|
|
|
|
fp.arguments_description('CMD1 CMD2 ...')
|
|
|
|
|
fp.skip_executable()
|
|
|
|
|
fp.limit_free_args_to_at_least(1)
|
2020-10-02 13:28:05 +03:00
|
|
|
|
context.count = fp.int('count', `c`, 10, 'Repetition count.')
|
|
|
|
|
context.series = fp.int('series', `s`, 2, 'Series count. `-s 2 -c 4 a b` => aaaabbbbaaaabbbb, while `-s 3 -c 2 a b` => aabbaabbaabb.')
|
|
|
|
|
context.warmup = fp.int('warmup', `w`, 2, 'Warmup runs. These are done *only at the start*, and are ignored.')
|
2020-10-01 20:05:27 +03:00
|
|
|
|
context.show_help = fp.bool('help', `h`, false, 'Show this help screen.')
|
2020-12-04 20:18:58 +03:00
|
|
|
|
context.use_newline = fp.bool('newline', `n`, false, 'Use \\n, do not overwrite the last line. Produces more output, but easier to diagnose.')
|
2020-10-02 18:10:25 +03:00
|
|
|
|
context.show_output = fp.bool('output', `O`, false, 'Show command stdout/stderr in the progress indicator for each command. NB: slower, for verbose commands.')
|
2020-10-01 20:05:27 +03:00
|
|
|
|
context.verbose = fp.bool('verbose', `v`, false, 'Be more verbose.')
|
2020-10-09 11:06:00 +03:00
|
|
|
|
context.fail_on_maxtime = fp.int('max_time', `m`, max_time, 'Fail with exit code 2, when first cmd takes above M milliseconds (regression).')
|
|
|
|
|
context.fail_on_regress_percent = fp.int('fail_percent', `f`, max_fail_percent, 'Fail with exit code 3, when first cmd is X% slower than the rest (regression).')
|
2020-11-29 17:13:45 +03:00
|
|
|
|
context.cmd_template = fp.string('template', `t`, '{T}', 'Command template. {T} will be substituted with the current command.')
|
|
|
|
|
cmd_params := fp.string_multi('parameter', `p`, 'A parameter substitution list. `{p}=val1,val2,val2` means that {p} in the template, will be substituted with each of val1, val2, val3.')
|
2020-12-04 20:18:58 +03:00
|
|
|
|
context.nmins = fp.int('nmins', `i`, 0, 'Ignore the BOTTOM X results (minimum execution time). Makes the results more robust to performance flukes.')
|
|
|
|
|
context.nmaxs = fp.int('nmaxs', `a`, 1, 'Ignore the TOP X results (maximum execution time). Makes the results more robust to performance flukes.')
|
2020-11-29 17:13:45 +03:00
|
|
|
|
for p in cmd_params {
|
|
|
|
|
parts := p.split(':')
|
|
|
|
|
if parts.len > 1 {
|
|
|
|
|
context.cmd_params[parts[0]] = parts[1].split(',')
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-01 20:05:27 +03:00
|
|
|
|
if context.show_help {
|
|
|
|
|
println(fp.usage())
|
|
|
|
|
exit(0)
|
|
|
|
|
}
|
|
|
|
|
if context.verbose {
|
|
|
|
|
scripting.set_verbose(true)
|
|
|
|
|
}
|
2020-11-29 17:13:45 +03:00
|
|
|
|
commands := fp.finalize() or {
|
2021-02-28 22:24:29 +03:00
|
|
|
|
eprintln('Error: $err')
|
2020-10-01 20:05:27 +03:00
|
|
|
|
exit(1)
|
|
|
|
|
}
|
2020-11-29 17:13:45 +03:00
|
|
|
|
context.commands = context.expand_all_commands(commands)
|
2020-10-15 16:17:52 +03:00
|
|
|
|
context.results = []CmdResult{len: context.commands.len, init: CmdResult{}}
|
2020-12-04 19:11:43 +03:00
|
|
|
|
if context.use_newline {
|
|
|
|
|
context.cline = '\n'
|
|
|
|
|
context.cgoback = '\n'
|
|
|
|
|
} else {
|
|
|
|
|
context.cline = '\r' + term.h_divider('')
|
|
|
|
|
context.cgoback = '\r'
|
|
|
|
|
}
|
2020-10-01 23:25:29 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn (mut context Context) clear_line() {
|
|
|
|
|
print(context.cline)
|
2020-10-01 20:46:45 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-29 17:13:45 +03:00
|
|
|
|
fn (mut context Context) expand_all_commands(commands []string) []string {
|
|
|
|
|
mut all_commands := []string{}
|
|
|
|
|
for cmd in commands {
|
|
|
|
|
maincmd := context.cmd_template.replace('{T}', cmd)
|
|
|
|
|
mut substituted_commands := [maincmd]
|
|
|
|
|
for paramk, paramlist in context.cmd_params {
|
|
|
|
|
for paramv in paramlist {
|
|
|
|
|
mut new_substituted_commands := []string{}
|
|
|
|
|
for cscmd in substituted_commands {
|
|
|
|
|
scmd := cscmd.replace(paramk, paramv)
|
|
|
|
|
new_substituted_commands << scmd
|
|
|
|
|
}
|
|
|
|
|
substituted_commands << new_substituted_commands
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
all_commands << substituted_commands
|
|
|
|
|
}
|
|
|
|
|
mut unique := map[string]int{}
|
|
|
|
|
for x in all_commands {
|
|
|
|
|
if x.contains('{') && x.contains('}') {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
unique[x] = 1
|
|
|
|
|
}
|
|
|
|
|
return unique.keys()
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-01 20:46:45 +03:00
|
|
|
|
fn (mut context Context) run() {
|
2020-10-02 13:28:05 +03:00
|
|
|
|
mut run_warmups := 0
|
2020-10-15 16:17:52 +03:00
|
|
|
|
for si in 1 .. context.series + 1 {
|
2020-10-02 13:28:05 +03:00
|
|
|
|
for icmd, cmd in context.commands {
|
|
|
|
|
mut runs := 0
|
|
|
|
|
mut duration := 0
|
|
|
|
|
mut sum := 0
|
|
|
|
|
mut oldres := ''
|
|
|
|
|
println('Series: ${si:4}/${context.series:-4}, command: $cmd')
|
|
|
|
|
if context.warmup > 0 && run_warmups < context.commands.len {
|
2020-10-15 16:17:52 +03:00
|
|
|
|
for i in 1 .. context.warmup + 1 {
|
2020-12-04 19:11:43 +03:00
|
|
|
|
print('${context.cgoback}warming up run: ${i:4}/${context.warmup:-4} for ${cmd:-50s} took ${duration:6} ms ...')
|
2020-10-02 13:28:05 +03:00
|
|
|
|
mut sw := time.new_stopwatch({})
|
2021-03-08 21:52:13 +03:00
|
|
|
|
res := os.execute(cmd)
|
|
|
|
|
if res.exit_code != 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2020-10-02 13:28:05 +03:00
|
|
|
|
duration = int(sw.elapsed().milliseconds())
|
|
|
|
|
}
|
|
|
|
|
run_warmups++
|
|
|
|
|
}
|
|
|
|
|
context.clear_line()
|
2020-10-15 16:17:52 +03:00
|
|
|
|
for i in 1 .. (context.count + 1) {
|
|
|
|
|
avg := f64(sum) / f64(i)
|
2020-12-04 19:11:43 +03:00
|
|
|
|
print('${context.cgoback}Average: ${avg:9.3f}ms | run: ${i:4}/${context.count:-4} | took ${duration:6} ms')
|
2020-10-02 18:10:25 +03:00
|
|
|
|
if context.show_output {
|
2020-10-15 16:17:52 +03:00
|
|
|
|
print(' | result: ${oldres:s}')
|
2020-10-02 13:28:05 +03:00
|
|
|
|
}
|
2020-10-01 20:13:19 +03:00
|
|
|
|
mut sw := time.new_stopwatch({})
|
2020-12-04 19:11:43 +03:00
|
|
|
|
res := scripting.exec(cmd) or { continue }
|
2020-10-01 20:13:19 +03:00
|
|
|
|
duration = int(sw.elapsed().milliseconds())
|
2020-10-02 13:28:05 +03:00
|
|
|
|
if res.exit_code != 0 {
|
|
|
|
|
eprintln('${i:10} non 0 exit code for cmd: $cmd')
|
|
|
|
|
continue
|
|
|
|
|
}
|
2021-03-08 21:52:13 +03:00
|
|
|
|
context.results[icmd].outputs << res.output.trim_right('\r\n').replace('\r\n',
|
|
|
|
|
'\n').split('\n')
|
2020-10-02 13:28:05 +03:00
|
|
|
|
context.results[icmd].timings << duration
|
|
|
|
|
sum += duration
|
|
|
|
|
runs++
|
|
|
|
|
oldres = res.output.replace('\n', ' ')
|
2020-10-01 20:13:19 +03:00
|
|
|
|
}
|
2020-10-02 13:28:05 +03:00
|
|
|
|
context.results[icmd].cmd = cmd
|
|
|
|
|
context.results[icmd].icmd = icmd
|
2020-10-06 08:12:09 +03:00
|
|
|
|
context.results[icmd].runs += runs
|
2020-12-04 20:18:58 +03:00
|
|
|
|
context.results[icmd].atiming = new_aints(context.results[icmd].timings, context.nmins,
|
|
|
|
|
context.nmaxs)
|
2020-10-02 13:28:05 +03:00
|
|
|
|
context.clear_line()
|
2020-12-04 19:11:43 +03:00
|
|
|
|
print(context.cgoback)
|
2020-10-15 16:17:52 +03:00
|
|
|
|
mut m := map[string][]int{}
|
2020-10-02 13:28:05 +03:00
|
|
|
|
for o in context.results[icmd].outputs {
|
|
|
|
|
x := o.split(':')
|
|
|
|
|
if x.len > 1 {
|
|
|
|
|
k := x[0]
|
|
|
|
|
v := x[1].trim_left(' ').int()
|
|
|
|
|
m[k] << v
|
|
|
|
|
}
|
2020-10-09 11:06:00 +03:00
|
|
|
|
}
|
2020-10-02 13:28:05 +03:00
|
|
|
|
mut summary := map[string]Aints{}
|
2020-10-15 16:17:52 +03:00
|
|
|
|
for k, v in m {
|
2020-10-06 08:12:09 +03:00
|
|
|
|
// show a temporary summary for the current series/cmd cycle
|
2020-12-04 20:18:58 +03:00
|
|
|
|
s := new_aints(v, context.nmins, context.nmaxs)
|
2020-10-02 13:28:05 +03:00
|
|
|
|
println(' $k: $s')
|
|
|
|
|
summary[k] = s
|
2020-10-01 20:05:27 +03:00
|
|
|
|
}
|
2020-10-06 08:12:09 +03:00
|
|
|
|
// merge current raw results to the previous ones
|
2021-02-16 14:46:12 +03:00
|
|
|
|
old_oms := context.results[icmd].oms.move()
|
2020-10-15 16:17:52 +03:00
|
|
|
|
mut new_oms := map[string][]int{}
|
|
|
|
|
for k, v in m {
|
2020-10-06 08:12:09 +03:00
|
|
|
|
if old_oms[k].len == 0 {
|
|
|
|
|
new_oms[k] = v
|
|
|
|
|
} else {
|
|
|
|
|
new_oms[k] << old_oms[k]
|
|
|
|
|
new_oms[k] << v
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-16 14:46:12 +03:00
|
|
|
|
context.results[icmd].oms = new_oms.move()
|
2020-10-15 16:17:52 +03:00
|
|
|
|
// println('')
|
2020-10-01 23:25:29 +03:00
|
|
|
|
}
|
2020-10-01 21:06:32 +03:00
|
|
|
|
}
|
2020-10-06 08:12:09 +03:00
|
|
|
|
// create full summaries, taking account of all runs
|
2020-10-15 16:17:52 +03:00
|
|
|
|
for icmd in 0 .. context.results.len {
|
2020-10-06 08:12:09 +03:00
|
|
|
|
mut new_full_summary := map[string]Aints{}
|
2020-10-15 16:17:52 +03:00
|
|
|
|
for k, v in context.results[icmd].oms {
|
2020-12-04 20:18:58 +03:00
|
|
|
|
new_full_summary[k] = new_aints(v, context.nmins, context.nmaxs)
|
2020-10-06 08:12:09 +03:00
|
|
|
|
}
|
2021-02-16 14:46:12 +03:00
|
|
|
|
context.results[icmd].summary = new_full_summary.move()
|
2020-10-06 08:12:09 +03:00
|
|
|
|
}
|
2020-10-01 20:46:45 +03:00
|
|
|
|
}
|
2020-10-15 16:17:52 +03:00
|
|
|
|
|
2020-10-01 20:46:45 +03:00
|
|
|
|
fn (mut context Context) show_diff_summary() {
|
2020-10-15 16:17:52 +03:00
|
|
|
|
context.results.sort_with_compare(fn (a &CmdResult, b &CmdResult) int {
|
2020-10-01 23:25:29 +03:00
|
|
|
|
if a.atiming.average < b.atiming.average {
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
if a.atiming.average > b.atiming.average {
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
})
|
2020-10-02 13:28:05 +03:00
|
|
|
|
println('Summary (commands are ordered by ascending mean time), after $context.series series of $context.count repetitions:')
|
2020-10-01 23:25:29 +03:00
|
|
|
|
base := context.results[0].atiming.average
|
2020-10-02 10:57:58 +03:00
|
|
|
|
mut first_cmd_percentage := f64(100.0)
|
2020-10-01 23:25:29 +03:00
|
|
|
|
for i, r in context.results {
|
2020-10-02 10:57:58 +03:00
|
|
|
|
cpercent := (r.atiming.average / base) * 100 - 100
|
|
|
|
|
first_marker := if r.icmd == 0 { util.bold('>') } else { ' ' }
|
|
|
|
|
if r.icmd == 0 {
|
|
|
|
|
first_cmd_percentage = cpercent
|
|
|
|
|
}
|
2020-12-04 20:18:58 +03:00
|
|
|
|
println(' $first_marker${(i + 1):3} | ${cpercent:5.1f}% slower | ${r.cmd:-57s} | $r.atiming')
|
2020-10-02 10:57:58 +03:00
|
|
|
|
}
|
2020-10-09 11:06:00 +03:00
|
|
|
|
$if debugcontext ? {
|
|
|
|
|
println('context: $context')
|
|
|
|
|
}
|
|
|
|
|
if int(base) > context.fail_on_maxtime {
|
|
|
|
|
print(performance_regression_label)
|
2020-10-15 16:17:52 +03:00
|
|
|
|
println('average time: ${base:6.1f} ms > $context.fail_on_maxtime ms threshold.')
|
2020-10-09 11:06:00 +03:00
|
|
|
|
exit(2)
|
|
|
|
|
}
|
2020-10-02 10:57:58 +03:00
|
|
|
|
if context.fail_on_regress_percent == max_fail_percent || context.results.len < 2 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
fail_threshold_max := f64(context.fail_on_regress_percent)
|
|
|
|
|
if first_cmd_percentage > fail_threshold_max {
|
2020-10-09 11:06:00 +03:00
|
|
|
|
print(performance_regression_label)
|
2020-10-02 10:57:58 +03:00
|
|
|
|
println('${first_cmd_percentage:5.1f}% > ${fail_threshold_max:5.1f}% threshold.')
|
2020-10-09 11:06:00 +03:00
|
|
|
|
exit(3)
|
2020-10-01 23:25:29 +03:00
|
|
|
|
}
|
2020-10-01 20:05:27 +03:00
|
|
|
|
}
|