X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=unittest_utils.py;h=d63f2b566b51b33bc794340b0d425d29a7e89142;hb=559c60c169223c7c6833e9beedf978a8ffdd3926;hp=5f4528348a0a3abb2538355db13884f21ca12fcd;hpb=142ec2f33945b549fbc9c2decd179cc0581cb55c;p=python_utils.git diff --git a/unittest_utils.py b/unittest_utils.py index 5f45283..d63f2b5 100644 --- a/unittest_utils.py +++ b/unittest_utils.py @@ -218,19 +218,21 @@ def check_method_for_perf_regressions(func: Callable) -> Callable: not config.config['unittests_ignore_perf'] ): msg = f'''{func_id} performance has regressed unacceptably. -{hist[-1]:f}s is the slowest record in {len(hist)} db perf samples. -It just ran in {run_time:f}s which is >5 stdevs slower than the slowest sample. +{slowest:f}s is the slowest runtime on record in {len(hist)} perf samples. +It just ran in {run_time:f}s which is 4+ stdevs slower than the slowest. Here is the current, full db perf timing distribution: ''' for x in hist: msg += f'{x:f}\n' logger.error(msg) - slf = args[0] - slf.fail(msg) + slf = args[0] # Peek at the wrapped function's self ref. + slf.fail(msg) # ...to fail the testcase. else: hist.append(run_time) + # Don't spam the database with samples; just pick a random + # sample from what we have and store that back. n = min(config.config['unittests_num_perf_samples'], len(hist)) hist = random.sample(hist, n) hist.sort() @@ -241,6 +243,18 @@ Here is the current, full db perf timing distribution: def check_all_methods_for_perf_regressions(prefix='test_'): + """Decorate unittests with this to pay attention to the perf of the + testcode and flag perf regressions. e.g. + + import unittest_utils as uu + + @uu.check_all_methods_for_perf_regressions() + class TestMyClass(unittest.TestCase): + + def test_some_part_of_my_class(self): + ... + + """ def decorate_the_testcase(cls): if issubclass(cls, unittest.TestCase): for name, m in inspect.getmembers(cls, inspect.isfunction):