Skip to content

Testing

src.utils.testing.time_function(func: Callable) -> Callable

Print the execution time in seconds of any decorated function.

Parameters:

Name Type Description Default
func Callable

The wrapped function.

required
Source code in src\utils\testing.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def time_function(func: Callable) -> Callable:
    """Print the execution time in seconds of any decorated function.

    Args:
        func: The wrapped function.
    """
    def wrapper(*args, **kwargs):
        """Wraps the function call in a `perf_counter` timer."""
        start_time = perf_counter()
        result = func(*args, **kwargs)
        end_time = perf_counter()
        execution_time = end_time - start_time
        print(f"Executed {func.__name__} in {execution_time:.4f} seconds")
        return result
    return wrapper

src.utils.testing.test_execution_time(funcs: list[tuple[Callable, list[Any]]], iterations: int = 1000, reset_func: Optional[Callable] = None) -> None

Test the execution time of a new function against an older function.

Parameters:

Name Type Description Default
funcs list[tuple[Callable, list[Any]]]

List of tuples containing a func to test and args to pass to it.

required
iterations int

Number of calls to each function to perform.

1000
reset_func Callable | None

Optional function to call to reset app state between actions.

None
Source code in src\utils\testing.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def test_execution_time(
    funcs: list[tuple[Callable, list[Any]]],
    iterations: int = 1000,
    reset_func: Optional[Callable] = None
) -> None:
    """Test the execution time of a new function against an older function.

    Args:
        funcs: List of tuples containing a func to test and args to pass to it.
        iterations: Number of calls to each function to perform.
        reset_func: Optional function to call to reset app state between actions.
    """
    # Track results
    res: dict[str, Union[None, int, float, str, list]] = {
        'value': None, 'average': 0, 'times': [], 'name': ''}

    # Test configuration
    colorama.init(autoreset=True)
    results: list[dict[str, Union[None, int, float, str, list]]] = []

    # Test each function
    for func, args in funcs:
        r = res.copy()
        for i in range(iterations):
            s = perf_counter()
            r['value'] = func(*args)
            r['times'].append(perf_counter()-s)
            if reset_func:
                reset_func()
        r['average'] = sum(r['times'])/len(r['times'])
        r['name'] = func.__name__
        results.append(r.copy())

    # Report results
    results, compare = sorted(results, key=itemgetter('average')), 0
    for i, r in enumerate(results):
        if i == 0:
            compare = r['average']
            print(f"{Fore.GREEN}{r['name']}: {r['average']}")
            continue
        delta = r['average'] - compare
        percent = round(delta / ((r['average'] + compare) / 2) * 100, 2)
        print(f"{r['name']}: {r['average']} ({percent}% Slower)")

    # Check if all values were the same
    check, values = f'{Fore.GREEN}SUCCESS', []
    for i, r in enumerate(results):
        if i == 0:
            values.append(r['value'])
            continue
        if r['value'] not in values:
            check = f'{Fore.RED}FAILED'
            break
    print(f"Values check: {check}")