Last active
August 29, 2015 14:01
-
-
Save Khady/14870ad9968b59708d42 to your computer and use it in GitHub Desktop.
Fix the usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env bash | |
## tests_hammer.bash for onitu | |
## by lenormf | |
## | |
function log { | |
echo "$@" 1>&2 | |
} | |
function fatal { | |
log "$@" | |
exit 1 | |
} | |
function main { | |
test $# -lt 2 && fatal "Usage: $0 <n> <cmd>" | |
local N="$1" | |
shift | |
local CMD="$@" | |
local min_time=0 | |
local max_time=0 | |
local sum_time=0 | |
[[ "$N" =~ [0-9]+ ]] || fatal "${N} isn't a number" | |
for i in $(seq "$N"); do | |
local NOW="$SECONDS" | |
echo "Test #${i}" | |
eval "${CMD}" | |
local ret=$? | |
local spent=$((SECONDS - NOW)) | |
test "$min_time" -eq 0 && min_time="$spent" | |
test "$max_time" -eq 0 && max_time="$spent" | |
test $ret -eq 0 && { | |
sum_time=$((sum_time + spent)) | |
test "$spent" -lt "$min_time" && min_time="$spent" | |
test "$spent" -gt "$max_time" && max_time="$spent" | |
continue | |
} || { | |
log "Test #${i} failed" | |
break | |
} | |
done | |
test $i -gt 1 && { | |
log "Average time:" $((sum_time / $((i - 1))))s | |
log "Extrema:" "${min_time}s" "/" "${max_time}s" | |
} || { | |
log "Time:" "$min_time"s | |
} | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment