Skip to content

Commit

Permalink
Add benchmarks for compiler itself.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 725944126
  • Loading branch information
gkdn authored and copybara-github committed Feb 12, 2025
1 parent 770af94 commit 0e3c8ed
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def benchmark_library(
srcs,
deps = [],
tags = [],
data = [],
jvm_only = False,
j2cl_srcs = None,
j2cl_deps = None,
j2wasm_srcs = None,
Expand All @@ -23,20 +25,27 @@ 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,
)

j2cl_library(
name = name + "-j2cl",
srcs = j2cl_srcs,
testonly = 1,
deps = j2cl_deps,
tags = tags,
)
13 changes: 12 additions & 1 deletion benchmarking/java/com/google/j2cl/benchmarking/benchmarks.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -49,21 +51,27 @@ 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,
],
)

j2cl_application(
name = "%s_j2cl_entry" % name,
testonly = 1,
deps = [":%s-j2cl_glue" % name],
jre_checks_check_level = "MINIMAL",
entry_points = ["%s_launcher" % name],
Expand All @@ -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),
Expand All @@ -91,13 +100,15 @@ 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,
],
)
j2cl_application(
name = "%s_j2wasm_entry" % name,
testonly = 1,
deps = [":%s_j2wasm_glue" % name],
entry_points = ["%s_launcher" % name],
)
Expand Down
24 changes: 24 additions & 0 deletions benchmarking/java/com/google/j2cl/benchmarks/transpiler/BUILD
Original file line number Diff line number Diff line change
@@ -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")
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
28 changes: 23 additions & 5 deletions dev/bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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():
Expand All @@ -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()
Expand Down
12 changes: 12 additions & 0 deletions transpiler/javatests/com/google/j2cl/transpiler/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"],
)

0 comments on commit 0e3c8ed

Please sign in to comment.