Skip to content

Test

performance_report(func, *args, save_path='need_profiling.prof', **kwargs)

Profiles the performance of a given function and saves the profiling report to a file.

Parameters:

Name Type Description Default
func callable

The function to be profiled.

required
*args

Variable length argument list to be passed to the function.

()
**kwargs

Arbitrary keyword arguments to be passed to the function.

{}

Returns:

Type Description

None

Side Effects

Creates a profiling report file named 'need_profiling.prof' in the current directory.

Source code in src/pymace/test/perftest.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def performance_report(func, *args, save_path = "need_profiling.prof", **kwargs):
    """
    Profiles the performance of a given function and saves the profiling report to a file.

    Args:
        func (callable): The function to be profiled.
        *args: Variable length argument list to be passed to the function.
        **kwargs: Arbitrary keyword arguments to be passed to the function.

    Returns:
        None

    Side Effects:
        Creates a profiling report file named 'need_profiling.prof' in the current directory.
    """
    with cProfile.Profile() as pr:
        func(*args, **kwargs)
    stats = pstats.Stats(pr)
    stats.sort_stats(pstats.SortKey.TIME)
    stats.dump_stats(filename=save_path)

performance_time(repetitions, func, *args, output=None, **kwargs)

Measures the performance time of a given function by executing it a specified number of times.

Parameters:

Name Type Description Default
repetitions int

The number of times to execute the function.

required
func callable

The function to be executed.

required
*args

Variable length argument list to pass to the function.

()
output str

If set to "toConsole", the performance results will be logged to the console. Defaults to None.

None
**kwargs

Arbitrary keyword arguments to pass to the function.

{}

Returns:

Name Type Description
float float | None

The average time per execution in seconds if output is not "toConsole".

Source code in src/pymace/test/perftest.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def performance_time(repetitions: int, func: callable, *args, output: str = None, **kwargs) -> float|None:
    """
    Measures the performance time of a given function by executing it a specified number of times.

    Args:
        repetitions (int): The number of times to execute the function.
        func (callable): The function to be executed.
        *args: Variable length argument list to pass to the function.
        output (str, optional): If set to "toConsole", the performance results will be logged to the console. Defaults to None.
        **kwargs: Arbitrary keyword arguments to pass to the function.

    Returns:
        float: The average time per execution in seconds if output is not "toConsole".
    """
    start = time.perf_counter()
    for _ in range(repetitions):
        func(*args, **kwargs)
    end = time.perf_counter()
    if output == "toConsole":
        took = end - start
        logging.debug(f"took {took:.3f} s, ")
        logging.debug(f"{repetitions/took:.3f} it/s, ")
        logging.debug(f"{took/repetitions*1e3:.3f} ms/it")
        return
    return (end - start) / repetitions

getsize(obj)

Calculates the size of a given python object

Parameters:

Name Type Description Default
obj any

Python Object

required

Raises:

Type Description
TypeError

Can't find the size of function and method type

Returns:

Name Type Description
int int

Size in Bytes

Source code in src/pymace/test/size.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def getsize(obj: any) -> int:
    """ Calculates the size of a given python object

    Args:
        obj (any): Python Object

    Raises:
        TypeError: Can't find the size of function and method type

    Returns:
        int: Size in Bytes
    """
    BLACKLIST = type, ModuleType, FunctionType
    if isinstance(obj, BLACKLIST):
        raise TypeError("getsize() does not take argument of type: " + str(type(obj)))
    seen_ids = set()
    size = 0
    objects = [obj]
    while objects:
        need_referents = []
        for obj in objects:
            if not isinstance(obj, BLACKLIST) and id(obj) not in seen_ids:
                seen_ids.add(id(obj))
                size += sys.getsizeof(obj)
                need_referents.append(obj)
        objects = get_referents(*need_referents)
    return size