| 1 | import ctypes |
| 2 | import time |
| 3 | |
| 4 | import pytest |
| 5 | |
| 6 | import pyinstrument.low_level.stat_profile as native_module |
| 7 | |
| 8 | lib = ctypes.CDLL(native_module.__file__) |
| 9 | |
| 10 | pyi_floatclock = lib.pyi_floatclock |
| 11 | pyi_floatclock.argtypes = [ctypes.c_int] |
| 12 | pyi_floatclock.restype = ctypes.c_double |
| 13 | |
| 14 | |
| 15 | def test_floatclock(): |
| 16 | time_a = pyi_floatclock(0) |
| 17 | time.sleep(0.001) |
| 18 | time_b = pyi_floatclock(0) |
| 19 | assert time_b > time_a |
| 20 | |
| 21 | |
| 22 | def test_is_in_seconds(): |
| 23 | floatclock_time_a = pyi_floatclock(0) |
| 24 | time_a = time.time() |
| 25 | |
| 26 | time.sleep(0.1) |
| 27 | |
| 28 | floatclock_time_b = pyi_floatclock(0) |
| 29 | time_b = time.time() |
| 30 | |
| 31 | floatclock_duration = floatclock_time_b - floatclock_time_a |
| 32 | duration = time_b - time_a |
| 33 | |
| 34 | assert floatclock_duration == pytest.approx(duration, rel=0.1) |