From 13ab725e5aba64f151021a3a26c0b21809f3597f Mon Sep 17 00:00:00 2001 From: CyberLeo Date: Thu, 17 Jan 2013 00:31:16 -0600 Subject: [PATCH] sh/stopwatch: Add 'time' verb to stopwatch, to retrieve just the times For externally formatted messages and such. --- lib/sh/stopwatch.sh | 47 ++++++++++++++++++++++++++++++++++++--------- test/stopwatch.sh | 12 +++++++++++- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/lib/sh/stopwatch.sh b/lib/sh/stopwatch.sh index 29844e2..3121c92 100644 --- a/lib/sh/stopwatch.sh +++ b/lib/sh/stopwatch.sh @@ -68,6 +68,9 @@ Stop a stopwatch; can be resumed later with 'start' Reset a stopwatch back to zero stopwatch reset +Return the bare time, and delta (if any) since last start + stopwatch time + Internal functions available: stopwatch_humany_duration @@ -89,6 +92,7 @@ stopwatch_set_message_callback stopping - called stop while running stopped - called stop while stopped reset - called reset + time - called to return only the formatted time strings EOF kill -ABRT $$ @@ -118,16 +122,16 @@ EOF tag="${2}" time="${3:+$(stopwatch_humany_duration ${3})}" delta="${4:+$(stopwatch_humany_duration ${4})}" - printf "Stopwatch: " case "${command}" in - starting) printf "'%s' starts%s.\n" "${tag}" "${time:+ at ${time}}";; - started) printf "'%s' is already running.\n" "${tag}" ;; - lap) printf "'%s' is running: %s%s\n" "${tag}" "${time}" "${delta:+ (${delta} since last lap)}" ;; - get) printf "'%s' is stopped: %s\n" "${tag}" "${time}" ;; - stopping) printf "'%s' stops at %s%s.\n" "${tag}" "${time}" "${delta:+ (${delta} since last start)}" ;; - stopped) printf "'%s' is stopped%s.\n" "${tag}" "${time:+ at ${time}}" ;; - reset) printf "'%s' is reset to zero.\n" "${tag}" ;; - *) printf "No message for command %s\n" "${command}" ;; + starting) printf "Stopwatch: '%s' starts%s.\n" "${tag}" "${time:+ at ${time}}";; + started) printf "Stopwatch: '%s' is already running.\n" "${tag}" ;; + lap) printf "Stopwatch: '%s' is running: %s%s\n" "${tag}" "${time}" "${delta:+ (${delta} since last lap)}" ;; + get) printf "Stopwatch: '%s' is stopped: %s\n" "${tag}" "${time}" ;; + stopping) printf "Stopwatch: '%s' stops at %s%s.\n" "${tag}" "${time}" "${delta:+ (${delta} since last start)}" ;; + stopped) printf "Stopwatch: '%s' is stopped%s.\n" "${tag}" "${time:+ at ${time}}" ;; + reset) printf "Stopwatch: '%s' is reset to zero.\n" "${tag}" ;; + time) printf "%s %s\n" "${time}" "${delta}" ;; + *) printf "Stopwatch: No message for command %s\n" "${command}" ;; esac } @@ -250,6 +254,30 @@ EOF eval "$(stopwatch_get_message_callback) reset ${name}" } + # Emit the current time and delta + stopwatch_time() { + name="${1:-stopwatch}" + [ "${nao}" ] || nao="$(_stopwatch_usec)" + if kvs_has_key stopwatch "${name}" + then + # It's running + start="$(kvs_get stopwatch "${name}")" + accum="$(kvs_get stopwatch "${name}_accumulator")" + delta="$(( ${nao} - ${start} ))" + diffs="${delta}" + [ "${accum}" ] && delta="$(( ${delta} + ${accum} ))" + eval "$(stopwatch_get_message_callback) time ${name} ${delta} ${diffs}" + elif kvs_has_key stopwatch "${name}_accumulator" + then + # It's stopped + accum="$(kvs_get stopwatch "${name}_accumulator")" + eval "$(stopwatch_get_message_callback) time ${name} ${accum} 0" + else + # It's nonexistent + eval "$(stopwatch_get_message_callback) time ${name} 0 0" + fi + } + stopwatch() { [ "${1}" -a "${2}" ] || stopwatch_help; nao="$(_stopwatch_usec)" @@ -258,6 +286,7 @@ EOF [Ll][Aa][Pp]) stopwatch_lap "${1}" ;; [Ss][Tt][Oo][Pp]) stopwatch_stop "${1}" ;; [Rr][Ee][Ss][Ee][Tt]) stopwatch_reset "${1}" ;; + [Tt][Ii][Mm][Ee]) stopwatch_time "${1}" ;; *) stopwatch_help ;; esac } diff --git a/test/stopwatch.sh b/test/stopwatch.sh index 466ce61..141e8e1 100644 --- a/test/stopwatch.sh +++ b/test/stopwatch.sh @@ -27,23 +27,33 @@ echo stopwatch test stop stopwatch test reset +stopwatch test time stopwatch test start +stopwatch test time sleep 5 stopwatch test lap +stopwatch test time sleep 5 stopwatch test lap +stopwatch test time sleep 5 stopwatch test stop +stopwatch test time sleep 5 stopwatch test start +stopwatch test time sleep 5 stopwatch test lap +stopwatch test time sleep 5 stopwatch test lap +stopwatch test time sleep 5 stopwatch test stop +stopwatch test time sleep 5 stopwatch test reset +stopwatch test time sleep 5 stopwatch test stop - +stopwatch test time -- 2.42.0