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:
parent
57680ec9be
commit
02f45635e5
@ -96,6 +96,7 @@ class BenchmarkTest(test.TestCase):
|
|||||||
self.assertFalse(_ran_somebenchmark_but_shouldnt[0])
|
self.assertFalse(_ran_somebenchmark_but_shouldnt[0])
|
||||||
|
|
||||||
# Run other benchmarks, but this wont run the one we care about
|
# Run other benchmarks, but this wont run the one we care about
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
benchmark._run_benchmarks("unrelated")
|
benchmark._run_benchmarks("unrelated")
|
||||||
|
|
||||||
# Validate that SomeBenchmark has not run yet
|
# Validate that SomeBenchmark has not run yet
|
||||||
|
@ -430,9 +430,13 @@ def _run_benchmarks(regex):
|
|||||||
|
|
||||||
Args:
|
Args:
|
||||||
regex: The string regular expression to match Benchmark classes against.
|
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)
|
registry = list(GLOBAL_BENCHMARK_REGISTRY)
|
||||||
|
|
||||||
|
selected_benchmarks = []
|
||||||
# Match benchmarks in registry against regex
|
# Match benchmarks in registry against regex
|
||||||
for benchmark in registry:
|
for benchmark in registry:
|
||||||
benchmark_name = "%s.%s" % (benchmark.__module__, benchmark.__name__)
|
benchmark_name = "%s.%s" % (benchmark.__module__, benchmark.__name__)
|
||||||
@ -448,6 +452,7 @@ def _run_benchmarks(regex):
|
|||||||
continue
|
continue
|
||||||
full_benchmark_name = "%s.%s" % (benchmark_name, attr)
|
full_benchmark_name = "%s.%s" % (benchmark_name, attr)
|
||||||
if regex == "all" or re.search(regex, full_benchmark_name):
|
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
|
# Instantiate the class if it hasn't been instantiated
|
||||||
benchmark_instance = benchmark_instance or benchmark()
|
benchmark_instance = benchmark_instance or benchmark()
|
||||||
# Get the method tied to the class
|
# Get the method tied to the class
|
||||||
@ -455,6 +460,9 @@ def _run_benchmarks(regex):
|
|||||||
# Call the instance method
|
# Call the instance method
|
||||||
instance_benchmark_fn()
|
instance_benchmark_fn()
|
||||||
|
|
||||||
|
if not selected_benchmarks:
|
||||||
|
raise ValueError("No benchmarks matched the pattern: '{}'".format(regex))
|
||||||
|
|
||||||
|
|
||||||
def benchmarks_main(true_main, argv=None):
|
def benchmarks_main(true_main, argv=None):
|
||||||
"""Run benchmarks as declared in argv.
|
"""Run benchmarks as declared in argv.
|
||||||
|
Loading…
Reference in New Issue
Block a user