| 1 |
import time |
| 2 |
import pytest |
| 3 |
import sys |
| 4 |
vmprof = pytest.importorskip('vmprof') |
| 5 |
greenlet = pytest.importorskip('greenlet') |
| 6 |
|
| 7 |
IS_32_BIT = sys.maxint == 2**31-1 |
| 8 |
|
| 9 |
def count_samples(filename): |
| 10 |
stats = vmprof.read_profile(filename) |
| 11 |
return len(stats.profiles) |
| 12 |
|
| 13 |
def cpuburn(duration): |
| 14 |
end = time.time() + duration |
| 15 |
while time.time() < end: |
| 16 |
pass |
| 17 |
|
| 18 |
|
| 19 |
@pytest.mark.xfail(IS_32_BIT, reason="vmprof fails to write profiles with negative addresses") |
| 20 |
def test_sampling_inside_callback(tmpdir): |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
G = greenlet.greenlet(cpuburn) |
| 25 |
fname = tmpdir.join('log.vmprof') |
| 26 |
with fname.open('w+b') as f: |
| 27 |
vmprof.enable(f.fileno(), 1/250.0) |
| 28 |
G.switch(0.1) |
| 29 |
vmprof.disable() |
| 30 |
|
| 31 |
samples = count_samples(str(fname)) |
| 32 |
|
| 33 |
assert 23 < samples < 27 |