1 if [ -z "${__stopwatch_sh_loaded}" ]
3 __stopwatch_sh_loaded=yes
5 __stopwatch_sh_VERSION="Stopwatch v0.1"
6 __stopwatch_sh_DESCRIPTION="Stopwatch implementation supporting multiple tagged timers"
7 __stopwatch_sh_COPYRIGHT=$( cat <<"END_OF_COPYRIGHT"
9 Copyright (c) 2000-2012, CyberLeo
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
15 * Redistributions of the source code must retain the above copyright
16 notice, this list of conditions, and the following disclaimer.
17 * Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions, and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20 * Neither the name of the organization nor the names of its contributors
21 may be used to endorse or promote products derived from this software
22 without specific prior written permission.
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
28 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
30 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 HOWEVER CAUSED AND ON ANY THEORY OF LIABILTY, WHETHER IN CONTRACT, STRICT
32 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 kvs="$(mktemp -t ".stopwatch.XXXXXXXX")"
40 trap "rm -f '${kvs}'" exit hup int term kill
46 ${__stopwatch_sh_VERSION}
47 ${__stopwatch_sh_DESCRIPTION}
49 ${__stopwatch_sh_COPYRIGHT}
51 Usage: stopwatch <tag> <start|lap|stop|reset>
53 Simple bourne shell based stopwatch
54 Requires 'kvs' and the id 'stopwatch' therein
55 so make sure you set 'kvs' before sourcing this script if necessary.
57 After loading, the following commands are available:
62 'Lap' a stopwatch (emit timer value without stopping)
65 Stop a stopwatch; can be resumed later with 'start'
68 Reset a stopwatch back to zero
75 # Try and format a passed integer number of seconds into something more
76 # useful (like 6m10s or something)
77 _stopwatch_timefmt() {
78 local secs mins hurs days
79 [ "${1}" -a "$(echo "${1}" | tr -Cd '0-9')" = "${1}" ] || return 255
83 [ "${secs}" -lt 60 ] || {
84 mins=$(( ${secs} / 60 ))
85 secs=$(( ${secs} % 60 ))
87 [ "${secs}" -gt 0 ] || secs=""
88 [ "${mins}" -lt 60 ] || {
89 hurs=$(( ${mins} / 60 ))
90 mins=$(( ${mins} % 60 ))
92 [ "${mins}" -gt 0 ] || mins=""
93 [ "${hurs}" -lt 24 ] || {
94 days=$(( ${hurs} / 24 ))
95 hurs=$(( ${hurs} % 24 ))
97 [ "${hurs}" -gt 0 ] || hurs=""
99 [ "${days}" -o "${hurs}" -o "${mins}" -o "${secs}" ] || printf "0s"
100 printf "%s%s%s%s" "${days:+${days}d}" "${hurs:+${hurs}h}" "${mins:+${mins}m}" "${secs:+${secs}s}"
103 # Start a named stopwatch, but do nothing if the named stopwatch is already
106 name="${1:-stopwatch}"
107 [ "${nao}" ] || nao="$(date +%s)"
108 # Is the stopwatch running?
109 if kvs_has_key stopwatch "${name}"
111 printf "Stopwatch: '%s' is already running.\n" "${name}"
113 # start the stopwatch
114 accum="$(kvs_get stopwatch "${name}_accumulator")"
115 kvs_set stopwatch "${name}" "${nao}"
116 [ "${accum}" ] && restart="$(printf " at %s" "$(_stopwatch_timefmt "${accum}")")"
117 printf "Stopwatch: '%s' starts%s.\n" "${name}" "${restart}"
121 # 'Lap' the named stopwatch: print out a line indicating how long the named
122 # stopwatch has been running, but do not stop nor reset the stopwatch.
124 name="${1:-stopwatch}"
125 [ "${nao}" ] || nao="$(date +%s)"
126 # Is the stopwatch running?
127 if kvs_has_key stopwatch "${name}"
129 # emit a line indicating how long it has been running
130 start="$(kvs_get stopwatch "${name}")"
131 accum="$(kvs_get stopwatch "${name}_accumulator")"
132 delta="$(( ${nao} - ${start} ))"
133 [ "${accum}" ] && delta="$(( ${delta} + ${accum} ))"
134 printf "Stopwatch: '%s' is running: %s.\n" "${name}" "$(_stopwatch_timefmt "${delta}")"
136 accum="$(kvs_get stopwatch "${name}_accumulator")"
137 [ "${accum}" ] && printf "Stopwatch: '%s' is stopped: %s.\n" "${name}" "$(_stopwatch_timefmt "${accum}")"
141 # Stop the named stopwatch and print out a line indicating how long it had
142 # been running up until that point; do not reset the stopwatch.
144 name="${1:-stopwatch}"
145 [ "${nao}" ] || nao="$(date +%s)"
146 if kvs_has_key stopwatch "${name}"
148 start="$(kvs_get stopwatch "${name}")"
149 accum="$(kvs_get stopwatch "${name}_accumulator")"
150 delta="$(( ${nao} - ${start} ))"
151 [ "${accum}" ] && delta="$(( ${delta} + ${accum} ))"
152 kvs_set stopwatch "${name}_accumulator" "${delta}"
153 kvs_unset stopwatch "${name}"
154 printf "Stopwatch: '%s' stops at %s.\n" "${name}" "$(_stopwatch_timefmt "${delta}")"
156 accum="$(kvs_get stopwatch "${name}_accumulator")"
157 [ "${accum}" ] && printf "Stopwatch: '%s' is stopped at %s.\n" "${name}" "$(_stopwatch_timefmt "${accum}")"
161 # Reset the named stopwatch back to zero.
163 name="${1:-stopwatch}"
164 [ "${nao}" ] || nao="$(date +%s)"
165 kvs_unset stopwatch "${name}"
166 kvs_unset stopwatch "${name}_accumulator"
167 printf "Stopwatch: '%s' is reset to 0s.\n" "${name}"
171 [ "${1}" -a "${2}" ] || stopwatch_help;
174 [Ss][Tt][Aa][Rr][Tt]) stopwatch_start "${1}" ;;
175 [Ll][Aa][Pp]) stopwatch_lap "${1}" ;;
176 [Ss][Tt][Oo][Pp]) stopwatch_stop "${1}" ;;
177 [Rr][Ee][Ss][Ee][Tt]) stopwatch_reset "${1}" ;;