From 38b1a301fd9514249f7322649ecd43877d71deee Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 24 Oct 2010 19:09:17 -0400 Subject: [PATCH] Extend src/sorting/scaling.py to handle `cat data | sort` syntax. --- src/sorting/scaling.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/sorting/scaling.py b/src/sorting/scaling.py index 814836e..5af9a8c 100755 --- a/src/sorting/scaling.py +++ b/src/sorting/scaling.py @@ -2,8 +2,9 @@ """Measure how a sorting executable scales with N. -The executable should support the following usage: +The executable should support one of the following: executable path/to/data/file + cat path/to/data/file | executable Where the data file is of the format output by data.py. """ @@ -30,17 +31,26 @@ def generate_data(stream, N, ordered=False): assert status == 0, status stream.flush() -def run_test(executable, data_filename): +def run_test(executable, stdin=True, data_filename=None): print >> sys.stderr, 'run %s' % executable + if stdin: + with open(data_filename, 'r') as f: + contents = f.read() start = time.time() - q = subprocess.Popen([executable, data_filename], - stdout=open('/dev/null', 'w')) - status = q.wait() + if stdin: + p = subprocess.Popen([executable], + stdout=open('/dev/null', 'w')) + p.communicate(contents) + else: + p = subprocess.Popen([executable, data_filename], + stdout=open('/dev/null', 'w')) + status = p.wait() stop = time.time() assert status == 0, status return stop - start -def run_tests(executable, data_file, ordered=False, repeats=10, max_time=1e2): +def run_tests(executable, stdin=True, data_file=None, ordered=False, + repeats=10, max_time=1e2): times = {} prev_time = 0 N = 2 @@ -49,7 +59,7 @@ def run_tests(executable, data_file, ordered=False, repeats=10, max_time=1e2): ts = numpy.zeros((repeats,), dtype=numpy.double) for i in range(repeats): generate_data(data_file, N, ordered=ordered) - ts[i] = run_test(executable, data_file.name) + ts[i] = run_test(executable, stdin, data_file.name) times[N] = ts prev_time = ts.mean() N *= 2 @@ -62,6 +72,8 @@ if __name__ == '__main__': p = optparse.OptionParser( usage='%prog [options] executable', epilog=__doc__) + p.add_option('-s', '--stdin', dest='stdin', default=False, + action='store_true', help='Use the stdin executable syntax.') p.add_option('-r', '--repeats', dest='repeats', default=10, type='int', help='Number of repeats to run at each N (%default).') p.add_option('-m', '--max-time', dest='max_time', default=1e2,type='float', @@ -76,6 +88,7 @@ if __name__ == '__main__': data_file = tempfile.NamedTemporaryFile() kwargs = { 'executable': executable, + 'stdin': options.stdin, 'data_file': data_file, 'repeats': options.repeats, 'max_time': options.max_time, -- 2.26.2