tools: add v time, to measure command execution time, in a cross platform way, without relying on other external tools

This commit is contained in:
Delyan Angelov 2024-12-22 11:45:20 +02:00
parent 63fff1dcd4
commit 2eb2f9f554
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
3 changed files with 50 additions and 0 deletions

39
cmd/tools/vtime.v Normal file
View File

@ -0,0 +1,39 @@
import os
import term
import time
import flag
struct Context {
mut:
show_help bool
cmd_line_opts []string
}
fn main() {
mut ctx := Context{}
args := arguments()
mut fp := flag.new_flag_parser(args#[1..])
fp.application('v time')
fp.version('0.0.1')
fp.description('Start a command, and report how much time it took to run, and what its exit code was.')
fp.arguments_description('CMD [ARGS]')
fp.skip_executable()
fp.limit_free_args_to_at_least(1)!
ctx.show_help = fp.bool('help', `h`, false, 'Show this help screen.')
if ctx.show_help {
println(fp.usage())
exit(0)
}
ctx.cmd_line_opts = fp.finalize() or {
eprintln('Error: ${err}')
exit(1)
}
cmd := ctx.cmd_line_opts.join(' ')
sw := time.new_stopwatch()
ecode := os.system(cmd)
elapsed := sw.elapsed()
stook_time := '${f64(elapsed.microseconds()) / 1000.0:8.3f} ms'
eprintln('> ${term.ecolorize(term.bright_yellow, stook_time)}. Exit code: ${ecode:3}. Command: ${term.ecolorize(term.green,
cmd)}')
exit(ecode)
}

View File

@ -47,6 +47,7 @@ const external_tools = [
'test-fmt',
'test-parser',
'test-self',
'time',
'tracev',
'up',
'vet',

View File

@ -0,0 +1,10 @@
v time 0.0.1
-----------------------------------------------
Usage: v time [options] CMD [ARGS]
Description: Start a command, and report how much time it took to run, and what its exit code was.
The arguments should be at least 1 in number.
Options:
-h, --help Show this help screen.