2022-01-04 12:21:08 +03:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2020-12-18 14:21:17 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2021-12-05 12:55:41 +03:00
|
|
|
[has_globals]
|
2020-12-18 14:21:17 +03:00
|
|
|
module util
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
2021-12-05 12:55:41 +03:00
|
|
|
__global g_timers = new_timers(should_print: false, label: 'g_timers')
|
|
|
|
|
2021-02-13 17:52:01 +03:00
|
|
|
[heap]
|
2020-12-18 14:21:17 +03:00
|
|
|
pub struct Timers {
|
2021-12-05 12:55:41 +03:00
|
|
|
label string
|
2020-12-18 18:23:38 +03:00
|
|
|
pub mut:
|
2020-12-18 14:21:17 +03:00
|
|
|
swatches map[string]time.StopWatch
|
|
|
|
should_print bool
|
2021-07-06 13:02:40 +03:00
|
|
|
// already_shown records for which of the swatches .show() or .show_if_exists() had been called already
|
|
|
|
already_shown []string
|
2020-12-18 14:21:17 +03:00
|
|
|
}
|
|
|
|
|
2021-12-05 12:55:41 +03:00
|
|
|
[params]
|
|
|
|
pub struct TimerParams {
|
|
|
|
should_print bool
|
|
|
|
label string
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_timers(params TimerParams) &Timers {
|
|
|
|
$if trace_timers_creation ? {
|
|
|
|
eprintln('>>>> new_timers, should_print: $params.should_print | label: $params.label')
|
|
|
|
}
|
2020-12-18 14:21:17 +03:00
|
|
|
return &Timers{
|
2021-12-05 12:55:41 +03:00
|
|
|
label: params.label
|
2020-12-18 14:21:17 +03:00
|
|
|
swatches: map[string]time.StopWatch{}
|
2021-12-05 12:55:41 +03:00
|
|
|
should_print: params.should_print
|
2021-10-29 16:35:58 +03:00
|
|
|
already_shown: []string{cap: 100}
|
2020-12-18 14:21:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 17:34:56 +03:00
|
|
|
pub fn get_timers() &Timers {
|
2021-12-05 12:55:41 +03:00
|
|
|
return g_timers
|
2021-02-05 17:34:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn timing_start(label string) {
|
2021-07-16 13:03:40 +03:00
|
|
|
mut t := get_timers()
|
|
|
|
t.start(label)
|
2021-02-05 17:34:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn timing_measure(label string) {
|
2021-12-05 12:55:41 +03:00
|
|
|
g_timers.show(label)
|
2021-02-05 17:34:56 +03:00
|
|
|
}
|
|
|
|
|
2021-02-24 21:03:53 +03:00
|
|
|
pub fn timing_measure_cumulative(label string) {
|
2021-12-05 12:55:41 +03:00
|
|
|
g_timers.measure_cumulative(label)
|
2021-02-24 21:03:53 +03:00
|
|
|
}
|
|
|
|
|
2021-02-05 17:34:56 +03:00
|
|
|
pub fn timing_set_should_print(should_print bool) {
|
2021-12-05 12:55:41 +03:00
|
|
|
g_timers.should_print = should_print
|
2021-02-05 17:34:56 +03:00
|
|
|
}
|
|
|
|
|
2020-12-18 14:21:17 +03:00
|
|
|
pub fn (mut t Timers) start(name string) {
|
2021-07-20 11:17:08 +03:00
|
|
|
mut sw := t.swatches[name] or { time.new_stopwatch() }
|
2021-02-24 21:03:53 +03:00
|
|
|
sw.start()
|
2020-12-18 14:21:17 +03:00
|
|
|
t.swatches[name] = sw
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut t Timers) measure(name string) i64 {
|
|
|
|
if name !in t.swatches {
|
|
|
|
timer_keys := t.swatches.keys()
|
|
|
|
eprintln('> Timer `$name` was NOT started.')
|
|
|
|
eprintln('> Available timers:')
|
|
|
|
eprintln('> $timer_keys')
|
|
|
|
}
|
2020-12-19 13:24:29 +03:00
|
|
|
ms := t.swatches[name].elapsed().microseconds()
|
2020-12-18 14:21:17 +03:00
|
|
|
return ms
|
|
|
|
}
|
|
|
|
|
2021-02-24 21:03:53 +03:00
|
|
|
pub fn (mut t Timers) measure_cumulative(name string) i64 {
|
|
|
|
ms := t.measure(name)
|
|
|
|
if name !in t.swatches {
|
|
|
|
return ms
|
|
|
|
}
|
|
|
|
mut sw := t.swatches[name]
|
|
|
|
sw.pause()
|
|
|
|
t.swatches[name] = sw
|
|
|
|
return ms
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut t Timers) measure_pause(name string) {
|
|
|
|
if name !in t.swatches {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mut sw := t.swatches[name]
|
|
|
|
sw.pause()
|
|
|
|
t.swatches[name] = sw
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut t Timers) measure_resume(name string) {
|
|
|
|
if name !in t.swatches {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mut sw := t.swatches[name]
|
|
|
|
sw.start()
|
|
|
|
t.swatches[name] = sw
|
|
|
|
}
|
|
|
|
|
2020-12-18 14:21:17 +03:00
|
|
|
pub fn (mut t Timers) message(name string) string {
|
2020-12-19 13:24:29 +03:00
|
|
|
ms := f64(t.measure(name)) / 1000.0
|
2020-12-19 13:49:28 +03:00
|
|
|
value := bold('${ms:-8.3f}')
|
|
|
|
formatted_message := '$value ms $name'
|
2020-12-18 14:21:17 +03:00
|
|
|
return formatted_message
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut t Timers) show(label string) {
|
|
|
|
formatted_message := t.message(label)
|
|
|
|
if t.should_print {
|
|
|
|
println(formatted_message)
|
|
|
|
}
|
2021-07-06 13:02:40 +03:00
|
|
|
t.already_shown << label
|
2020-12-18 14:21:17 +03:00
|
|
|
}
|
|
|
|
|
2021-02-24 21:03:53 +03:00
|
|
|
pub fn (mut t Timers) show_if_exists(label string) {
|
|
|
|
if label !in t.swatches {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.show(label)
|
2021-07-06 13:02:40 +03:00
|
|
|
t.already_shown << label
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut t Timers) show_remaining() {
|
|
|
|
for k, _ in t.swatches {
|
|
|
|
if k in t.already_shown {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t.show(k)
|
|
|
|
}
|
2021-02-24 21:03:53 +03:00
|
|
|
}
|
|
|
|
|
2020-12-18 14:21:17 +03:00
|
|
|
pub fn (mut t Timers) dump_all() {
|
|
|
|
for k, _ in t.swatches {
|
|
|
|
elapsed := t.message(k)
|
|
|
|
println(elapsed)
|
|
|
|
}
|
|
|
|
}
|