Add a sanity check on the benchmark runner to surface user errors in setting the benchmarks regex.

PiperOrigin-RevId: 321636971
Change-Id: Ib556e4a552903f2a64bb87a55475f3968bea8e32
This commit is contained in:
Sai Ganesh Bandiatmakuri 2020-07-16 13:44:11 -07:00 committed by TensorFlower Gardener
parent 57680ec9be
commit 02f45635e5
2 changed files with 10 additions and 1 deletions

View File

@ -96,6 +96,7 @@ class BenchmarkTest(test.TestCase):
self.assertFalse(_ran_somebenchmark_but_shouldnt[0])
# Run other benchmarks, but this wont run the one we care about
with self.assertRaises(ValueError):
benchmark._run_benchmarks("unrelated")
# Validate that SomeBenchmark has not run yet

View File

@ -430,9 +430,13 @@ def _run_benchmarks(regex):
Args:
regex: The string regular expression to match Benchmark classes against.
Raises:
ValueError: If no benchmarks were selected by the input regex.
"""
registry = list(GLOBAL_BENCHMARK_REGISTRY)
selected_benchmarks = []
# Match benchmarks in registry against regex
for benchmark in registry:
benchmark_name = "%s.%s" % (benchmark.__module__, benchmark.__name__)
@ -448,6 +452,7 @@ def _run_benchmarks(regex):
continue
full_benchmark_name = "%s.%s" % (benchmark_name, attr)
if regex == "all" or re.search(regex, full_benchmark_name):
selected_benchmarks.append(full_benchmark_name)
# Instantiate the class if it hasn't been instantiated
benchmark_instance = benchmark_instance or benchmark()
# Get the method tied to the class
@ -455,6 +460,9 @@ def _run_benchmarks(regex):
# Call the instance method
instance_benchmark_fn()
if not selected_benchmarks:
raise ValueError("No benchmarks matched the pattern: '{}'".format(regex))
def benchmarks_main(true_main, argv=None):
"""Run benchmarks as declared in argv.