Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/tools/maint/rebaseline_tests.py
6174 views
1
#!/usr/bin/env python3
2
# Copyright 2024 The Emscripten Authors. All rights reserved.
3
# Emscripten is available under two separate licenses, the MIT license and the
4
# University of Illinois/NCSA Open Source License. Both these licenses can be
5
# found in the LICENSE file.
6
7
"""Automatically rebaseline tests that have codesize expectations and create
8
a git commit containing the resulting changes along with readable details of
9
the generated changes.
10
"""
11
12
import argparse
13
import json
14
import os
15
import statistics
16
import subprocess
17
import sys
18
19
script_dir = os.path.dirname(os.path.abspath(__file__))
20
root_dir = os.path.dirname(os.path.dirname(script_dir))
21
22
sys.path.insert(0, root_dir)
23
from tools import utils
24
25
26
def run(cmd, **args):
27
return subprocess.check_output(cmd, text=True, cwd=root_dir, **args)
28
29
30
all_deltas = []
31
32
33
def read_size_from_json(content):
34
json_data = json.loads(content)
35
if 'total' in json_data:
36
return json_data['total']
37
# If `total` if not in the json dict then just use the first key. This happens when only one
38
# file size is reported (in this case we don't calculate or store the `total`).
39
first_key = list(json_data.keys())[0]
40
return json_data[first_key]
41
42
43
def process_changed_file(filename):
44
content = open(filename).read()
45
old_content = run(['git', 'show', f'HEAD:{filename}'])
46
print(f'processing {filename}')
47
48
ext = os.path.splitext(filename)[1]
49
if ext == '.size':
50
size = int(content.strip())
51
old_size = int(old_content.strip())
52
elif ext == '.json':
53
size = read_size_from_json(content)
54
old_size = read_size_from_json(old_content)
55
else:
56
# Unhandled file type
57
return f'{filename} updated\n'
58
59
filename = filename.removeprefix('test/')
60
delta = size - old_size
61
percent_delta = delta * 100 / old_size
62
all_deltas.append(percent_delta)
63
return f'{filename}: {old_size} => {size} [{delta:+} bytes / {percent_delta:+.2f}%]\n'
64
65
66
def main():
67
parser = argparse.ArgumentParser()
68
parser.add_argument('-s', '--skip-tests', action='store_true', help="Don't actually run the tests, just analyze the existing results")
69
parser.add_argument('-b', '--new-branch', action='store_true', help='Create a new branch containing the updates')
70
parser.add_argument('-c', '--clear-cache', action='store_true', help='Clear the cache before rebaselining (useful when working with llvm changes)')
71
parser.add_argument('-n', '--check-only', dest='check_only', action='store_true', help='Return non-zero if test expectations are out of date, and skip creating a git commit')
72
args = parser.parse_args()
73
74
if args.clear_cache:
75
run(['./emcc', '--clear-cache'])
76
77
if not args.skip_tests:
78
if not args.check_only and run(['git', 'status', '-uno', '--porcelain']).strip():
79
print('tree is not clean')
80
return 1
81
82
subprocess.check_call([utils.exe_path_from_root('test/runner'), '--rebaseline', 'codesize'], cwd=root_dir)
83
84
output = run(['git', 'status', '-uno', '--porcelain'])
85
filenames = []
86
for line in output.splitlines():
87
filename = line.strip().rsplit(' ', 1)[1]
88
if filename.startswith('test') and os.path.isfile(filename):
89
filenames.append(filename)
90
91
if not filenames:
92
print('test expectations are up-to-date')
93
return 0
94
95
if args.check_only:
96
message = f'''Test expectations are out-of-date
97
98
The following ({len(filenames)}) test expectation files were updated by
99
running the tests with `--rebaseline`:
100
101
```
102
'''
103
else:
104
message = f'''
105
Automatic rebaseline of codesize expectations. NFC
106
107
This is an automatic change generated by tools/maint/rebaseline_tests.py.
108
109
The following ({len(filenames)}) test expectation files were updated by
110
running the tests with `--rebaseline`:
111
112
```
113
'''
114
115
for file in filenames:
116
message += process_changed_file(file)
117
118
if all_deltas:
119
message += f'\nAverage change: {statistics.mean(all_deltas):+.2f}% ({min(all_deltas):+.2f}% - {max(all_deltas):+.2f}%)\n'
120
121
message += '```\n'
122
123
print(message)
124
if args.check_only:
125
return 1
126
127
if args.new_branch:
128
run(['git', 'checkout', '-b', 'rebaseline_tests'])
129
run(['git', 'add', '-u', '.'])
130
run(['git', 'commit', '-F', '-'], input=message)
131
132
return 2
133
134
135
if __name__ == '__main__':
136
sys.exit(main())
137
138