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

stopwatch: auto_start option, make pause cumulative

This commit is contained in:
JalonSolov
2020-05-30 03:20:54 -04:00
committed by GitHub
parent 077e06b44e
commit b7dc5b2f7b
7 changed files with 52 additions and 36 deletions

View File

@@ -4,7 +4,7 @@ import time
// time than you have specified. To avoid false positives from CI test
// failures, some of the asserts will be run only if you pass `-d stopwatch`
fn test_stopwatch_works_as_intended() {
sw := time.new_stopwatch()
sw := time.new_stopwatch({})
// sample code that you want to measure:
println('Hello world')
time.sleep_ms(1)
@@ -14,20 +14,21 @@ fn test_stopwatch_works_as_intended() {
}
fn test_stopwatch_time_between_pause_and_start_should_be_skipped_in_elapsed() {
sw := time.new_stopwatch()
println('Testing pause function')
sw := time.new_stopwatch({})
time.sleep_ms(10) // A
// eprintln('${sw.elapsed().milliseconds()}ms')
eprintln('Elapsed after 10ms nap: ${sw.elapsed().milliseconds()}ms')
assert sw.elapsed().milliseconds() >= 10
sw.pause()
time.sleep_ms(10)
// eprintln('${sw.elapsed().milliseconds()}ms')
eprintln('Elapsed after pause and another 10ms nap: ${sw.elapsed().milliseconds()}ms')
assert sw.elapsed().milliseconds() >= 10
$if stopwatch ? {
assert sw.elapsed().milliseconds() < 20
}
sw.start()
time.sleep_ms(10) // B
// Here, sw.elapsed() should be ~10ms (from A) + ~10ms (from B) >= 20ms
eprintln('Elapsed after resume and another 10ms nap: ${sw.elapsed().milliseconds()}ms')
assert sw.elapsed().milliseconds() >= 20
$if stopwatch ? {
assert sw.elapsed().milliseconds() < 30