| 1 | module benchmark |
| 2 | |
| 3 | import time |
| 4 | |
| 5 | // if n == 0, n predict == 1 |
| 6 | fn test_predict_n_zero() { |
| 7 | mut b := Benchmark{ |
| 8 | n: 0 |
| 9 | duration: 0 |
| 10 | bench_time: time.second |
| 11 | bench_func: fn () ! {} |
| 12 | } |
| 13 | expected := 1 |
| 14 | println(b.predict_n()) |
| 15 | assert b.predict_n() == expected |
| 16 | } |
| 17 | |
| 18 | // n can't be more 1000000000 |
| 19 | fn test_predict_n_limit() { |
| 20 | mut b := Benchmark{ |
| 21 | n: 10000000000 |
| 22 | duration: 0 |
| 23 | bench_time: time.second |
| 24 | bench_func: fn () ! {} |
| 25 | } |
| 26 | expected := 1000000000 |
| 27 | assert b.predict_n() == expected |
| 28 | } |
| 29 | |
| 30 | // test prediction for slow bench function |
| 31 | fn test_slow_fn() { |
| 32 | mut b := Benchmark{ |
| 33 | duration: time.second |
| 34 | bench_func: fn () ! {} |
| 35 | } |
| 36 | assert b.predict_n() == 1 |
| 37 | } |
| 38 | |
| 39 | // if bench_func cause error set failed true, n = 1 |
| 40 | fn test_fn_with_error() { |
| 41 | f := fn () ! { |
| 42 | return error('error') |
| 43 | } |
| 44 | mut bench := setup(f) or { |
| 45 | eprintln('Error creating benchmark: ${err}') |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | bench.run() |
| 50 | |
| 51 | assert bench.failed == true |
| 52 | assert bench.benchmark_result.n == 1 |
| 53 | } |
| 54 | |
| 55 | fn test_n_must_be_over_1() { |
| 56 | f := fn () ! { |
| 57 | mut i := 0 |
| 58 | i++ |
| 59 | } |
| 60 | mut bench := setup(f) or { |
| 61 | eprintln('Error creating benchmark: ${err}') |
| 62 | return |
| 63 | } |
| 64 | |
| 65 | bench.run() |
| 66 | |
| 67 | assert bench.benchmark_result.n > 1 |
| 68 | } |
| 69 | |
| 70 | fn test_n() { |
| 71 | f := fn () ! { |
| 72 | mut i := 0 |
| 73 | i++ |
| 74 | } |
| 75 | mut bench := setup(f, BenchmarkDefaults{ |
| 76 | n: 1000 |
| 77 | }) or { |
| 78 | eprintln('Error creating benchmark: ${err}') |
| 79 | return |
| 80 | } |
| 81 | |
| 82 | bench.run() |
| 83 | |
| 84 | assert bench.benchmark_result.n == 1000 |
| 85 | } |
| 86 | |
| 87 | fn test_max_bench_time() { |
| 88 | f := fn () ! { |
| 89 | time.sleep(500 * time.millisecond) |
| 90 | } |
| 91 | mut bench := setup(f) or { |
| 92 | eprintln('Error creating benchmark: ${err}') |
| 93 | return |
| 94 | } |
| 95 | |
| 96 | bench.run() |
| 97 | |
| 98 | assert bench.benchmark_result.n == 3 |
| 99 | assert bench.benchmark_result.t >= time.second |
| 100 | } |
| 101 | |
| 102 | fn test_performance() { |
| 103 | scheduler := [func_1, func_2, func_3] |
| 104 | expected := [false, false, false] |
| 105 | mut actual := []bool{} |
| 106 | |
| 107 | for i in scheduler { |
| 108 | mut bench := setup(i) or { |
| 109 | eprintln('Error creating benchmark: ${err}') |
| 110 | return |
| 111 | } |
| 112 | |
| 113 | bench.run() |
| 114 | actual << bench.failed |
| 115 | } |
| 116 | |
| 117 | assert expected.len == actual.len |
| 118 | for i := 0; i < expected.len; i++ { |
| 119 | assert expected[i] == actual[i] |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | fn func_1() ! { |
| 124 | mut arr := []int{} |
| 125 | appender(mut arr) |
| 126 | assert arr.len == 10 |
| 127 | } |
| 128 | |
| 129 | fn appender(mut arr []int) { |
| 130 | if arr.len == 10 { |
| 131 | return |
| 132 | } |
| 133 | arr << 1 |
| 134 | appender(mut arr) |
| 135 | } |
| 136 | |
| 137 | fn func_2() ! { |
| 138 | target := 2 |
| 139 | arr := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 140 | |
| 141 | mut left := 0 |
| 142 | mut right := arr.len - 1 |
| 143 | |
| 144 | for left <= right { |
| 145 | mid := left + (right - left) / 2 |
| 146 | if arr[mid] == target { |
| 147 | return |
| 148 | } |
| 149 | if arr[mid] < target { |
| 150 | left = mid + 1 |
| 151 | } |
| 152 | if arr[mid] > target { |
| 153 | right = mid - 1 |
| 154 | } |
| 155 | } |
| 156 | return |
| 157 | } |
| 158 | |
| 159 | fn func_3() ! { |
| 160 | mut arr := [10, 2, 13, 4, 5, 16, 7, 1, 9, 20] |
| 161 | |
| 162 | for i := 0; i < arr.len - 1; i++ { |
| 163 | for j := 0; j < arr.len - i - 1; j++ { |
| 164 | if arr[j] > arr[j + 1] { |
| 165 | arr[j], arr[j + 1] = arr[j + 1], arr[j] |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |