|
1 | 1 | #!/bin/bash
|
2 | 2 |
|
3 | 3 | function clock::now() {
|
4 |
| - if perl -MTime::HiRes -e "" > /dev/null 2>&1; then |
5 |
| - perl -MTime::HiRes -e 'printf("%.0f\n", Time::HiRes::time() * 1000)' |
6 |
| - elif [[ "${_OS:-}" != "OSX" ]]; then |
7 |
| - date +%s%N |
8 |
| - else |
9 |
| - echo "" |
| 4 | + if dependencies::has_perl && perl -MTime::HiRes -e "" > /dev/null 2>&1; then |
| 5 | + if perl -MTime::HiRes -e 'printf("%.0f\n",Time::HiRes::time()*1000000000)'; then |
| 6 | + return 0 |
| 7 | + fi |
| 8 | + fi |
| 9 | + |
| 10 | + if check_os::is_windows && dependencies::has_powershell; then |
| 11 | + powershell -Command " |
| 12 | + \$unixEpoch = [DateTime]'1970-01-01 00:00:00'; |
| 13 | + \$now = [DateTime]::UtcNow; |
| 14 | + \$ticksSinceEpoch = (\$now - \$unixEpoch).Ticks; |
| 15 | + \$nanosecondsSinceEpoch = \$ticksSinceEpoch * 100; |
| 16 | + Write-Output \$nanosecondsSinceEpoch |
| 17 | + " |
| 18 | + return 0 |
| 19 | + fi |
| 20 | + |
| 21 | + if ! check_os::is_macos && ! check_os::is_alpine; then |
| 22 | + local result |
| 23 | + result=$(date +%s%N) |
| 24 | + if [[ "$result" != *N ]] && [[ "$result" -gt 0 ]]; then |
| 25 | + echo "$result" |
| 26 | + return 0 |
| 27 | + fi |
10 | 28 | fi
|
| 29 | + |
| 30 | + local shell_time has_shell_time |
| 31 | + shell_time="$(clock::shell_time)" |
| 32 | + has_shell_time="$?" |
| 33 | + if [[ "$has_shell_time" -eq 0 ]]; then |
| 34 | + local seconds microseconds |
| 35 | + seconds=$(echo "$shell_time" | cut -f 1 -d '.') |
| 36 | + microseconds=$(echo "$shell_time" | cut -f 2 -d '.') |
| 37 | + |
| 38 | + math::calculate "($seconds * 1000000000) + ($microseconds * 1000)" |
| 39 | + return 0 |
| 40 | + fi |
| 41 | + |
| 42 | + echo "" |
| 43 | + return 1 |
| 44 | +} |
| 45 | + |
| 46 | +function clock::shell_time() { |
| 47 | + # Get time directly from the shell rather than a program. |
| 48 | + [[ -n ${EPOCHREALTIME+x} && -n "$EPOCHREALTIME" ]] && LC_ALL=C echo "$EPOCHREALTIME" |
11 | 49 | }
|
12 | 50 |
|
13 |
| -_START_TIME=$(clock::now) |
14 | 51 |
|
15 | 52 | function clock::total_runtime_in_milliseconds() {
|
16 | 53 | end_time=$(clock::now)
|
17 | 54 | if [[ -n $end_time ]]; then
|
18 |
| - echo $(( end_time - _START_TIME )) |
| 55 | + math::calculate "($end_time-$_START_TIME)/1000000" |
| 56 | + else |
| 57 | + echo "" |
| 58 | + fi |
| 59 | +} |
| 60 | + |
| 61 | +function clock::total_runtime_in_nanoseconds() { |
| 62 | + end_time=$(clock::now) |
| 63 | + if [[ -n $end_time ]]; then |
| 64 | + math::calculate "($end_time-$_START_TIME)" |
19 | 65 | else
|
20 | 66 | echo ""
|
21 | 67 | fi
|
22 | 68 | }
|
| 69 | + |
| 70 | +function clock::init() { |
| 71 | + _START_TIME=$(clock::now) |
| 72 | +} |
0 commit comments