1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

v.util.timers: improve tracing by supporting -d trace_timers_creation

This commit is contained in:
Delyan Angelov
2021-12-05 11:55:41 +02:00
parent 2754368873
commit 229d2fb667
5 changed files with 30 additions and 18 deletions

View File

@@ -1,12 +1,16 @@
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
[has_globals]
module util
import time
__global g_timers = new_timers(should_print: false, label: 'g_timers')
[heap]
pub struct Timers {
label string
pub mut:
swatches map[string]time.StopWatch
should_print bool
@@ -14,18 +18,26 @@ pub mut:
already_shown []string
}
pub fn new_timers(should_print bool) &Timers {
[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')
}
return &Timers{
label: params.label
swatches: map[string]time.StopWatch{}
should_print: should_print
should_print: params.should_print
already_shown: []string{cap: 100}
}
}
const timers = new_timers(false)
pub fn get_timers() &Timers {
return util.timers
return g_timers
}
pub fn timing_start(label string) {
@@ -34,18 +46,15 @@ pub fn timing_start(label string) {
}
pub fn timing_measure(label string) {
mut t := get_timers()
t.show(label)
g_timers.show(label)
}
pub fn timing_measure_cumulative(label string) {
mut t := get_timers()
t.measure_cumulative(label)
g_timers.measure_cumulative(label)
}
pub fn timing_set_should_print(should_print bool) {
mut t := util.timers
t.should_print = should_print
g_timers.should_print = should_print
}
pub fn (mut t Timers) start(name string) {