|
59 | 59 | # to do nothing. |
60 | 60 | shutil.copystat = lambda *args, **kwargs: 0 |
61 | 61 |
|
62 | | -parser = argparse.ArgumentParser(prog = 'benchmark_change.py') |
63 | | -parser.add_argument('--base-commit', required=True) |
64 | | -parser.add_argument('--test-commit', required=True) |
65 | | -parser.add_argument('--test-case', required=True) |
66 | | -parser.add_argument('--num-iterations', type=int, required=True) |
67 | | -parser.add_argument('--num-binary-variants', type=int, required=True) |
68 | | -parser.add_argument('--output-dir', required=True) |
69 | | -parser.add_argument('--ldflags', required=False) |
| 62 | +parser = argparse.ArgumentParser(prog="benchmark_change.py") |
| 63 | +parser.add_argument("--base-commit", required=True) |
| 64 | +parser.add_argument("--test-commit", required=True) |
| 65 | +parser.add_argument("--test-case", required=True) |
| 66 | +parser.add_argument("--num-iterations", type=int, required=True) |
| 67 | +parser.add_argument("--num-binary-variants", type=int, required=True) |
| 68 | +parser.add_argument("--output-dir", required=True) |
| 69 | +parser.add_argument("--ldflags", required=False) |
70 | 70 | args = parser.parse_args() |
71 | 71 |
|
72 | 72 | test_dir = tempfile.mkdtemp() |
73 | | -print(f'Using {test_dir} as temporary directory') |
| 73 | +print(f"Using {test_dir} as temporary directory") |
74 | 74 |
|
75 | 75 | os.makedirs(args.output_dir) |
76 | | -print(f'Using {args.output_dir} as output directory') |
| 76 | +print(f"Using {args.output_dir} as output directory") |
| 77 | + |
77 | 78 |
|
78 | 79 | def extract_link_command(target): |
79 | | - # We assume that the last command printed by "ninja -t commands" containing a |
80 | | - # "-o" flag is the link command (we need to check for -o because subsequent |
81 | | - # commands create symlinks for ld.lld and so on). This is true for CMake and |
82 | | - # gn. |
83 | | - link_command = None |
84 | | - for line in subprocess.Popen(['ninja', '-t', 'commands', target], |
85 | | - stdout=subprocess.PIPE).stdout.readlines(): |
86 | | - commands = line.decode('utf-8').split('&&') |
87 | | - for command in commands: |
88 | | - if ' -o ' in command: |
89 | | - link_command = command.strip() |
90 | | - return link_command |
| 80 | + # We assume that the last command printed by "ninja -t commands" containing a |
| 81 | + # "-o" flag is the link command (we need to check for -o because subsequent |
| 82 | + # commands create symlinks for ld.lld and so on). This is true for CMake and |
| 83 | + # gn. |
| 84 | + link_command = None |
| 85 | + for line in subprocess.Popen( |
| 86 | + ["ninja", "-t", "commands", target], stdout=subprocess.PIPE |
| 87 | + ).stdout.readlines(): |
| 88 | + commands = line.decode("utf-8").split("&&") |
| 89 | + for command in commands: |
| 90 | + if " -o " in command: |
| 91 | + link_command = command.strip() |
| 92 | + return link_command |
| 93 | + |
91 | 94 |
|
92 | 95 | def generate_binary_variants(case_name): |
93 | | - subprocess.run(['ninja', 'lld']) |
94 | | - link_command = extract_link_command('lld') |
| 96 | + subprocess.run(["ninja", "lld"]) |
| 97 | + link_command = extract_link_command("lld") |
| 98 | + |
| 99 | + for i in range(0, args.num_binary_variants): |
| 100 | + print(f"Generating binary variant {i} for {case_name} case") |
| 101 | + command = f"{link_command} -o {test_dir}/lld-{case_name}{i} -Wl,--randomize-section-padding={i}" |
| 102 | + subprocess.run(command, check=True, shell=True) |
95 | 103 |
|
96 | | - for i in range(0, args.num_binary_variants): |
97 | | - print(f'Generating binary variant {i} for {case_name} case') |
98 | | - command = f'{link_command} -o {test_dir}/lld-{case_name}{i} -Wl,--randomize-section-padding={i}' |
99 | | - subprocess.run(command, check=True, shell=True) |
100 | 104 |
|
101 | 105 | # Make sure that there are no local changes. |
102 | | -subprocess.run(['git', 'diff', '--exit-code', 'HEAD'], check=True) |
| 106 | +subprocess.run(["git", "diff", "--exit-code", "HEAD"], check=True) |
103 | 107 |
|
104 | 108 | # Resolve the base and test commit, since if they are relative to HEAD we will |
105 | 109 | # check out the wrong commit below. |
106 | | -resolved_base_commit = subprocess.check_output(['git', 'rev-parse', args.base_commit]).strip() |
107 | | -resolved_test_commit = subprocess.check_output(['git', 'rev-parse', args.test_commit]).strip() |
| 110 | +resolved_base_commit = subprocess.check_output( |
| 111 | + ["git", "rev-parse", args.base_commit] |
| 112 | +).strip() |
| 113 | +resolved_test_commit = subprocess.check_output( |
| 114 | + ["git", "rev-parse", args.test_commit] |
| 115 | +).strip() |
108 | 116 |
|
109 | 117 | test_case_dir = os.path.dirname(args.test_case) |
110 | 118 | test_case_respfile = os.path.basename(args.test_case) |
111 | 119 |
|
112 | | -test_dir_test_case_dir = f'{test_dir}/testcase' |
| 120 | +test_dir_test_case_dir = f"{test_dir}/testcase" |
113 | 121 | shutil.copytree(test_case_dir, test_dir_test_case_dir) |
114 | 122 |
|
115 | | -subprocess.run(['git', 'checkout', resolved_base_commit], check=True) |
116 | | -generate_binary_variants('base') |
| 123 | +subprocess.run(["git", "checkout", resolved_base_commit], check=True) |
| 124 | +generate_binary_variants("base") |
| 125 | + |
| 126 | +subprocess.run(["git", "checkout", resolved_test_commit], check=True) |
| 127 | +generate_binary_variants("test") |
117 | 128 |
|
118 | | -subprocess.run(['git', 'checkout', resolved_test_commit], check=True) |
119 | | -generate_binary_variants('test') |
120 | 129 |
|
121 | 130 | def hyperfine_link_command(case_name): |
122 | | - return f'../lld-{case_name}$(({{iter}}%{args.num_binary_variants})) -flavor ld.lld @{test_case_respfile} {args.ldflags or ""}' |
123 | | - |
124 | | -results_csv = f'{args.output_dir}/results.csv' |
125 | | -subprocess.run(['hyperfine', '--export-csv', os.path.abspath(results_csv), |
126 | | - '-P', 'iter', '0', str(args.num_iterations - 1), |
127 | | - hyperfine_link_command('base'), |
128 | | - hyperfine_link_command('test')], |
129 | | - check=True, cwd=test_dir_test_case_dir) |
| 131 | + return f'../lld-{case_name}$(({{iter}}%{args.num_binary_variants})) -flavor ld.lld @{test_case_respfile} {args.ldflags or ""}' |
| 132 | + |
| 133 | + |
| 134 | +results_csv = f"{args.output_dir}/results.csv" |
| 135 | +subprocess.run( |
| 136 | + [ |
| 137 | + "hyperfine", |
| 138 | + "--export-csv", |
| 139 | + os.path.abspath(results_csv), |
| 140 | + "-P", |
| 141 | + "iter", |
| 142 | + "0", |
| 143 | + str(args.num_iterations - 1), |
| 144 | + hyperfine_link_command("base"), |
| 145 | + hyperfine_link_command("test"), |
| 146 | + ], |
| 147 | + check=True, |
| 148 | + cwd=test_dir_test_case_dir, |
| 149 | +) |
130 | 150 |
|
131 | 151 | shutil.rmtree(test_dir) |
0 commit comments