From 0e3c8edefab432b9089152ce62f346846dada8c3 Mon Sep 17 00:00:00 2001 From: Goktug Gokdogan Date: Wed, 12 Feb 2025 00:55:44 -0800 Subject: [PATCH] Add benchmarks for compiler itself. PiperOrigin-RevId: 725944126 --- .../j2cl/benchmarking/benchmark_library.bzl | 9 ++++ .../google/j2cl/benchmarking/benchmarks.bzl | 13 +++++- .../google/j2cl/benchmarks/transpiler/BUILD | 24 +++++++++++ .../transpiler/JavaCompileBenchmark.java | 41 +++++++++++++++++++ .../transpiler/KotlinCompileBenchmark.java | 41 +++++++++++++++++++ dev/bench.py | 28 ++++++++++--- .../com/google/j2cl/transpiler/BUILD | 12 ++++++ 7 files changed, 162 insertions(+), 6 deletions(-) create mode 100644 benchmarking/java/com/google/j2cl/benchmarks/transpiler/BUILD create mode 100644 benchmarking/java/com/google/j2cl/benchmarks/transpiler/JavaCompileBenchmark.java create mode 100644 benchmarking/java/com/google/j2cl/benchmarks/transpiler/KotlinCompileBenchmark.java diff --git a/benchmarking/java/com/google/j2cl/benchmarking/benchmark_library.bzl b/benchmarking/java/com/google/j2cl/benchmarking/benchmark_library.bzl index 1bf3adcc8e..e0289816cb 100644 --- a/benchmarking/java/com/google/j2cl/benchmarking/benchmark_library.bzl +++ b/benchmarking/java/com/google/j2cl/benchmarking/benchmark_library.bzl @@ -9,6 +9,8 @@ def benchmark_library( srcs, deps = [], tags = [], + data = [], + jvm_only = False, j2cl_srcs = None, j2cl_deps = None, j2wasm_srcs = None, @@ -23,13 +25,19 @@ def benchmark_library( java_library( name = name, srcs = srcs, + testonly = 1, + data = data, deps = deps, tags = tags, ) + if jvm_only: + return + j2wasm_library( name = name + "-j2wasm", srcs = j2wasm_srcs, + testonly = 1, deps = j2wasm_deps, tags = tags, ) @@ -37,6 +45,7 @@ def benchmark_library( j2cl_library( name = name + "-j2cl", srcs = j2cl_srcs, + testonly = 1, deps = j2cl_deps, tags = tags, ) diff --git a/benchmarking/java/com/google/j2cl/benchmarking/benchmarks.bzl b/benchmarking/java/com/google/j2cl/benchmarking/benchmarks.bzl index 4073797d8a..ef77280bc5 100644 --- a/benchmarking/java/com/google/j2cl/benchmarking/benchmarks.bzl +++ b/benchmarking/java/com/google/j2cl/benchmarking/benchmarks.bzl @@ -14,7 +14,7 @@ load("@io_bazel_rules_closure//closure:defs.bzl", "closure_js_library") _BENCHMARK_LIST_RULE_NAME = "benchmark_list" -def benchmark(name, deps = []): +def benchmark(name, deps = [], data = [], jvm_only = False, perfgate_test_tags = []): """Defines a benchmark that can be run on the jvw, web and wasm. Args: @@ -39,7 +39,9 @@ def benchmark(name, deps = []): "%s.java" % name, create_launcher(name, benchmark_java_package), ], + data = data, deps = deps, + jvm_only = jvm_only, ) # JVM Benchmark @@ -49,14 +51,19 @@ def benchmark(name, deps = []): ) java_binary( name = "%s_local" % name, + testonly = 1, runtime_deps = [":%s_lib" % name], main_class = "%s.%sLauncher" % (benchmark_java_package, name), ) + if jvm_only: + return + # J2CL benchmark closure_js_library( name = "%s-j2cl_glue" % name, srcs = [create_j2cl_glue(name, benchmark_java_package)], + testonly = 1, deps = [ ":%s_lib-j2cl" % name, ], @@ -64,6 +71,7 @@ def benchmark(name, deps = []): j2cl_application( name = "%s_j2cl_entry" % name, + testonly = 1, deps = [":%s-j2cl_glue" % name], jre_checks_check_level = "MINIMAL", entry_points = ["%s_launcher" % name], @@ -78,6 +86,7 @@ def benchmark(name, deps = []): # J2WASM Benchmark j2wasm_application( name = "%s_j2wasm_binary" % name, + testonly = 1, deps = [":%s_lib-j2wasm" % name], entry_points = [ "%s.%sLauncher#execute" % (benchmark_java_package, name), @@ -91,6 +100,7 @@ def benchmark(name, deps = []): closure_js_library( name = "%s_j2wasm_glue" % name, srcs = [create_j2wasm_glue(name, wasm_url, wasm_module_name)], + testonly = 1, lenient = True, deps = [ ":%s_j2wasm_binary" % name, @@ -98,6 +108,7 @@ def benchmark(name, deps = []): ) j2cl_application( name = "%s_j2wasm_entry" % name, + testonly = 1, deps = [":%s_j2wasm_glue" % name], entry_points = ["%s_launcher" % name], ) diff --git a/benchmarking/java/com/google/j2cl/benchmarks/transpiler/BUILD b/benchmarking/java/com/google/j2cl/benchmarks/transpiler/BUILD new file mode 100644 index 0000000000..0c83f7e66b --- /dev/null +++ b/benchmarking/java/com/google/j2cl/benchmarks/transpiler/BUILD @@ -0,0 +1,24 @@ +load("//:benchmarking/java/com/google/j2cl/benchmarking/benchmarks.bzl", "benchmark", "gen_benchmark_suite") + +package( + default_applicable_licenses = ["//:j2cl_license"], + licenses = ["notice"], +) + +benchmark( + name = "JavaCompileBenchmark", + data = [ + "//samples/box2d/src/main/java:libbox2d_library-j2cl-src.jar", + ], + jvm_only = True, + # Too slow to run as part of presubmits. + perfgate_test_tags = [ + "manual", + "notap", + ], + deps = [ + "//transpiler/javatests/com/google/j2cl/transpiler:tester_lib_public", + ], +) + +gen_benchmark_suite(name = "transpiler") diff --git a/benchmarking/java/com/google/j2cl/benchmarks/transpiler/JavaCompileBenchmark.java b/benchmarking/java/com/google/j2cl/benchmarks/transpiler/JavaCompileBenchmark.java new file mode 100644 index 0000000000..d9d5fb56b9 --- /dev/null +++ b/benchmarking/java/com/google/j2cl/benchmarks/transpiler/JavaCompileBenchmark.java @@ -0,0 +1,41 @@ +/* + * Copyright 2025 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.j2cl.benchmarks.transpiler; + +import static com.google.j2cl.transpiler.TranspilerTester.newTesterWithDefaults; + +import com.google.j2cl.benchmarking.framework.AbstractBenchmark; +import com.google.j2cl.transpiler.TranspilerTester.TranspileResult; + +/** Benchmark for the compiler itself. */ +public class JavaCompileBenchmark extends AbstractBenchmark { + + @Override + public Object run() { + try { + return compile(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + private static TranspileResult compile() throws Exception { + return newTesterWithDefaults() + .addCompilationUnit("org.jspecify.annotations.Nullable", "public @interface Nullable {}") + .addSourcePathArg("samples/box2d/src/main/java/libbox2d_library-j2cl-src.jar") + .assertTranspileSucceeds(); + } +} diff --git a/benchmarking/java/com/google/j2cl/benchmarks/transpiler/KotlinCompileBenchmark.java b/benchmarking/java/com/google/j2cl/benchmarks/transpiler/KotlinCompileBenchmark.java new file mode 100644 index 0000000000..b2a16aadcd --- /dev/null +++ b/benchmarking/java/com/google/j2cl/benchmarks/transpiler/KotlinCompileBenchmark.java @@ -0,0 +1,41 @@ +/* + * Copyright 2025 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.j2cl.benchmarks.transpiler; + +import static com.google.j2cl.transpiler.TranspilerTester.newTesterWithKotlinDefaults; + +import com.google.j2cl.benchmarking.framework.AbstractBenchmark; +import com.google.j2cl.transpiler.TranspilerTester.TranspileResult; + +/** Benchmark for the compiler itself. */ +public class KotlinCompileBenchmark extends AbstractBenchmark { + + @Override + public Object run() { + try { + return compile(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + private static TranspileResult compile() throws Exception { + return newTesterWithKotlinDefaults() + .addSourcePathArg( + "samples/box2d/src/main/kotlin/auto_converted/libbox2d_library-j2cl-src.jar") + .assertTranspileSucceeds(); + } +} diff --git a/dev/bench.py b/dev/bench.py index aa8a221c3a..dac495eaa0 100644 --- a/dev/bench.py +++ b/dev/bench.py @@ -20,14 +20,21 @@ def main(argv): - bench_names = [] if argv.bench_names == ["all"] else argv.bench_names - - if subprocess.call("v8 -e ''", shell=True): + if subprocess.call("v8 -e ''", shell=True) and argv.platforms != ["JVM"]: print("Make sure d8 is installed via jsvu") sys.exit(1) - bench_map = _get_bench_map() - bench_names = bench_names or bench_map.keys() + if argv.bench_names == ["all"]: + bench_map = _get_bench_map() + bench_names = bench_map.keys() + elif argv.bench_names == ["transpiler"]: + assert argv.platforms == ["JVM"], "Transpiler benchmarks only support JVM" + bench_map = _get_transpiler_bench_map() + bench_names = bench_map.keys() + else: + bench_map = _get_bench_map() | _get_transpiler_bench_map() + bench_names = argv.bench_names + # Benchs as list of (name1, {j2cl: target1, j2wasm: target1}) pairs benchs = [ (n, repo_util.get_benchmarks(bench_map[n] + "_local", argv.platforms)) @@ -59,6 +66,7 @@ def main(argv): _JRE_BENCHMARK_LIST_FILE = "benchmarking/java/com/google/j2cl/benchmarks/jre/benchmark_list.txt" _OCTANE_BENCHMARK_LIST_FILE = "benchmarking/java/com/google/j2cl/benchmarks/octane/benchmark_list.txt" +_TRANSPILER_BENCHMARK_LIST_FILE = "benchmarking/java/com/google/j2cl/benchmarks/transpiler/benchmark_list.txt" def _get_bench_map(): @@ -73,6 +81,16 @@ def _get_bench_map(): return bench_names +def _get_transpiler_bench_map(): + repo_util.build([_TRANSPILER_BENCHMARK_LIST_FILE]) + + bench_names = {} + for bench_name in _read_gen_file(_TRANSPILER_BENCHMARK_LIST_FILE): + bench_names[bench_name] = "transpiler/" + bench_name + + return bench_names + + def _read_gen_file(file_path): with open(repo_util.BIN_DIR + file_path, "rt") as my_file: return my_file.read().splitlines() diff --git a/transpiler/javatests/com/google/j2cl/transpiler/BUILD b/transpiler/javatests/com/google/j2cl/transpiler/BUILD index 352f3e0969..c05b4815fe 100644 --- a/transpiler/javatests/com/google/j2cl/transpiler/BUILD +++ b/transpiler/javatests/com/google/j2cl/transpiler/BUILD @@ -175,7 +175,9 @@ java_library( name = "tester_lib", testonly = 1, srcs = ["TranspilerTester.java"], + javacopts = ["-Xep:BetaApi:OFF"], deps = [ + "//third_party:error_prone_annotations", "//third_party:guava", "//third_party:junit", "//third_party:runfiles", @@ -184,3 +186,13 @@ java_library( "//transpiler/java/com/google/j2cl/transpiler:commandlinerunner_lib", ], ) + +java_library( + name = "tester_lib_public", + testonly = 1, + data = [ + ":jre_bundle_deploy.jar", + ], + visibility = ["//benchmarking/java/com/google/j2cl/benchmarks/transpiler:__pkg__"], + exports = [":tester_lib"], +)