示例#1
0
def test_new_interval():
    r = recorder.Recorder()
    c = stack.StackCollector(r)
    new_interval = c._compute_new_interval(1000000)
    assert new_interval == 0.049
    new_interval = c._compute_new_interval(2000000)
    assert new_interval == 0.098
    c = stack.StackCollector(r, max_time_usage_pct=10)
    new_interval = c._compute_new_interval(200000)
    assert new_interval == 0.01
    new_interval = c._compute_new_interval(1)
    assert new_interval == c.MIN_INTERVAL_TIME
示例#2
0
def _build_default_collectors():
    r = recorder.Recorder()
    return [
        stack.StackCollector(r),
        memory.MemoryCollector(r),
        exceptions.UncaughtExceptionCollector(r),
        threading.LockCollector(r),
    ]
示例#3
0
def test_collect_truncate():
    r = recorder.Recorder()
    c = stack.StackCollector(r, nframes=5)
    c.start()
    func1()
    while not r.events[stack.StackSampleEvent]:
        pass
    c.stop()
    e = r.events[stack.StackSampleEvent][0]
    assert e.nframes > c.nframes
    assert len(e.frames) == c.nframes
示例#4
0
def test_collect_once():
    r = recorder.Recorder()
    s = stack.StackCollector(r)
    s._init()
    all_events = s._collect()
    assert len(all_events) == 2
    e = all_events[0][0]
    assert e.thread_id > 0
    # Thread name is None with gevent
    assert isinstance(e.thread_name, (str, type(None)))
    assert len(e.frames) > 1
    assert e.frames[0][0].endswith(".py")
    assert e.frames[0][1] > 0
    assert isinstance(e.frames[0][2], str)
示例#5
0
def _test_memory_ignore(ignore):
    r = recorder.Recorder()
    # Start a stack collector so it allocates memory
    with stack.StackCollector(r):
        r = recorder.Recorder()
        c = memory.MemoryCollector(r, ignore_profiler=ignore, capture_pct=100)
        while not r.events[memory.MemorySampleEvent]:
            c.collect()
            _ = _alloc()
    events = r.events[memory.MemorySampleEvent]
    files = {
        frame.filename
        for event in events for trace in event.snapshot.traces
        for frame in trace.traceback
    }
    return files
示例#6
0
def test_stress_threads():
    NB_THREADS = 20

    threads = []
    for i in range(NB_THREADS):
        t = threading.Thread(target=_f0)  # noqa: E149,F821
        t.start()
        threads.append(t)

    s = stack.StackCollector(recorder=recorder.Recorder())
    s._init()
    number = 10000
    exectime = timeit.timeit(s.collect, number=number)
    print("%.3f ms per call" % (1000.0 * exectime / number))
    for t in threads:
        t.join()
示例#7
0
def test_exception_collection():
    r = recorder.Recorder()
    c = stack.StackCollector(r)
    c.start()
    try:
        raise ValueError("hello")
    except Exception:
        sleep(1)
    c.stop()

    exception_events = r.events[stack.StackExceptionSampleEvent]
    assert len(exception_events) >= 1
    e = exception_events[0]
    assert e.timestamp > 0
    assert e.sampling_period > 0
    if not TESTING_GEVENT:
        assert e.thread_id == _thread.get_ident()
        assert e.thread_name == "MainThread"
    assert e.frames == [(__file__, 205, "test_exception_collection")]
    assert e.nframes == 1
    assert e.exc_type == ValueError
示例#8
0
def test_max_time_usage_over():
    r = recorder.Recorder()
    with pytest.raises(ValueError):
        stack.StackCollector(r, max_time_usage_pct=200)