| 1 | import time |
| 2 | import benchmark |
| 3 | import term |
| 4 | |
| 5 | fn test_measure() { |
| 6 | mut b := benchmark.start() |
| 7 | time.sleep(200 * time.millisecond) |
| 8 | x := b.measure('sleeping') // returns microseconds |
| 9 | flush_stdout() |
| 10 | assert x > 150_000 |
| 11 | // assert x < 800_000 // this can be much longer on a slow CI runner |
| 12 | } |
| 13 | |
| 14 | fn test_record_measure() { |
| 15 | mut b := benchmark.start() |
| 16 | println('step 1') |
| 17 | flush_stdout() |
| 18 | time.sleep(100 * time.millisecond) |
| 19 | x := b.record_measure('sleeping 1') |
| 20 | assert x > 50_000 |
| 21 | // assert x < 200_000 |
| 22 | flush_stdout() |
| 23 | |
| 24 | println('step 2') |
| 25 | flush_stdout() |
| 26 | time.sleep(150 * time.millisecond) |
| 27 | y := b.record_measure('sleeping 2') |
| 28 | assert y > 100_000 |
| 29 | // assert y < 200_000 |
| 30 | flush_stdout() |
| 31 | |
| 32 | res := b.all_recorded_measures() |
| 33 | println('All recorded measurements:') |
| 34 | println(res) |
| 35 | flush_stdout() |
| 36 | assert res.contains('ms in sleeping 1') |
| 37 | assert res.contains('ms in sleeping 1') |
| 38 | assert res.contains('SPENT') |
| 39 | } |
| 40 | |
| 41 | fn test_total_message() { |
| 42 | mut b := benchmark.start() |
| 43 | for _ in 0 .. 100 { |
| 44 | time.sleep(time.millisecond) |
| 45 | x := b.record_measure('sleeping 1') |
| 46 | assert x > 1_000 |
| 47 | } |
| 48 | |
| 49 | res := b.total_message('sleeping 1') |
| 50 | println(res) |
| 51 | |
| 52 | assert res.contains(' Min: ') |
| 53 | assert res.contains(' Max: ') |
| 54 | assert res.contains(' Avg: ') |
| 55 | |
| 56 | time.sleep(time.millisecond) |
| 57 | y := b.record_measure('sleeping 2') |
| 58 | assert y > 1_000 |
| 59 | // Should not contain min max avg, insufficient information |
| 60 | res2 := b.total_message('sleeping 2') |
| 61 | |
| 62 | assert !res2.contains(' Min: ') |
| 63 | assert !res2.contains(' Max: ') |
| 64 | assert !res2.contains(' Avg: ') |
| 65 | } |
| 66 | |
| 67 | fn test_step_message_handles_ansi_label_longer_than_label_width() { |
| 68 | mut b := benchmark.start() |
| 69 | ansi_fail_label := '\x1b[41m\x1b[1m\x1b[37m FAIL \x1b[39m\x1b[22m\x1b[49m' |
| 70 | long_cmd := 'command: /nix/store/' + 'x'.repeat(90) + '-vlang/build/source/v -o v_g -g cmd/v' |
| 71 | message := b.step_message_with_label(ansi_fail_label, long_cmd) |
| 72 | |
| 73 | assert term.strip_ansi(message).contains('FAIL') |
| 74 | assert message.contains(long_cmd) |
| 75 | } |
| 76 | |