Last active
October 22, 2018 12:26
-
-
Save mfarrugi/e82850510673946cb7cf to your computer and use it in GitHub Desktop.
Benchmark shell scripts!
This file contains 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/python | |
import sys | |
import re | |
lines = sys.argv[1] | |
ms_thresh = int(sys.argv[2]) | |
lines = lines.split("\n") | |
lines = map(str.strip, lines) | |
events = ( (float(event[0]), event[1]) | |
for event in (tuple(line.split(" :: ")) for line in lines) ) | |
first = next(events) | |
start = first[0] | |
end = 0 | |
prev = start | |
durations = [] | |
for event in events: | |
time, label = event | |
duration = time - prev | |
prev = time | |
durations.append((duration, label)) | |
end = time | |
total = end - start | |
durations.sort(key=lambda x: x[0], reverse=True) | |
for dur, lab in durations: | |
if dur * 1000 < ms_thresh: | |
break | |
print "{:4.1f} ms {:5.2f}% :: {}".format(dur * 1000, dur / total * 100, lab) | |
print "-" * 60 | |
print "{:4.1f} ms :: {}".format(total * 1000, "Total Time") |
This file contains 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
rm -f ~/perf.txt | |
# Each call to mark took about 5ms on my machine, so it's not good for profiling anything slower. | |
function mark() { | |
echo "$(date +%s.%N) :: $1" >> ~/perf.txt | |
} | |
mark "Start" | |
# ... load stuff | |
mark "Loading" | |
# ... do stuff | |
mark "Done" | |
# Not sure why I couldn't pipe cat, but oh well. | |
python benchmark-sort.py "$(cat ~/perf.txt)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment