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