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

37 lines
1.3 KiB
V
Raw Permalink Normal View History

2020-05-01 10:20:53 +03:00
import time
// Note: on CI jobs, especially msvc ones, sleep_ms may sleep for much more
// 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() {
mut sw := time.new_stopwatch()
2020-05-01 10:20:53 +03:00
// sample code that you want to measure:
println('Hello world')
2021-02-27 20:41:06 +03:00
time.sleep(1 * time.millisecond)
2020-05-01 10:20:53 +03:00
//
println('Greeting the world took: ${sw.elapsed().nanoseconds()}ns')
assert sw.elapsed().nanoseconds() > 0
}
fn test_stopwatch_time_between_pause_and_start_should_be_skipped_in_elapsed() {
println('Testing pause function')
mut sw := time.new_stopwatch()
2021-02-27 20:41:06 +03:00
time.sleep(10 * time.millisecond) // A
eprintln('Elapsed after 10ms nap: ${sw.elapsed().milliseconds()}ms')
assert sw.elapsed().milliseconds() >= 8 // sometimes it sleeps for 9ms on windows..
2020-05-01 10:20:53 +03:00
sw.pause()
2021-02-27 20:41:06 +03:00
time.sleep(10 * time.millisecond)
eprintln('Elapsed after pause and another 10ms nap: ${sw.elapsed().milliseconds()}ms')
assert sw.elapsed().milliseconds() >= 8
2020-05-01 12:01:30 +03:00
$if stopwatch ? {
assert sw.elapsed().milliseconds() < 20
}
2020-05-01 10:20:53 +03:00
sw.start()
2021-02-27 20:41:06 +03:00
time.sleep(10 * time.millisecond) // B
eprintln('Elapsed after resume and another 10ms nap: ${sw.elapsed().milliseconds()}ms')
assert sw.elapsed().milliseconds() >= 18
2020-05-01 12:01:30 +03:00
$if stopwatch ? {
assert sw.elapsed().milliseconds() < 30
}
2020-05-01 10:20:53 +03:00
}