Extend src/sorting/scaling.py to handle `cat data | sort` syntax.
authorW. Trevor King <wking@drexel.edu>
Sun, 24 Oct 2010 23:09:17 +0000 (19:09 -0400)
committerW. Trevor King <wking@drexel.edu>
Sun, 24 Oct 2010 23:09:17 +0000 (19:09 -0400)
src/sorting/scaling.py

index 814836eb885cbf5001ade274c7f63020de615f56..5af9a8cc2f944ab40ebc0818f3093c0ca18610d6 100755 (executable)
@@ -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,