-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathassert_duration.sh
More file actions
84 lines (63 loc) · 2.22 KB
/
assert_duration.sh
File metadata and controls
84 lines (63 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env bash
function bashunit::duration::measure_ms() {
local command="$1"
local start_ns
start_ns=$(bashunit::clock::now)
eval "$command" >/dev/null 2>&1
local end_ns
end_ns=$(bashunit::clock::now)
local elapsed_ms
elapsed_ms=$(bashunit::math::calculate "($end_ns - $start_ns) / 1000000" | awk '{printf "%.0f", $1}')
echo "$elapsed_ms"
}
function assert_duration() {
bashunit::assert::should_skip && return 0
local command="$1"
local threshold_ms="$2"
local elapsed_ms
elapsed_ms=$(bashunit::duration::measure_ms "$command")
if [ "$elapsed_ms" -gt "$threshold_ms" ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${threshold_ms}" "to complete within (ms)" "${command}"
return
fi
bashunit::state::add_assertions_passed
}
function assert_duration_less_than() {
bashunit::assert::should_skip && return 0
local command="$1"
local threshold_ms="$2"
local elapsed_ms
elapsed_ms=$(bashunit::duration::measure_ms "$command")
if [ "$elapsed_ms" -ge "$threshold_ms" ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${threshold_ms}" "to complete within (ms)" "${command}"
return
fi
bashunit::state::add_assertions_passed
}
function assert_duration_greater_than() {
bashunit::assert::should_skip && return 0
local command="$1"
local threshold_ms="$2"
local elapsed_ms
elapsed_ms=$(bashunit::duration::measure_ms "$command")
if [ "$elapsed_ms" -le "$threshold_ms" ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${threshold_ms}" "to take at least (ms)" "${command}"
return
fi
bashunit::state::add_assertions_passed
}