]> CyberLeo.Net >> Repos - CDN/shlib.git/blob - lib/sh/stopwatch.sh
sh/stopwatch: bump stopwatch resolution to microseconds when possible
[CDN/shlib.git] / lib / sh / stopwatch.sh
1 if [ -z "${__stopwatch_sh_loaded}" ]
2 then
3   __stopwatch_sh_loaded=yes
4
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"
8
9 Copyright (c) 2000-2012, CyberLeo
10 All rights reserved.
11
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14
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.
23
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.
34
35 END_OF_COPYRIGHT
36 )
37
38   [ "${kvs}" ] || {
39     kvs="$(mktemp -t ".stopwatch.XXXXXXXX")"
40     trap "rm -f '${kvs}'" exit hup int term kill
41   }
42   want kvs
43
44   stopwatch_help() {
45     cat <<EOF >&2
46 ${__stopwatch_sh_VERSION}
47 ${__stopwatch_sh_DESCRIPTION}
48
49 ${__stopwatch_sh_COPYRIGHT}
50
51 Usage: stopwatch <tag> <start|lap|stop|reset>
52
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.
56
57 After loading, the following commands are available:
58
59 Start a stopwatch
60   stopwatch <tag> start
61
62 'Lap' a stopwatch (emit timer value without stopping)
63   stopwatch <tag> lap
64
65 Stop a stopwatch; can be resumed later with 'start'
66   stopwatch <tag> stop
67
68 Reset a stopwatch back to zero
69   stopwatch <tag> reset
70
71 EOF
72     kill -ABRT $$
73   }
74
75   case "$(uname -s)" in
76   Linux)
77     _stopwatch_usec() {
78       echo $(( $(date +%s%N) / 1000 ))
79     }
80     ;;
81   FreeBSD)
82     _stopwatch_usec() {
83       "${_root}/lib/sh/usec" || echo $(( $(date +%s) * 1000000 ))
84     }
85     ;;
86   *)
87     echo "Unsupported platform: $(uname -s)" >&2
88     exit 1
89     ;;
90   esac
91
92   # Try and format a passed integer number of seconds into something more
93   # useful (like 6m10s or something)
94   _stopwatch_timefmt() {
95     local usec secs mins hurs days
96     [ "${1}" -a "$(echo "${1}" | tr -Cd '0-9')" = "${1}" ] || return 255
97     usec=$(( ${1} % 1000000 ))
98     secs=$(( ${1} / 1000000 ))
99     mins=0
100     hurs=0
101     [ "${secs}" -lt 60 ] || {
102       mins=$(( ${secs} / 60 ))
103       secs=$(( ${secs} % 60 ))
104     }
105     [ "${mins}" -lt 60 ] || {
106       hurs=$(( ${mins} / 60 ))
107       mins=$(( ${mins} % 60 ))
108     }
109     [ "${mins}" -gt 0 ] || mins=""
110     [ "${hurs}" -lt 24 ] || {
111       days=$(( ${hurs} / 24 ))
112       hurs=$(( ${hurs} % 24 ))
113     }
114     [ "${hurs}" -gt 0 ] || hurs=""
115
116     printf "%s%s%s%s.%03ds" "${days:+${days}d}" "${hurs:+${hurs}h}" "${mins:+${mins}m}" "${secs}" "$(( ${usec} / 1000 ))"
117   }
118
119   # Start a named stopwatch, but do nothing if the named stopwatch is already
120   # running.
121   stopwatch_start() {
122     name="${1:-stopwatch}"
123     [ "${nao}" ] || nao="$(_stopwatch_usec)"
124     # Is the stopwatch running?
125     if kvs_has_key stopwatch "${name}"
126     then
127       printf "Stopwatch: '%s' is already running.\n" "${name}"
128     else
129       # start the stopwatch
130       accum="$(kvs_get stopwatch "${name}_accumulator")"
131       kvs_set stopwatch "${name}" "${nao}"
132       [ "${accum}" ] && restart="$(printf " at %s" "$(_stopwatch_timefmt "${accum}")")"
133       printf "Stopwatch: '%s' starts%s.\n" "${name}" "${restart}"
134     fi
135   }
136
137   # 'Lap' the named stopwatch: print out a line indicating how long the named
138   # stopwatch has been running, but do not stop nor reset the stopwatch.
139   stopwatch_lap() {
140     name="${1:-stopwatch}"
141     [ "${nao}" ] || nao="$(_stopwatch_usec)"
142     # Is the stopwatch running?
143     if kvs_has_key stopwatch "${name}"
144     then
145       # emit a line indicating how long it has been running
146       start="$(kvs_get stopwatch "${name}")"
147       accum="$(kvs_get stopwatch "${name}_accumulator")"
148       delta="$(( ${nao} - ${start} ))"
149       [ "${accum}" ] && delta="$(( ${delta} + ${accum} ))"
150       printf "Stopwatch: '%s' is running: %s.\n" "${name}" "$(_stopwatch_timefmt "${delta}")"
151     else
152       accum="$(kvs_get stopwatch "${name}_accumulator")"
153       [ "${accum}" ] && printf "Stopwatch: '%s' is stopped: %s.\n" "${name}" "$(_stopwatch_timefmt "${accum}")"
154     fi
155   }
156
157   # Stop the named stopwatch and print out a line indicating how long it had
158   # been running up until that point; do not reset the stopwatch.
159   stopwatch_stop() {
160     name="${1:-stopwatch}"
161     [ "${nao}" ] || nao="$(_stopwatch_usec)"
162     if kvs_has_key stopwatch "${name}"
163     then
164       start="$(kvs_get stopwatch "${name}")"
165       accum="$(kvs_get stopwatch "${name}_accumulator")"
166       delta="$(( ${nao} - ${start} ))"
167       [ "${accum}" ] && delta="$(( ${delta} + ${accum} ))"
168       kvs_set stopwatch "${name}_accumulator" "${delta}"
169       kvs_unset stopwatch "${name}"
170       printf "Stopwatch: '%s' stops at %s.\n" "${name}" "$(_stopwatch_timefmt "${delta}")"
171     else
172       accum="$(kvs_get stopwatch "${name}_accumulator")"
173       [ "${accum}" ] && printf "Stopwatch: '%s' is stopped at %s.\n" "${name}" "$(_stopwatch_timefmt "${accum}")"
174     fi
175   }
176
177   # Reset the named stopwatch back to zero.
178   stopwatch_reset() {
179     name="${1:-stopwatch}"
180     [ "${nao}" ] || nao="$(_stopwatch_usec)"
181     kvs_unset stopwatch "${name}"
182     kvs_unset stopwatch "${name}_accumulator"
183     printf "Stopwatch: '%s' is reset to 0s.\n" "${name}"
184   }
185
186   stopwatch() {
187     [ "${1}" -a "${2}" ] || stopwatch_help;
188     nao="$(_stopwatch_usec)"
189     case "${2}" in
190     [Ss][Tt][Aa][Rr][Tt])  stopwatch_start "${1}"  ;;
191     [Ll][Aa][Pp])          stopwatch_lap "${1}"    ;;
192     [Ss][Tt][Oo][Pp])      stopwatch_stop "${1}"   ;;
193     [Rr][Ee][Ss][Ee][Tt])  stopwatch_reset "${1}"  ;;
194     *)                     stopwatch_help          ;;
195     esac
196   }
197 fi