2020-05-01 09:20:53 +02:00
|
|
|
import time
|
|
|
|
|
2020-05-01 10:58:47 +02:00
|
|
|
// NB: 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() {
|
2020-06-01 17:24:38 +02:00
|
|
|
mut sw := time.new_stopwatch({})
|
2020-05-01 09:20:53 +02:00
|
|
|
// sample code that you want to measure:
|
|
|
|
println('Hello world')
|
|
|
|
time.sleep_ms(1)
|
|
|
|
//
|
|
|
|
println('Greeting the world took: ${sw.elapsed().nanoseconds()}ns')
|
|
|
|
assert sw.elapsed().nanoseconds() > 0
|
|
|
|
}
|
|
|
|
|
2020-05-01 10:58:47 +02:00
|
|
|
fn test_stopwatch_time_between_pause_and_start_should_be_skipped_in_elapsed() {
|
2020-05-30 09:20:54 +02:00
|
|
|
println('Testing pause function')
|
2020-06-01 17:24:38 +02:00
|
|
|
mut sw := time.new_stopwatch({})
|
2020-05-01 09:20:53 +02:00
|
|
|
time.sleep_ms(10) // A
|
2020-05-30 09:20:54 +02:00
|
|
|
eprintln('Elapsed after 10ms nap: ${sw.elapsed().milliseconds()}ms')
|
2020-05-01 09:20:53 +02:00
|
|
|
assert sw.elapsed().milliseconds() >= 10
|
|
|
|
sw.pause()
|
|
|
|
time.sleep_ms(10)
|
2020-05-30 09:20:54 +02:00
|
|
|
eprintln('Elapsed after pause and another 10ms nap: ${sw.elapsed().milliseconds()}ms')
|
2020-05-01 09:20:53 +02:00
|
|
|
assert sw.elapsed().milliseconds() >= 10
|
2020-05-01 11:01:30 +02:00
|
|
|
$if stopwatch ? {
|
2020-05-01 10:58:47 +02:00
|
|
|
assert sw.elapsed().milliseconds() < 20
|
|
|
|
}
|
2020-05-01 09:20:53 +02:00
|
|
|
sw.start()
|
|
|
|
time.sleep_ms(10) // B
|
2020-05-30 09:20:54 +02:00
|
|
|
eprintln('Elapsed after resume and another 10ms nap: ${sw.elapsed().milliseconds()}ms')
|
2020-05-01 09:20:53 +02:00
|
|
|
assert sw.elapsed().milliseconds() >= 20
|
2020-05-01 11:01:30 +02:00
|
|
|
$if stopwatch ? {
|
2020-05-01 10:58:47 +02:00
|
|
|
assert sw.elapsed().milliseconds() < 30
|
|
|
|
}
|
2020-05-01 09:20:53 +02:00
|
|
|
}
|