Created
July 4, 2018 07:53
-
-
Save bulletmark/bb256875b8ba6b49874e398078e1ca89 to your computer and use it in GitHub Desktop.
Program to benchmark set of checksum commands
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/python3 | |
'Program to benchmark set of checksum commands.' | |
# Author: M.Blakeney, Jul 2018. | |
import os, argparse, time, tempfile, subprocess, statistics | |
import collections, platform | |
# Default set of commands to execute | |
CMDS = 'md5sum,sha1sum,sha256sum' | |
# Default functions to use for time calc and average | |
TIMEFUNC = time.perf_counter | |
AVEFUNC = statistics.median | |
# Process command line options | |
opt = argparse.ArgumentParser(description=__doc__.strip()) | |
opt.add_argument('-c', '--count', type=int, default=5, | |
help='number of times to run each command, default=%(default)d') | |
opt.add_argument('-m', '--mb', type=int, default=100, | |
help='MB size of file to use, default=%(default)d') | |
opt.add_argument('--cmds', default=CMDS, | |
help='set of commands to run, default="%(default)s"') | |
args = opt.parse_args() | |
# Create temp file with random content of required size | |
fp = tempfile.NamedTemporaryFile() | |
fp.write(os.urandom(args.mb * 1024 * 1024)) | |
fp.flush() | |
cmds = [cmd.strip() for cmd in args.cmds.split(',')] | |
results = collections.defaultdict(list) | |
# Do the sample runs | |
for count in range(args.count): | |
for cmd in cmds: | |
tstart = TIMEFUNC() | |
subprocess.run([cmd, fp.name], stdout=subprocess.DEVNULL) | |
results[cmd].append(TIMEFUNC() - tstart) | |
# Calculate a representative time from lists of sample run results | |
times = {k: AVEFUNC(v) for k, v in results.items()} | |
stimes = sorted(times, key=times.get) | |
base = times[stimes[0]] | |
host = platform.platform().strip() | |
print(f'Results for {args.count} runs for {args.mb}MB file on {host}:') | |
for ind, cmd in enumerate(stimes, 1): | |
val = times[cmd] | |
percent = (val - base) * 100 / base | |
print(f'{ind:2}: {val:8.3f}s {percent:+8.1f}% {cmd}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment