File: test_custom_timer.py

package info (click to toggle)
python-pyinstrument 5.1.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,624 kB
  • sloc: python: 6,713; ansic: 897; makefile: 46; sh: 26; javascript: 18
file content (34 lines) | stat: -rw-r--r-- 641 bytes parent folder | download
1
2
3
4
5
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
33
34
from typing import Any

from .util import parametrize_setstatprofile


class CallCounter:
    def __init__(self) -> None:
        self.count = 0

    def __call__(self, *args: Any, **kwds: Any) -> Any:
        self.count += 1


@parametrize_setstatprofile
def test_increment(setstatprofile):
    time = 0.0

    def fake_time():
        return time

    def fake_sleep(duration):
        nonlocal time
        time += duration

    counter = CallCounter()

    setstatprofile(counter, timer_func=fake_time, timer_type="timer_func")

    for _ in range(100):
        fake_sleep(1.0)

    setstatprofile(None)

    assert counter.count == 100