|
| 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 | +from datetime import datetime |
| 8 | +import json |
| 9 | +import os |
| 10 | +import subprocess |
| 11 | +import statistics |
| 12 | +import sys |
| 13 | + |
| 14 | +script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 15 | +root_dir = os.path.dirname(os.path.dirname(script_dir)) |
| 16 | + |
| 17 | +sys.path.insert(0, root_dir) |
| 18 | +from tools import utils |
| 19 | + |
| 20 | +DEBUG = int(os.environ.get('DEBUG', '0')) |
| 21 | + |
| 22 | +TESTS = [ |
| 23 | + 'browser.test_small_js_flags', |
| 24 | + 'other.test_INCOMING_MODULE_JS_API', |
| 25 | + 'other.*code_size*', |
| 26 | + 'other.*codesize*' |
| 27 | +] |
| 28 | + |
| 29 | + |
| 30 | +def run(cmd, **args): |
| 31 | + return subprocess.check_output(cmd, text=True, cwd=root_dir, **args) |
| 32 | + |
| 33 | + |
| 34 | +all_deltas = [] |
| 35 | + |
| 36 | + |
| 37 | +def process_changed_file(filename): |
| 38 | + content = open(filename).read() |
| 39 | + old_content = run(['git', 'show', f'HEAD:{filename}']) |
| 40 | + print(f'processing {filename}') |
| 41 | + if len(content.splitlines()) == 1: |
| 42 | + size = int(content.strip()) |
| 43 | + old_size = int(old_content.strip()) |
| 44 | + else: |
| 45 | + try: |
| 46 | + current_json = json.loads(content) |
| 47 | + old_json = json.loads(old_content) |
| 48 | + except: |
| 49 | + print(f'{filename}: Unable to passe json content. Unsupported file format?') |
| 50 | + sys.exit(1) |
| 51 | + size = current_json['total'] |
| 52 | + old_size = old_json['total'] |
| 53 | + |
| 54 | + filename = utils.removeprefix(filename, 'test/') |
| 55 | + delta = size - old_size |
| 56 | + percent_delta = delta * 100 / old_size |
| 57 | + all_deltas.append(percent_delta) |
| 58 | + return f'{filename}: {old_size} => {size} [{delta:+} bytes / {percent_delta:+.2f}%]\n' |
| 59 | + |
| 60 | + |
| 61 | +def main(argv): |
| 62 | + if not DEBUG and run(['git', 'status', '-uno', '--porcelain']).strip(): |
| 63 | + print('tree is not clean') |
| 64 | + return 1 |
| 65 | + |
| 66 | + subprocess.check_call(['test/runner', '--rebaseline', '--browser=0'] + TESTS, cwd=root_dir) |
| 67 | + |
| 68 | + if not run(['git', 'status', '-uno', '--porcelain']): |
| 69 | + print('no updates found') |
| 70 | + return 1 |
| 71 | + |
| 72 | + output = run(['git', 'status', '-uno', '--porcelain']) |
| 73 | + filenames = [] |
| 74 | + for line in output.splitlines(): |
| 75 | + status, filename = line.strip().split(' ', 1) |
| 76 | + filenames.append(filename) |
| 77 | + |
| 78 | + |
| 79 | + commit_message = f''' |
| 80 | +Automatic rebaseline of codesize expectations. NFC |
| 81 | +
|
| 82 | +This is an automatic change generated by tools/maint/rebaseline_tests.py. |
| 83 | +
|
| 84 | +The following ({len(filenames)}) test expectation files were updated by |
| 85 | +running the tests with `--rebaseline`: |
| 86 | +
|
| 87 | +''' |
| 88 | + |
| 89 | + for file in filenames: |
| 90 | + commit_message += process_changed_file(file) |
| 91 | + |
| 92 | + commit_message += f'\nAverage change: {statistics.mean(all_deltas):+.2f}%\n' |
| 93 | + |
| 94 | + run(['git', 'checkout', '-b', 'rebaseline_tests']) |
| 95 | + run(['git', 'add', '-u', '.']) |
| 96 | + subprocess.run(['git', 'commit', '-F', '-'], text=True, input=commit_message, check=True, cwd=root_dir) |
| 97 | + |
| 98 | + print(commit_message) |
| 99 | + return 0 |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == '__main__': |
| 103 | + sys.exit(main(sys.argv)) |
0 commit comments