From 58b83cd155417ba3830a5f806cdd56b200531610 Mon Sep 17 00:00:00 2001 From: CyberLeo Date: Fri, 28 Dec 2012 16:43:19 -0600 Subject: [PATCH] sh/stopwatch: bump stopwatch resolution to microseconds when possible --- lib/sh/stopwatch.sh | 36 ++++++++++++++++++++++++++---------- test/stopwatch.sh | 15 +++++++-------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/lib/sh/stopwatch.sh b/lib/sh/stopwatch.sh index ee05e6d..591bc4e 100644 --- a/lib/sh/stopwatch.sh +++ b/lib/sh/stopwatch.sh @@ -72,19 +72,36 @@ EOF kill -ABRT $$ } + case "$(uname -s)" in + Linux) + _stopwatch_usec() { + echo $(( $(date +%s%N) / 1000 )) + } + ;; + FreeBSD) + _stopwatch_usec() { + "${_root}/lib/sh/usec" || echo $(( $(date +%s) * 1000000 )) + } + ;; + *) + echo "Unsupported platform: $(uname -s)" >&2 + exit 1 + ;; + esac + # Try and format a passed integer number of seconds into something more # useful (like 6m10s or something) _stopwatch_timefmt() { - local secs mins hurs days + local usec secs mins hurs days [ "${1}" -a "$(echo "${1}" | tr -Cd '0-9')" = "${1}" ] || return 255 - secs="${1}" + usec=$(( ${1} % 1000000 )) + secs=$(( ${1} / 1000000 )) mins=0 hurs=0 [ "${secs}" -lt 60 ] || { mins=$(( ${secs} / 60 )) secs=$(( ${secs} % 60 )) } - [ "${secs}" -gt 0 ] || secs="" [ "${mins}" -lt 60 ] || { hurs=$(( ${mins} / 60 )) mins=$(( ${mins} % 60 )) @@ -96,15 +113,14 @@ EOF } [ "${hurs}" -gt 0 ] || hurs="" - [ "${days}" -o "${hurs}" -o "${mins}" -o "${secs}" ] || printf "0s" - printf "%s%s%s%s" "${days:+${days}d}" "${hurs:+${hurs}h}" "${mins:+${mins}m}" "${secs:+${secs}s}" + printf "%s%s%s%s.%03ds" "${days:+${days}d}" "${hurs:+${hurs}h}" "${mins:+${mins}m}" "${secs}" "$(( ${usec} / 1000 ))" } # Start a named stopwatch, but do nothing if the named stopwatch is already # running. stopwatch_start() { name="${1:-stopwatch}" - [ "${nao}" ] || nao="$(date +%s)" + [ "${nao}" ] || nao="$(_stopwatch_usec)" # Is the stopwatch running? if kvs_has_key stopwatch "${name}" then @@ -122,7 +138,7 @@ EOF # stopwatch has been running, but do not stop nor reset the stopwatch. stopwatch_lap() { name="${1:-stopwatch}" - [ "${nao}" ] || nao="$(date +%s)" + [ "${nao}" ] || nao="$(_stopwatch_usec)" # Is the stopwatch running? if kvs_has_key stopwatch "${name}" then @@ -142,7 +158,7 @@ EOF # been running up until that point; do not reset the stopwatch. stopwatch_stop() { name="${1:-stopwatch}" - [ "${nao}" ] || nao="$(date +%s)" + [ "${nao}" ] || nao="$(_stopwatch_usec)" if kvs_has_key stopwatch "${name}" then start="$(kvs_get stopwatch "${name}")" @@ -161,7 +177,7 @@ EOF # Reset the named stopwatch back to zero. stopwatch_reset() { name="${1:-stopwatch}" - [ "${nao}" ] || nao="$(date +%s)" + [ "${nao}" ] || nao="$(_stopwatch_usec)" kvs_unset stopwatch "${name}" kvs_unset stopwatch "${name}_accumulator" printf "Stopwatch: '%s' is reset to 0s.\n" "${name}" @@ -169,7 +185,7 @@ EOF stopwatch() { [ "${1}" -a "${2}" ] || stopwatch_help; - nao="$(date +%s)" + nao="$(_stopwatch_usec)" case "${2}" in [Ss][Tt][Aa][Rr][Tt]) stopwatch_start "${1}" ;; [Ll][Aa][Pp]) stopwatch_lap "${1}" ;; diff --git a/test/stopwatch.sh b/test/stopwatch.sh index 3c7b4d8..1eb242a 100644 --- a/test/stopwatch.sh +++ b/test/stopwatch.sh @@ -1,29 +1,28 @@ #!/bin/sh _root="$(readlink -f "$(dirname "${0}")/../")"; . "${_root}/lib/sh/env.sh" - want stopwatch stopwatch test start -_stopwatch_timefmt 10 +_stopwatch_timefmt 10000000 echo stopwatch test lap -_stopwatch_timefmt 30 +_stopwatch_timefmt 30000000 echo stopwatch test lap -_stopwatch_timefmt 59 +_stopwatch_timefmt 59000000 echo stopwatch test lap -_stopwatch_timefmt 60 +_stopwatch_timefmt 60000000 echo stopwatch test lap -_stopwatch_timefmt 61 +_stopwatch_timefmt 61000000 echo stopwatch test lap -_stopwatch_timefmt 86399 +_stopwatch_timefmt 86399000000 echo stopwatch test lap -_stopwatch_timefmt 99999999 +_stopwatch_timefmt 99999999000000 echo stopwatch test stop stopwatch test reset -- 2.42.0