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" 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
71 Internal functions available:
73 stopwatch_humany_duration <usec>
74 Transform an integer number of microseconds into a human-readable string
75 (Ex: 3d14h10m3.223s or 7m10.013s )
77 stopwatch_set_message_callback <command>
78 Register a callback to format event messages emitted when a stopwatch is
79 commanded. The callback will receive the following parameters:
80 <command> - one of: starting, started, lap, get, stopping, stopped, reset
81 <tag> - stopwatch name
82 <time> - the current time on the stopwatch, if any
83 The commands are as follows:
84 starting - called start while stopped
85 started - called start while running
86 lap - called lap while running
87 get - called lap while stopped
88 stopping - called stop while running
89 stopped - called stop while stopped
99 echo $(( $(date +%s%N) / 1000 ))
104 "${_root}/lib/sh/usec" || echo $(( $(date +%s) * 1000000 ))
108 echo "Unsupported platform: $(uname -s)" >&2
113 # Format stopwatch event messages (Can be overridden; see
114 # 'stopwatch_set_message_callback')
115 _stopwatch_message_callback() {
118 time="${3:+$(stopwatch_humany_duration ${3})}"
121 starting) printf "'%s' starts%s.\n" "${tag}" "${time:+ at ${time}}";;
122 started) printf "'%s' is already running.\n" "${tag}" ;;
123 lap) printf "'%s' is running: %s\n" "${tag}" "${time}" ;;
124 get) printf "'%s' is stopped: %s\n" "${tag}" "${time}" ;;
125 stopping) printf "'%s' stops at %s.\n" "${tag}" "${time}" ;;
126 stopped) printf "'%s' is stopped%s.\n" "${tag}" "${time:+ at ${time}}" ;;
127 reset) printf "'%s' is reset to zero.\n" "${tag}" ;;
128 *) printf "No message for command %s\n" "${command}" ;;
132 # Set the message callback
133 stopwatch_set_message_callback() {
134 _stopwatch_registered_message_callback="${1:-_stopwatch_message_callback}"
135 kvs_set stopwatch message_callback "${_stopwatch_registered_message_callback}"
138 # Fetch the message callback; default if unset
139 stopwatch_get_message_callback() {
140 [ "${_stopwatch_registered_message_callback}" ] ||
141 _stopwatch_registered_message_callback="$(kvs_get stopwatch messsage_callback)"
142 if [ "${_stopwatch_registered_message_callback}" ]
144 echo "${_stopwatch_registered_message_callback}"
146 _stopwatch_registered_message_callback=_stopwatch_message_callback
147 echo _stopwatch_message_callback
151 # Try and format a passed integer number of seconds into something more
152 # easily parseable by a human (like 6m10s or something)
153 stopwatch_humany_duration() {
154 local usec secs mins hurs days
155 [ "${1}" -a "$(echo "${1}" | tr -Cd '0-9')" = "${1}" ] || return 255
156 usec=$(( ${1} % 1000000 ))
157 secs=$(( ${1} / 1000000 ))
160 [ "${secs}" -lt 60 ] || {
161 mins=$(( ${secs} / 60 ))
162 secs=$(( ${secs} % 60 ))
164 [ "${mins}" -lt 60 ] || {
165 hurs=$(( ${mins} / 60 ))
166 mins=$(( ${mins} % 60 ))
168 [ "${mins}" -gt 0 ] || mins=""
169 [ "${hurs}" -lt 24 ] || {
170 days=$(( ${hurs} / 24 ))
171 hurs=$(( ${hurs} % 24 ))
173 [ "${hurs}" -gt 0 ] || hurs=""
175 printf "%s%s%s%s.%03ds" "${days:+${days}d}" "${hurs:+${hurs}h}" "${mins:+${mins}m}" "${secs}" "$(( ${usec} / 1000 ))"
178 # Start a named stopwatch, but do nothing if the named stopwatch is already
181 name="${1:-stopwatch}"
182 [ "${nao}" ] || nao="$(_stopwatch_usec)"
183 # Is the stopwatch running?
184 if kvs_has_key stopwatch "${name}"
186 eval "$(stopwatch_get_message_callback) started ${name}"
188 # start the stopwatch
189 accum="$(kvs_get stopwatch "${name}_accumulator")"
190 kvs_set stopwatch "${name}" "${nao}"
191 eval "$(stopwatch_get_message_callback) starting ${name} ${accum}"
195 # 'Lap' the named stopwatch: print out a line indicating how long the named
196 # stopwatch has been running, but do not stop nor reset the stopwatch.
198 name="${1:-stopwatch}"
199 [ "${nao}" ] || nao="$(_stopwatch_usec)"
200 # Is the stopwatch running?
201 if kvs_has_key stopwatch "${name}"
203 # emit a line indicating how long it has been running
204 start="$(kvs_get stopwatch "${name}")"
205 accum="$(kvs_get stopwatch "${name}_accumulator")"
206 delta="$(( ${nao} - ${start} ))"
207 [ "${accum}" ] && delta="$(( ${delta} + ${accum} ))"
208 eval "$(stopwatch_get_message_callback) lap ${name} ${delta}"
210 accum="$(kvs_get stopwatch "${name}_accumulator")"
211 eval "$(stopwatch_get_message_callback) get ${name} ${accum}"
215 # Stop the named stopwatch and print out a line indicating how long it had
216 # been running up until that point; do not reset the stopwatch.
218 name="${1:-stopwatch}"
219 [ "${nao}" ] || nao="$(_stopwatch_usec)"
220 if kvs_has_key stopwatch "${name}"
222 start="$(kvs_get stopwatch "${name}")"
223 accum="$(kvs_get stopwatch "${name}_accumulator")"
224 delta="$(( ${nao} - ${start} ))"
225 [ "${accum}" ] && delta="$(( ${delta} + ${accum} ))"
226 kvs_set stopwatch "${name}_accumulator" "${delta}"
227 kvs_unset stopwatch "${name}"
228 eval "$(stopwatch_get_message_callback) stopping ${name} ${delta}"
230 accum="$(kvs_get stopwatch "${name}_accumulator")"
231 eval "$(stopwatch_get_message_callback) stopped ${name} ${accum}"
235 # Reset the named stopwatch back to zero.
237 name="${1:-stopwatch}"
238 [ "${nao}" ] || nao="$(_stopwatch_usec)"
239 kvs_unset stopwatch "${name}"
240 kvs_unset stopwatch "${name}_accumulator"
241 eval "$(stopwatch_get_message_callback) reset ${name}"
245 [ "${1}" -a "${2}" ] || stopwatch_help;
246 nao="$(_stopwatch_usec)"
248 [Ss][Tt][Aa][Rr][Tt]) stopwatch_start "${1}" ;;
249 [Ll][Aa][Pp]) stopwatch_lap "${1}" ;;
250 [Ss][Tt][Oo][Pp]) stopwatch_stop "${1}" ;;
251 [Rr][Ee][Ss][Ee][Tt]) stopwatch_reset "${1}" ;;