| 1 |
import ctypes |
| 2 |
import os |
| 3 |
import sys |
| 4 |
import time |
| 5 |
|
| 6 |
import pyinstrument.low_level.stat_profile as native_module |
| 7 |
|
| 8 |
from ..util import busy_wait, flaky_in_ci |
| 9 |
|
| 10 |
lib = ctypes.CDLL(native_module.__file__) |
| 11 |
|
| 12 |
pyi_timing_thread_subscribe = lib.pyi_timing_thread_subscribe |
| 13 |
pyi_timing_thread_subscribe.argtypes = [ctypes.c_double] |
| 14 |
pyi_timing_thread_subscribe.restype = ctypes.c_int |
| 15 |
|
| 16 |
pyi_timing_thread_get_time = lib.pyi_timing_thread_get_time |
| 17 |
pyi_timing_thread_get_time.argtypes = [] |
| 18 |
pyi_timing_thread_get_time.restype = ctypes.c_double |
| 19 |
|
| 20 |
pyi_timing_thread_get_interval = lib.pyi_timing_thread_get_interval |
| 21 |
pyi_timing_thread_get_interval.argtypes = [] |
| 22 |
pyi_timing_thread_get_interval.restype = ctypes.c_double |
| 23 |
|
| 24 |
pyi_timing_thread_unsubscribe = lib.pyi_timing_thread_unsubscribe |
| 25 |
pyi_timing_thread_unsubscribe.argtypes = [ctypes.c_int] |
| 26 |
pyi_timing_thread_unsubscribe.restype = ctypes.c_int |
| 27 |
|
| 28 |
PYI_TIMING_THREAD_UNKNOWN_ERROR = -1 |
| 29 |
PYI_TIMING_THREAD_TOO_MANY_SUBSCRIBERS = -2 |
| 30 |
|
| 31 |
|
| 32 |
if sys.platform == "win32": |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
WAIT_TIME = 0.03 |
| 40 |
elif os.environ.get("QEMU_EMULATED"): |
| 41 |
|
| 42 |
WAIT_TIME = 0.2 |
| 43 |
else: |
| 44 |
WAIT_TIME = 0.015 |
| 45 |
|
| 46 |
|
| 47 |
@flaky_in_ci |
| 48 |
def test(): |
| 49 |
|
| 50 |
assert pyi_timing_thread_get_interval() == -1 |
| 51 |
|
| 52 |
time_before = pyi_timing_thread_get_time() |
| 53 |
time.sleep(WAIT_TIME) |
| 54 |
assert pyi_timing_thread_get_time() == time_before |
| 55 |
|
| 56 |
|
| 57 |
subscription_id = pyi_timing_thread_subscribe(0.001) |
| 58 |
try: |
| 59 |
assert subscription_id >= 0 |
| 60 |
|
| 61 |
assert pyi_timing_thread_get_interval() == 0.001 |
| 62 |
|
| 63 |
|
| 64 |
busy_wait(WAIT_TIME) |
| 65 |
time_a = pyi_timing_thread_get_time() |
| 66 |
assert time_a > time_before |
| 67 |
busy_wait(WAIT_TIME) |
| 68 |
time_b = pyi_timing_thread_get_time() |
| 69 |
assert time_b > time_a |
| 70 |
|
| 71 |
|
| 72 |
assert pyi_timing_thread_unsubscribe(subscription_id) == 0 |
| 73 |
|
| 74 |
assert pyi_timing_thread_get_interval() == -1 |
| 75 |
|
| 76 |
|
| 77 |
time.sleep(WAIT_TIME) |
| 78 |
time_c = pyi_timing_thread_get_time() |
| 79 |
time.sleep(WAIT_TIME) |
| 80 |
time_d = pyi_timing_thread_get_time() |
| 81 |
assert time_c == time_d |
| 82 |
finally: |
| 83 |
|
| 84 |
pyi_timing_thread_unsubscribe(subscription_id) |
| 85 |
|
| 86 |
|
| 87 |
def test_max_subscribers(): |
| 88 |
subscription_ids = [] |
| 89 |
|
| 90 |
try: |
| 91 |
for i in range(1000): |
| 92 |
subscription_id = pyi_timing_thread_subscribe(0.001) |
| 93 |
assert subscription_id >= 0 |
| 94 |
subscription_ids.append(subscription_id) |
| 95 |
|
| 96 |
|
| 97 |
assert pyi_timing_thread_subscribe(0.001) == PYI_TIMING_THREAD_TOO_MANY_SUBSCRIBERS |
| 98 |
|
| 99 |
|
| 100 |
for subscription_id in subscription_ids: |
| 101 |
assert pyi_timing_thread_get_interval() == 0.001 |
| 102 |
assert pyi_timing_thread_unsubscribe(subscription_id) == 0 |
| 103 |
|
| 104 |
|
| 105 |
assert pyi_timing_thread_get_interval() == -1 |
| 106 |
finally: |
| 107 |
|
| 108 |
while subscription_ids: |
| 109 |
subscription_id = subscription_ids.pop() |
| 110 |
pyi_timing_thread_unsubscribe(subscription_id) |