src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/eval/builtin_fn_time_test.elvts (about) 1 ///////// 2 # sleep # 3 ///////// 4 5 //mock-time-after 6 7 ## number with no unit ## 8 ~> sleep 0 9 slept for 0s 10 ~> sleep 1 11 slept for 1s 12 ~> sleep 0.1 13 slept for 100ms 14 ~> sleep (num 0) 15 slept for 0s 16 ~> sleep (num 42) 17 slept for 42s 18 ~> sleep (num 1.7) 19 slept for 1.7s 20 21 ## number with unit ## 22 ~> sleep 1.3s 23 slept for 1.3s 24 ~> sleep 0.1ms 25 slept for 100µs 26 ~> sleep 3h5m7s 27 slept for 3h5m7s 28 29 ## valid durations ## 30 ~> sleep 1x 31 Exception: invalid sleep duration 32 [tty]:1:1-8: sleep 1x 33 ~> sleep -7 34 Exception: sleep duration must be >= zero 35 [tty]:1:1-8: sleep -7 36 ~> sleep -3h 37 Exception: sleep duration must be >= zero 38 [tty]:1:1-9: sleep -3h 39 ~> sleep 1/2 40 slept for 500ms 41 ~> sleep (num -7) 42 Exception: sleep duration must be >= zero 43 [tty]:1:1-14: sleep (num -7) 44 ~> sleep [1] 45 Exception: invalid sleep duration 46 [tty]:1:1-9: sleep [1] 47 48 ## can be interrupted ## 49 //inject-time-after-with-sigint-or-skip 50 ~> sleep 1s 51 Exception: interrupted 52 [tty]:1:1-8: sleep 1s 53 54 //////// 55 # time # 56 //////// 57 58 // Since runtime duration is non-deterministic, we only have some sanity checks 59 // here. 60 61 ~> time { echo foo } | var out time = (all) 62 put $out 63 ▶ foo 64 65 ## &on-end ## 66 ~> var duration = '' 67 time &on-end={|x| set duration = $x } { echo foo } | var out = (all) 68 put $out 69 kind-of $duration 70 ▶ foo 71 ▶ number 72 73 ## propagating exception ## 74 ~> time { fail body } | nop (all) 75 Exception: body 76 [tty]:1:8-17: time { fail body } | nop (all) 77 [tty]:1:1-19: time { fail body } | nop (all) 78 79 ## propagating exception from &on-end ## 80 ~> time &on-end={|_| fail on-end } { } 81 Exception: on-end 82 [tty]:1:19-30: time &on-end={|_| fail on-end } { } 83 [tty]:1:1-35: time &on-end={|_| fail on-end } { } 84 85 ## exception from body takes precedence ## 86 ~> time &on-end={|_| fail on-end } { fail body } 87 Exception: body 88 [tty]:1:35-44: time &on-end={|_| fail on-end } { fail body } 89 [tty]:1:1-45: time &on-end={|_| fail on-end } { fail body } 90 91 ## bubbling output error ## 92 ~> time { } >&- 93 Exception: invalid argument 94 [tty]:1:1-12: time { } >&- 95 96 ///////////// 97 # benchmark # 98 ///////////// 99 100 // These steps depend on the implementation detail that benchmark calls time.Now 101 // once before a run and once after a run. 102 103 ## default output ## 104 //mock-benchmark-run-durations 1 2 105 ~> benchmark &min-runs=2 &min-time=2s { } 106 1.5s ± 500ms (min 1s, max 2s, 2 runs) 107 108 ## &on-end ## 109 //mock-benchmark-run-durations 1 2 110 ~> benchmark &min-runs=2 &min-time=2s &on-end=$put~ { } 111 ▶ [&avg=(num 1.5) &max=(num 2.0) &min=(num 1.0) &runs=(num 2) &stddev=(num 0.5)] 112 113 ## &min-runs determining number of runs ## 114 //mock-benchmark-run-durations 1 2 1 2 115 ~> benchmark &min-runs=4 &min-time=0s &on-end={|m| put $m[runs]} { } 116 ▶ (num 4) 117 118 ## &min-time determining number of runs ## 119 //mock-benchmark-run-durations 1 5 5 120 ~> benchmark &min-runs=0 &min-time=10s &on-end={|m| put $m[runs]} { } 121 ▶ (num 3) 122 123 ## &on-run-end ## 124 //mock-benchmark-run-durations 1 2 1 125 ~> benchmark &min-runs=3 &on-run-end=$put~ &on-end={|m| } { } 126 ▶ (num 1.0) 127 ▶ (num 2.0) 128 ▶ (num 1.0) 129 130 ## body throws exception ## 131 //mock-benchmark-run-durations 1 2 1 132 ~> var i = 0 133 benchmark { set i = (+ $i 1); if (== $i 3) { fail failure } } 134 1.5s ± 500ms (min 1s, max 2s, 2 runs) 135 Exception: failure 136 [tty]:2:46-58: benchmark { set i = (+ $i 1); if (== $i 3) { fail failure } } 137 [tty]:2:1-61: benchmark { set i = (+ $i 1); if (== $i 3) { fail failure } } 138 139 ## body throws exception on first run ## 140 ~> benchmark { fail failure } 141 Exception: failure 142 [tty]:1:13-25: benchmark { fail failure } 143 [tty]:1:1-26: benchmark { fail failure } 144 ~> benchmark &on-end=$put~ { fail failure } 145 Exception: failure 146 [tty]:1:27-39: benchmark &on-end=$put~ { fail failure } 147 [tty]:1:1-40: benchmark &on-end=$put~ { fail failure } 148 149 ## &on-run-end throws exception ## 150 //mock-benchmark-run-durations 1 151 ~> benchmark &on-run-end={|_| fail failure } { } 152 1s ± 0s (min 1s, max 1s, 1 runs) 153 Exception: failure 154 [tty]:1:28-40: benchmark &on-run-end={|_| fail failure } { } 155 [tty]:1:1-45: benchmark &on-run-end={|_| fail failure } { } 156 157 ## &on-end throws exception ## 158 ~> benchmark &min-runs=2 &min-time=0s &on-end={|_| fail failure } { } 159 Exception: failure 160 [tty]:1:49-61: benchmark &min-runs=2 &min-time=0s &on-end={|_| fail failure } { } 161 [tty]:1:1-66: benchmark &min-runs=2 &min-time=0s &on-end={|_| fail failure } { } 162 163 ## option errors ## 164 ~> benchmark &min-runs=-1 { } 165 Exception: bad value: min-runs option must be non-negative integer, but is -1 166 [tty]:1:1-26: benchmark &min-runs=-1 { } 167 ~> benchmark &min-time=abc { } 168 Exception: bad value: min-time option must be duration string, but is abc 169 [tty]:1:1-27: benchmark &min-time=abc { } 170 ~> benchmark &min-time=-1s { } 171 Exception: bad value: min-time option must be non-negative duration, but is -1s 172 [tty]:1:1-27: benchmark &min-time=-1s { } 173 174 ## bubbling output error ## 175 //mock-benchmark-run-durations 1 176 ~> benchmark &min-runs=0 &min-time=0s { } >&- 177 Exception: invalid argument 178 [tty]:1:1-42: benchmark &min-runs=0 &min-time=0s { } >&-