]> CyberLeo.Net >> Repos - CDN/shlib.git/blob - lib/sh/stopwatch.sh
sh/stopwatch: rename and expose stopwatch_humany_duration as unstable API
[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" 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 Internal functions available:
72
73 stopwatch_humany_duration <usec>
74   Transform an integer number of microseconds into a human-readable string
75   (Ex: 3d14h10m3.223s or 7m10.013s )
76
77 EOF
78     kill -ABRT $$
79   }
80
81   case "$(uname -s)" in
82   Linux)
83     _stopwatch_usec() {
84       echo $(( $(date +%s%N) / 1000 ))
85     }
86     ;;
87   FreeBSD)
88     _stopwatch_usec() {
89       "${_root}/lib/sh/usec" || echo $(( $(date +%s) * 1000000 ))
90     }
91     ;;
92   *)
93     echo "Unsupported platform: $(uname -s)" >&2
94     exit 1
95     ;;
96   esac
97
98   # Try and format a passed integer number of seconds into something more
99   # easily parseable by a human (like 6m10s or something)
100   stopwatch_humany_duration() {
101     local usec secs mins hurs days
102     [ "${1}" -a "$(echo "${1}" | tr -Cd '0-9')" = "${1}" ] || return 255
103     usec=$(( ${1} % 1000000 ))
104     secs=$(( ${1} / 1000000 ))
105     mins=0
106     hurs=0
107     [ "${secs}" -lt 60 ] || {
108       mins=$(( ${secs} / 60 ))
109       secs=$(( ${secs} % 60 ))
110     }
111     [ "${mins}" -lt 60 ] || {
112       hurs=$(( ${mins} / 60 ))
113       mins=$(( ${mins} % 60 ))
114     }
115     [ "${mins}" -gt 0 ] || mins=""
116     [ "${hurs}" -lt 24 ] || {
117       days=$(( ${hurs} / 24 ))
118       hurs=$(( ${hurs} % 24 ))
119     }
120     [ "${hurs}" -gt 0 ] || hurs=""
121
122     printf "%s%s%s%s.%03ds" "${days:+${days}d}" "${hurs:+${hurs}h}" "${mins:+${mins}m}" "${secs}" "$(( ${usec} / 1000 ))"
123   }
124
125   # Start a named stopwatch, but do nothing if the named stopwatch is already
126   # running.
127   stopwatch_start() {
128     name="${1:-stopwatch}"
129     [ "${nao}" ] || nao="$(_stopwatch_usec)"
130     # Is the stopwatch running?
131     if kvs_has_key stopwatch "${name}"
132     then
133       printf "Stopwatch: '%s' is already running.\n" "${name}"
134     else
135       # start the stopwatch
136       accum="$(kvs_get stopwatch "${name}_accumulator")"
137       kvs_set stopwatch "${name}" "${nao}"
138       [ "${accum}" ] && restart="$(printf " at %s" "$(stopwatch_humany_duration "${accum}")")"
139       printf "Stopwatch: '%s' starts%s.\n" "${name}" "${restart}"
140     fi
141   }
142
143   # 'Lap' the named stopwatch: print out a line indicating how long the named
144   # stopwatch has been running, but do not stop nor reset the stopwatch.
145   stopwatch_lap() {
146     name="${1:-stopwatch}"
147     [ "${nao}" ] || nao="$(_stopwatch_usec)"
148     # Is the stopwatch running?
149     if kvs_has_key stopwatch "${name}"
150     then
151       # emit a line indicating how long it has been running
152       start="$(kvs_get stopwatch "${name}")"
153       accum="$(kvs_get stopwatch "${name}_accumulator")"
154       delta="$(( ${nao} - ${start} ))"
155       [ "${accum}" ] && delta="$(( ${delta} + ${accum} ))"
156       printf "Stopwatch: '%s' is running: %s.\n" "${name}" "$(stopwatch_humany_duration "${delta}")"
157     else
158       accum="$(kvs_get stopwatch "${name}_accumulator")"
159       [ "${accum}" ] && printf "Stopwatch: '%s' is stopped: %s.\n" "${name}" "$(stopwatch_humany_duration "${accum}")"
160     fi
161   }
162
163   # Stop the named stopwatch and print out a line indicating how long it had
164   # been running up until that point; do not reset the stopwatch.
165   stopwatch_stop() {
166     name="${1:-stopwatch}"
167     [ "${nao}" ] || nao="$(_stopwatch_usec)"
168     if kvs_has_key stopwatch "${name}"
169     then
170       start="$(kvs_get stopwatch "${name}")"
171       accum="$(kvs_get stopwatch "${name}_accumulator")"
172       delta="$(( ${nao} - ${start} ))"
173       [ "${accum}" ] && delta="$(( ${delta} + ${accum} ))"
174       kvs_set stopwatch "${name}_accumulator" "${delta}"
175       kvs_unset stopwatch "${name}"
176       printf "Stopwatch: '%s' stops at %s.\n" "${name}" "$(stopwatch_humany_duration "${delta}")"
177     else
178       accum="$(kvs_get stopwatch "${name}_accumulator")"
179       [ "${accum}" ] && printf "Stopwatch: '%s' is stopped at %s.\n" "${name}" "$(stopwatch_humany_duration "${accum}")"
180     fi
181   }
182
183   # Reset the named stopwatch back to zero.
184   stopwatch_reset() {
185     name="${1:-stopwatch}"
186     [ "${nao}" ] || nao="$(_stopwatch_usec)"
187     kvs_unset stopwatch "${name}"
188     kvs_unset stopwatch "${name}_accumulator"
189     printf "Stopwatch: '%s' is reset to 0s.\n" "${name}"
190   }
191
192   stopwatch() {
193     [ "${1}" -a "${2}" ] || stopwatch_help;
194     nao="$(_stopwatch_usec)"
195     case "${2}" in
196     [Ss][Tt][Aa][Rr][Tt])  stopwatch_start "${1}"  ;;
197     [Ll][Aa][Pp])          stopwatch_lap "${1}"    ;;
198     [Ss][Tt][Oo][Pp])      stopwatch_stop "${1}"   ;;
199     [Rr][Ee][Ss][Ee][Tt])  stopwatch_reset "${1}"  ;;
200     *)                     stopwatch_help          ;;
201     esac
202   }
203 fi