Created
April 15, 2015 02:25
-
-
Save scoates/fc90bcb604eceea43a35 to your computer and use it in GitHub Desktop.
Measure the time delta between lines in stdout
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/env python | |
import time | |
from sys import stdin, stdout, argv, exit | |
try: | |
if argv[1] == 'help' or argv[1] == '--help' or argv[1] == '-h': | |
print "%s: times each line of stdin." % argv[0] | |
print " Optional parameter is a float of a threshold. (defaults to 2.0)" | |
exit(255); | |
threshold = argv[1] | |
except IndexError: | |
threshold = 2.0 | |
timeline_last = time.time() | |
while 1: | |
try: | |
line = stdin.readline() | |
except KeyboardInterrupt: | |
break | |
if not line: | |
break | |
diff = time.time() - timeline_last | |
if diff > threshold: | |
indicator = '!' | |
else: | |
indicator = ' ' | |
print "[%s%0.2f] %s" % (indicator, diff, line.rstrip()) | |
timeline_last = time.time() | |
stdout.flush() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment