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
def _build_default_collectors(): r = recorder.Recorder() return [ stack.StackCollector(r), memory.MemoryCollector(r), exceptions.UncaughtExceptionCollector(r), threading.LockCollector(r), ]
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
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)
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
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()
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
def test_max_time_usage_over(): r = recorder.Recorder() with pytest.raises(ValueError): stack.StackCollector(r, max_time_usage_pct=200)