|
| 1 | +import argparse |
| 2 | +import contextlib |
| 3 | +import os |
| 4 | +import subprocess |
| 5 | +from graph_results import parse_benchmark_results, graph_results |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +def main(): |
| 11 | + argparser = argparse.ArgumentParser(description='Builds & runs the benchmarks, saves the output to a text file, and produces a number of graphs') |
| 12 | + argparser.add_argument('-f', '--file', type=str, default=None, help='true if you want to use previously saved output; otherwise, we\'ll build and run the benchmark "live"') |
| 13 | + args = argparser.parse_args() |
| 14 | + |
| 15 | + if args.file: |
| 16 | + with open(args.file) as results_file: |
| 17 | + results_raw = results_file.readlines() |
| 18 | + else: |
| 19 | + results_raw = build_and_run() |
| 20 | + with open('output.txt', 'w') as out_file: |
| 21 | + out_file.writelines([line + '\n' for line in results_raw]) |
| 22 | + |
| 23 | + for max_elements in [64, 256, 1024, 16384]: |
| 24 | + graph_results( |
| 25 | + parse_benchmark_results(results_raw, None, max_elements), |
| 26 | + os.path.join('graphs', "container_size_up_to_%d" % max_elements)) |
| 27 | + |
| 28 | + |
| 29 | +def build_and_run(): |
| 30 | + with friendly_cd_to_repo_root(): |
| 31 | + subprocess.check_call(['cmake', 'CMakeLists.txt']) |
| 32 | + subprocess.check_call(['make']) |
| 33 | + benchmark_output_bytes = subprocess.check_output(['./llvm_data_structure_benchmarks', '--benchmark_color=false']) |
| 34 | + return benchmark_output_bytes.decode('utf-8').splitlines() |
| 35 | + |
| 36 | + |
| 37 | +@contextlib.contextmanager |
| 38 | +def friendly_cd_to_repo_root(additional_path=None): |
| 39 | + """ |
| 40 | + A context manager for changing directories to some specific subdirectory |
| 41 | + of this repo. |
| 42 | + :type additional_path: str|None |
| 43 | +
|
| 44 | + >>> prev_path = os.getcwd() |
| 45 | + >>> with friendly_cd_to_repo_root('scripts'): |
| 46 | + ... assert os.getcwd().endswith(os.path.join('llvm-data-structure-benchmarks', 'scripts')) |
| 47 | + >>> assert prev_path == os.getcwd() |
| 48 | + """ |
| 49 | + def _cd_up_until_file_path_exists(file_to_check_for, max_levels=3): |
| 50 | + for i in range(max_levels): |
| 51 | + if not file_to_check_for in os.listdir(os.getcwd()): |
| 52 | + os.chdir('..') |
| 53 | + assert file_to_check_for in os.listdir(os.getcwd()), 'It looks like you didn\'t run this script from an expected location' |
| 54 | + |
| 55 | + prev_dir = os.getcwd() |
| 56 | + try: |
| 57 | + _cd_up_until_file_path_exists('data_structure_benchmarks.cpp') |
| 58 | + if additional_path: |
| 59 | + os.chdir(additional_path) |
| 60 | + yield |
| 61 | + finally: |
| 62 | + os.chdir(prev_dir) |
| 63 | + |
| 64 | + |
| 65 | +if __name__ == "__main__": |
| 66 | + main() |
0 commit comments