blob: 027d994132ee847533dfcf66e45eb06187ae75af [file] [log] [blame]
Joshua Haberman7907ed92021-10-06 19:21:00 -07001#!/usr/bin/python3
Joshua Habermane59d2c82021-04-05 10:47:53 -07002#
Adam Cozzette5aca7282023-08-07 10:01:08 -07003# Protocol Buffers - Google's data interchange format
4# Copyright 2023 Google LLC. All rights reserved.
5# https://developers.google.com/protocol-buffers/
Joshua Habermane59d2c82021-04-05 10:47:53 -07006#
7# Redistribution and use in source and binary forms, with or without
Adam Cozzette5aca7282023-08-07 10:01:08 -07008# modification, are permitted provided that the following conditions are
9# met:
Joshua Habermane59d2c82021-04-05 10:47:53 -070010#
Adam Cozzette5aca7282023-08-07 10:01:08 -070011# * Redistributions of source code must retain the above copyright
12# notice, this list of conditions and the following disclaimer.
13# * Redistributions in binary form must reproduce the above
14# copyright notice, this list of conditions and the following disclaimer
15# in the documentation and/or other materials provided with the
16# distribution.
17# * Neither the name of Google LLC nor the names of its
18# contributors may be used to endorse or promote products derived from
19# this software without specific prior written permission.
20#
21# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Joshua Habermane59d2c82021-04-05 10:47:53 -070032
Joshua Haberman5741eb92020-09-14 12:45:59 -070033"""Benchmarks the current working directory against a given baseline.
Joshua Haberman08b6d2d2020-04-06 07:56:41 -070034
Joshua Haberman5741eb92020-09-14 12:45:59 -070035This script benchmarks both size and speed. Sample output:
36"""
37
38import contextlib
Joshua Haberman08b6d2d2020-04-06 07:56:41 -070039import json
Joshua Haberman5741eb92020-09-14 12:45:59 -070040import os
Joshua Haberman08b6d2d2020-04-06 07:56:41 -070041import re
Joshua Haberman5741eb92020-09-14 12:45:59 -070042import subprocess
43import sys
44import tempfile
45
46@contextlib.contextmanager
47def GitWorktree(commit):
48 tmpdir = tempfile.mkdtemp()
Joshua Habermana202ce92020-09-18 12:45:46 -070049 subprocess.run(['git', 'worktree', 'add', '-q', '-d', tmpdir, commit], check=True)
Joshua Haberman5741eb92020-09-14 12:45:59 -070050 cwd = os.getcwd()
51 os.chdir(tmpdir)
52 try:
53 yield tmpdir
54 finally:
55 os.chdir(cwd)
56 subprocess.run(['git', 'worktree', 'remove', tmpdir], check=True)
Joshua Haberman08b6d2d2020-04-06 07:56:41 -070057
58def Run(cmd):
59 subprocess.check_call(cmd, shell=True)
60
Joshua Haberman358fa142020-11-12 12:36:26 -080061def Benchmark(outbase, bench_cpu=True, runs=12, fasttable=False):
Joshua Haberman08b6d2d2020-04-06 07:56:41 -070062 tmpfile = "/tmp/bench-output.json"
63 Run("rm -rf {}".format(tmpfile))
Joshua Haberman358fa142020-11-12 12:36:26 -080064 #Run("CC=clang bazel test ...")
65 if fasttable:
66 extra_args = " --//:fasttable_enabled=true"
67 else:
68 extra_args = ""
Joshua Haberman08b6d2d2020-04-06 07:56:41 -070069
Joshua Habermanb58d2a02020-10-11 17:56:57 -070070 if bench_cpu:
Joshua Haberman358fa142020-11-12 12:36:26 -080071 Run("CC=clang bazel build -c opt --copt=-march=native benchmarks:benchmark" + extra_args)
Joshua Haberman77c03812021-10-04 18:29:32 -070072 Run("./bazel-bin/benchmarks/benchmark --benchmark_out_format=json --benchmark_out={} --benchmark_repetitions={} --benchmark_min_time=0.05 --benchmark_enable_random_interleaving=true".format(tmpfile, runs))
Joshua Habermanb58d2a02020-10-11 17:56:57 -070073 with open(tmpfile) as f:
74 bench_json = json.load(f)
75
76 # Translate into the format expected by benchstat.
Joshua Haberman77c03812021-10-04 18:29:32 -070077 txt_filename = outbase + ".txt"
78 with open(txt_filename, "w") as f:
Joshua Habermanb58d2a02020-10-11 17:56:57 -070079 for run in bench_json["benchmarks"]:
Joshua Haberman77c03812021-10-04 18:29:32 -070080 if run["run_type"] == "aggregate":
81 continue
Joshua Haberman9abf8e02020-11-14 21:00:06 -080082 name = run["name"]
83 name = name.replace(" ", "")
84 name = re.sub(r'^BM_', 'Benchmark', name)
Joshua Habermanb58d2a02020-10-11 17:56:57 -070085 values = (name, run["iterations"], run["cpu_time"])
86 print("{} {} {} ns/op".format(*values), file=f)
Joshua Haberman77c03812021-10-04 18:29:32 -070087 Run("sort {} -o {} ".format(txt_filename, txt_filename))
Joshua Habermanb58d2a02020-10-11 17:56:57 -070088
Joshua Haberman970c6452022-03-08 17:44:06 -080089 Run("CC=clang bazel build -c opt --copt=-g --copt=-march=native :conformance_upb"
90 + extra_args)
Joshua Haberman84054362022-03-06 10:44:47 -080091 Run("cp -f bazel-bin/conformance_upb {}.bin".format(outbase))
Joshua Haberman9b31e8f2020-09-13 17:51:27 -070092
Joshua Haberman08b6d2d2020-04-06 07:56:41 -070093
Joshua Haberman50978252021-11-05 00:31:38 +000094baseline = "main"
Joshua Habermanc3588292021-02-22 15:30:37 -080095bench_cpu = True
Joshua Haberman358fa142020-11-12 12:36:26 -080096fasttable = False
Joshua Haberman5741eb92020-09-14 12:45:59 -070097
98if len(sys.argv) > 1:
99 baseline = sys.argv[1]
100
101 # Quickly verify that the baseline exists.
102 with GitWorktree(baseline):
103 pass
104
105# Benchmark our current directory first, since it's more likely to be broken.
Joshua Haberman358fa142020-11-12 12:36:26 -0800106Benchmark("/tmp/new", bench_cpu, fasttable=fasttable)
Joshua Haberman5741eb92020-09-14 12:45:59 -0700107
108# Benchmark the baseline.
109with GitWorktree(baseline):
Joshua Haberman358fa142020-11-12 12:36:26 -0800110 Benchmark("/tmp/old", bench_cpu, fasttable=fasttable)
Joshua Haberman9b31e8f2020-09-13 17:51:27 -0700111
112print()
113print()
Joshua Haberman08b6d2d2020-04-06 07:56:41 -0700114
Joshua Habermanb58d2a02020-10-11 17:56:57 -0700115if bench_cpu:
116 Run("~/go/bin/benchstat /tmp/old.txt /tmp/new.txt")
Joshua Haberman9b31e8f2020-09-13 17:51:27 -0700117
118print()
119print()
120
121Run("objcopy --strip-debug /tmp/old.bin /tmp/old.bin.stripped")
122Run("objcopy --strip-debug /tmp/new.bin /tmp/new.bin.stripped")
123Run("~/code/bloaty/bloaty /tmp/new.bin.stripped -- /tmp/old.bin.stripped --debug-file=/tmp/old.bin --debug-file=/tmp/new.bin -d compileunits,symbols")