From c527decda152734afa1302172bde4cf4fa434abb Mon Sep 17 00:00:00 2001 From: Marc Leclair Date: Thu, 7 Oct 2021 20:39:36 -0400 Subject: [PATCH] For #21700: Created benchmark script runner and edited benchmark gradle file --- app/benchmark.gradle | 12 ++++++++- tools/benchmark_runner.py | 56 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tools/benchmark_runner.py diff --git a/app/benchmark.gradle b/app/benchmark.gradle index 9238a821e..b88390a3a 100644 --- a/app/benchmark.gradle +++ b/app/benchmark.gradle @@ -61,6 +61,16 @@ ext.maybeConfigForJetpackBenchmark = { android -> // and to opt into a lower-max CPU frequency on unrooted devices that support it // - UNLOCKED: ./gradlew lockClocks, which locks the CPU frequency, fails on my device. See // https://issuetracker.google.com/issues/176836267 for potential workarounds. - testInstrumentationRunnerArgument 'androidx.benchmark.suppressErrors', 'ACTIVITY-MISSING,UNLOCKED' + + // The tests don't always output a JSON file with the data. To make sure it does, we have to + // set androidx.benchmark.output.enable to true. + // We set the the output directory simply for simplicity since the benchmark_runner.py script + // can't know the name of the phone in the /build/outputs/ directory. The system defaults to + // {phone_name} which can be troublesome finding in some case. + testInstrumentationRunnerArguments = [ + 'androidx.benchmark.suppressErrors' : 'ACTIVITY-MISSING,UNLOCKED', + 'androidx.benchmark.output.enable' : 'true', + additionalTestOutputDir : '/storage/emulated/0/benchmark' + ] } } diff --git a/tools/benchmark_runner.py b/tools/benchmark_runner.py new file mode 100644 index 000000000..5d39b0dfa --- /dev/null +++ b/tools/benchmark_runner.py @@ -0,0 +1,56 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at https://mozilla.org/MPL/2.0/. + +import subprocess +import webbrowser +import os +import argparse + +DESCRIPTION = """ This script is made to run benchmark tests on Fenix. It'll open +the JSON output file in firefox (or another browser of your choice if you pass the string in) +""" + +ff_browser = 'firefox' +output_path = '/storage/emulated/0/benchmark/' +output_file = 'org.mozilla.fenix-benchmarkData.json' +file_url = "file:///" + + +def parse_args(): + parser = argparse.ArgumentParser(description=DESCRIPTION) + parser.add_argument("--class_to_test", + help="Path to the class to test. Format it as 'org.mozilla.fenix.[path_to_benchmark_test") + + return parser.parse_args() + + +def run_benchmark(class_to_test): + args = ['./gradlew', '-Pbenchmark', 'app:connectedCheck'] + if class_to_test: + args.append("-Pandroid.testInstrumentationRunnerArguments.class={clazz}".format(clazz=class_to_test)) + subprocess.run(args, check=True, text=True) + + +def fetch_benchmark_results(): + subprocess.run( + ['adb', 'pull', "{path}{file}".format(path=output_path, file=output_file)], + check=True, text=True) + print("The benchmark results can be seen here: {file_path}" + .format(file_path=os.path.abspath("./{file}".format(file=file_url)))) + + +def open_in_browser(): + abs_path = os.path.abspath("./{file}".format(file=output_file)) + webbrowser.get(ff_browser).open_new(file_url+abs_path) + + +def main(): + args = parse_args() + run_benchmark(args.class_to_test) + fetch_benchmark_results() + open_in_browser() + + +if __name__ == '__main__': + main()