tools: improve the output of v repeat command, by coloring the faster/slower label

This commit is contained in:
Delyan Angelov 2024-08-23 09:59:35 +03:00
parent 971feb8f89
commit 89d0743fd6
No known key found for this signature in database
GPG key ID: 66886C0F12D595ED

View file

@ -3,8 +3,21 @@ module main
import os import os
import flag import flag
import time import time
import term
import math import math
import term
const tred = term.red
const tbold = term.bold
const tgray = term.gray
const tcyan = term.cyan
const tgreen = term.green
const tbcyan = term.bright_cyan
const tbblue = term.bright_blue
const tdivider = term.h_divider
fn c(cfn fn (string) string, s string) string {
return term.colorize(cfn, s)
}
const max_fail_percent = 100 * 1000 const max_fail_percent = 100 * 1000
const max_time = 60 * 1000 // ms const max_time = 60 * 1000 // ms
@ -104,15 +117,15 @@ fn new_aints(ovals []i64, extreme_mins int, extreme_maxs int) Aints {
} }
fn bold(s string) string { fn bold(s string) string {
return term.colorize(term.green, term.colorize(term.bold, s)) return c(tgreen, c(tbold, s))
} }
fn (a Aints) str() string { fn (a Aints) str() string {
avg := bold('${a.average / 1000:5.1f}ms') avg := bold('${a.average / 1000:5.1f}ms')
tdev := term.colorize(term.red, '${a.stddev / 1000:5.1f}ms') tdev := c(tred, '${a.stddev / 1000:5.1f}ms')
baseline := '${avg} ± σ: ${tdev},' baseline := '${avg} ± σ: ${tdev},'
tmin := term.colorize(term.bright_cyan, '${f64(a.imin) / 1000:5.1f}ms') tmin := c(tbcyan, '${f64(a.imin) / 1000:5.1f}ms')
tmax := term.colorize(term.bright_blue, '${f64(a.imax) / 1000:5.1f}ms') tmax := c(tbblue, '${f64(a.imax) / 1000:5.1f}ms')
return '${baseline:-46s} ${tmin}${tmax}' return '${baseline:-46s} ${tmin}${tmax}'
} }
@ -166,7 +179,7 @@ fn (mut context Context) run() {
mut duration := i64(0) mut duration := i64(0)
mut oldres := '' mut oldres := ''
series_label := '${icmd + 1}/${context.commands.len}, ${si + 1}/${context.series}' series_label := '${icmd + 1}/${context.commands.len}, ${si + 1}/${context.series}'
line_prefix := '${context.cgoback}Command: ${term.colorize(term.gray, cmd)}, ${series_label:9}' line_prefix := '${context.cgoback}Command: ${c(tgray, cmd)}, ${series_label:9}'
if context.series != 1 || context.commands.len != 1 { if context.series != 1 || context.commands.len != 1 {
flushed_print(line_prefix) flushed_print(line_prefix)
} }
@ -198,7 +211,8 @@ fn (mut context Context) run() {
sum += duration sum += duration
runs++ runs++
avg = (f64(sum) / f64(i + 1)) avg = (f64(sum) / f64(i + 1))
flushed_print('${line_prefix}, current average: ${avg / 1000:9.3f}ms, run ${i + 1:4}/${context.run_count:-4} took ${f64(duration) / 1000:6} ms') cavg := '${avg / 1000:9.3f}ms'
flushed_print('${line_prefix}, current average: ${c(tgreen, cavg)}, run ${i + 1:4}/${context.run_count:-4} took ${f64(duration) / 1000:6} ms')
if context.show_output { if context.show_output {
flushed_print(' | result: ${oldres:s}') flushed_print(' | result: ${oldres:s}')
} }
@ -288,8 +302,8 @@ fn (mut context Context) show_diff_summary() {
mut first_cmd_percentage := f64(100.0) mut first_cmd_percentage := f64(100.0)
mut first_marker := '' mut first_marker := ''
if context.results.len == 1 { if context.results.len == 1 {
context.show_summary_title('${context.results[0].atiming}, ${context.series} series, ${context.run_count} runs for ${term.colorize(term.green, gcmd := c(tgreen, context.results[0].cmd)
context.results[0].cmd):-57s}') context.show_summary_title('${context.results[0].atiming}, ${context.series} series, ${context.run_count} runs for ${gcmd:-57s}')
} else { } else {
context.show_summary_title('Summary after ${context.series} series x ${context.run_count} runs (%s are relative to first command, or `base`).') context.show_summary_title('Summary after ${context.series} series x ${context.run_count} runs (%s are relative to first command, or `base`).')
for i, r in context.results { for i, r in context.results {
@ -306,13 +320,13 @@ fn (mut context Context) show_diff_summary() {
mut tcomparison := 'base ' mut tcomparison := 'base '
if r.atiming.average != base { if r.atiming.average != base {
if r.atiming.average < base { if r.atiming.average < base {
tcomparison = '${base / r.atiming.average:4.2f}x faster' tcomparison = '${base / r.atiming.average:4.2f}x ${c(tgreen, 'faster')}'
} else { } else {
tcomparison = '${r.atiming.average / base:4.2f}x slower' tcomparison = '${r.atiming.average / base:4.2f}x ${c(tcyan, 'slower')}'
} }
} }
println(' ${first_marker}${(i + 1):3} ${comparison:7} ${tcomparison:5} ${r.atiming} `${term.colorize(term.green, gcmd := c(tgreen, r.cmd)
r.cmd)}`') println(' ${first_marker}${(i + 1):3} ${comparison:7} ${tcomparison:5} ${r.atiming} `${gcmd}`')
} }
} }
$if debugcontext ? { $if debugcontext ? {
@ -390,7 +404,7 @@ fn (mut context Context) parse_options() ! {
context.cline = '\n' context.cline = '\n'
context.cgoback = '\n' context.cgoback = '\n'
} else { } else {
context.cline = '\r' + term.h_divider('') + '\r' context.cline = '\r' + tdivider('') + '\r'
context.cgoback = '\r' context.cgoback = '\r'
} }
} }