|
| 1 | +#! /usr/bin/python |
| 2 | +# |
| 3 | +# Compares the speed of two different compilers on one or more benchmarks in |
| 4 | +# the suite. |
| 5 | + |
| 6 | +from __future__ import division, print_function |
| 7 | + |
| 8 | +import os |
| 9 | +import subprocess |
| 10 | +import sys |
| 11 | +import time |
| 12 | + |
| 13 | +def run_test(dir, rustc): |
| 14 | + os.chdir(dir) |
| 15 | + |
| 16 | + # First clean and rebuild from scratch. This downloads any necessary code |
| 17 | + # and builds preliminary crates. |
| 18 | + # |
| 19 | + # We use call() instead of check_call() for `make clean` because it can |
| 20 | + # fail reasonably, e.g. if we try to delete a Cargo.lock file thta isn't |
| 21 | + # present. |
| 22 | + subprocess.call('make clean > /dev/null 2>&1', shell=True) |
| 23 | + make_env = os.environ.copy() |
| 24 | + make_env['RUSTC'] = rustc |
| 25 | + subprocess.check_call('make > /dev/null 2>&1', env=make_env, shell=True) |
| 26 | + |
| 27 | + # Measure compilation speed of the crate of interest three times. |
| 28 | + times = [] |
| 29 | + for i in range(0, 3): |
| 30 | + subprocess.check_call('make touch > /dev/null 2>&1', shell=True) |
| 31 | + t1 = time.time() |
| 32 | + subprocess.check_call('make > /dev/null 2>&1', env=make_env, shell=True) |
| 33 | + t2 = time.time() |
| 34 | + t = t2 - t1 |
| 35 | + times.append(t) |
| 36 | + |
| 37 | + os.chdir('..') |
| 38 | + |
| 39 | + return times |
| 40 | + |
| 41 | + |
| 42 | +if __name__ == '__main__': |
| 43 | + if (len(sys.argv)) < 3: |
| 44 | + print('usage:', sys.argv[0], '<rustc1>', '<rustc2>', '[benchmarks]') |
| 45 | + sys.exit(1) |
| 46 | + |
| 47 | + rustc1 = sys.argv[1] |
| 48 | + rustc2 = sys.argv[2] |
| 49 | + |
| 50 | + # Get all the directories, excluding ".git". |
| 51 | + all_dirs = [f for f in os.listdir('.') if os.path.isdir(f) and f != ".git"] |
| 52 | + |
| 53 | + # Get any specified directories. Otherwise, default to all of them. |
| 54 | + dirs = sys.argv[3:] |
| 55 | + if dirs: |
| 56 | + for dir in dirs: |
| 57 | + if dir not in all_dirs: |
| 58 | + print('error: bad directory specified: \'' + dir + '\'') |
| 59 | + sys.exit(1) |
| 60 | + else: |
| 61 | + dirs = all_dirs |
| 62 | + |
| 63 | + # Run the requested benchmarks. |
| 64 | + for dir in dirs: |
| 65 | + times1 = run_test(dir, rustc1) |
| 66 | + times2 = run_test(dir, rustc2) |
| 67 | + |
| 68 | + min1 = min(times1) |
| 69 | + min2 = min(times2) |
| 70 | + max1 = max(times1) |
| 71 | + max2 = max(times2) |
| 72 | + |
| 73 | + print('{:15s} {:6.3f}s vs {:6.3f}s --> ' |
| 74 | + '{:5.3f}x faster (variance: {:5.3f}x, {:5.3f}x)'. |
| 75 | + format(dir[:15], min1, min2, min1 / min2, max1 / min1, max2 / min2)) |
| 76 | + |
0 commit comments