diff options
author | Ben Sima <ben@bensima.com> | 2025-04-29 22:20:48 -0400 |
---|---|---|
committer | Ben Sima <ben@bensima.com> | 2025-04-29 22:20:48 -0400 |
commit | d4a9d5b0b3a3d26fb283c6a1318061bf97c90bbd (patch) | |
tree | 9f2b9d284e32fb24606bf23997bbeb9a9d5354f6 | |
parent | 041be01116c8ab8c41d15f16b2ef71c1c949e7a9 (diff) |
add reactiontime script
-rwxr-xr-x | reactiontime | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/reactiontime b/reactiontime new file mode 100755 index 0000000..292845e --- /dev/null +++ b/reactiontime @@ -0,0 +1,40 @@ +#!/usr/bin/env python +import random +import datetime +import time +import sys +import pathlib + + +def as_millis(s): + return s*1000 + + +def take_sample(): + waittime = random.randint(0,5) + time.sleep(waittime) + start=datetime.datetime.now() + sys.stderr.write("GO") + sys.stderr.flush() + input() + end=datetime.datetime.now() + delta = end - start + return as_millis(delta.total_seconds()) + + +def record_metric(avg, log: pathlib.Path): + now = datetime.datetime.now() + with log.open('a+', encoding='utf-8') as f: + f.write(f"{now}\t{avg}\n") + f.flush() + + +if __name__ == "__main__": + samples = [] + n = 5 + print("Press Enter when you see GO") + for i in range(0, n): + samples.append(take_sample()) + avg = sum(samples)/n + record_metric(avg, pathlib.Path("~/org/reactiontime.log").expanduser()) + print(avg) |