@@ -2502,7 +2502,7 @@ function time_start {
25022502 if [[ -n " $start_time " ]]; then
25032503 die $LINENO " Trying to start the clock on $name , but it's already been started"
25042504 fi
2505- _TIME_START[$name ]=$( date +%s)
2505+ _TIME_START[$name ]=$( date +%s%3N )
25062506}
25072507
25082508# time_stop $name
@@ -2523,14 +2523,57 @@ function time_stop {
25232523 if [[ -z " $start_time " ]]; then
25242524 die $LINENO " Trying to stop the clock on $name , but it was never started"
25252525 fi
2526- end_time=$( date +%s)
2526+ end_time=$( date +%s%3N )
25272527 elapsed_time=$(( $end_time - $start_time ))
25282528 total=${_TIME_TOTAL[$name]:- 0}
25292529 # reset the clock so we can start it in the future
25302530 _TIME_START[$name ]=" "
25312531 _TIME_TOTAL[$name ]=$(( $total + $elapsed_time ))
25322532}
25332533
2534+ function oscwrap {
2535+ local out
2536+ local rc
2537+ local start
2538+ local end
2539+ # Cannot use timer_start and timer_stop as we run in subshells
2540+ # and those rely on modifying vars in the same process (which cannot
2541+ # happen from a subshell.
2542+ start=$( date +%s%3N)
2543+ out=$( command openstack " $@ " )
2544+ rc=$?
2545+ end=$( date +%s%3N)
2546+ echo $(( end - start)) >> $OSCWRAP_TIMER_FILE
2547+
2548+ echo " $out "
2549+ return $rc
2550+ }
2551+
2552+ function install_oscwrap {
2553+ # File to accumulate our timing data
2554+ OSCWRAP_TIMER_FILE=$( mktemp)
2555+ # Bash by default doesn't expand aliases, allow it for the aliases
2556+ # we want to whitelist.
2557+ shopt -s expand_aliases
2558+ # Remove all aliases that might be expanded to preserve old unexpanded
2559+ # behavior
2560+ unalias -a
2561+ # Add only the alias we want for openstack
2562+ alias openstack=oscwrap
2563+ }
2564+
2565+ function cleanup_oscwrap {
2566+ local total=0
2567+ if python3_enabled ; then
2568+ local python=python3
2569+ else
2570+ local python=python
2571+ fi
2572+ total=$( cat $OSCWRAP_TIMER_FILE | $python -c " import sys; print(sum(int(l) for l in sys.stdin))" )
2573+ _TIME_TOTAL[" osc" ]=$total
2574+ rm $OSCWRAP_TIMER_FILE
2575+ }
2576+
25342577# time_totals
25352578# Print out total time summary
25362579function time_totals {
@@ -2549,6 +2592,8 @@ function time_totals {
25492592 fi
25502593 done
25512594
2595+ cleanup_oscwrap
2596+
25522597 xtrace=$( set +o | grep xtrace)
25532598 set +o xtrace
25542599
@@ -2560,6 +2605,8 @@ function time_totals {
25602605 echo
25612606 for t in ${! _TIME_TOTAL[*]} ; do
25622607 local v=${_TIME_TOTAL[$t]}
2608+ # because we're recording in milliseconds
2609+ v=$(( $v / 1000 ))
25632610 printf " %-${len} s %3d\n" " $t " " $v "
25642611 done
25652612 echo " ========================="
0 commit comments