def test_get_traces_intern_traceback(self): # dummy wrappers to get more useful and identical frames in the traceback def allocate_bytes2(size): return allocate_bytes(size) def allocate_bytes3(size): return allocate_bytes2(size) def allocate_bytes4(size): return allocate_bytes3(size) # Ensure that two identical tracebacks are not duplicated mprofile.stop() mprofile.start(4) obj_size = 123 obj1, obj1_traceback = allocate_bytes4(obj_size) obj2, obj2_traceback = allocate_bytes4(obj_size) traces = mprofile._get_traces() obj1_traceback._frames = tuple(reversed(obj1_traceback._frames)) obj2_traceback._frames = tuple(reversed(obj2_traceback._frames)) trace1 = self.find_trace(traces, obj1_traceback) trace2 = self.find_trace(traces, obj2_traceback) size1, traceback1 = trace1 size2, traceback2 = trace2 self.assertIs(traceback2, traceback1)
def test_profile_1frame(self): mprofile.start(max_frames=1) alloc_obj = object() snap = mprofile.take_snapshot() mprofile.stop() self.assertEqual(len(snap.traces), 1) self.assertEqual(len(snap.traces[0].traceback), 1)
def test_profile_one(self): mprofile.start() alloc_obj = object() snap = mprofile.take_snapshot() mprofile.stop() self.assertEqual(len(snap.traces), 1) frame = snap.traces[0].traceback[-1] self.assertTrue(frame.filename.endswith("test_mprofile.py")) self.assertEqual(frame.name, "test_profile_one") self.assertEqual(frame.firstlineno, 16) self.assertEqual(frame.lineno, 18)
def test_profile_concurrent(self): mprofile.start() t1 = threading.Thread(target=alloc_objects) t1.start() time.sleep(0.001) t2 = threading.Thread(target=alloc_objects) t2.start() time.sleep(0.001) snap = mprofile.take_snapshot() t1.join() mprofile.stop() t2.join()
def test_profile(self): mprofile.start() large_alloc = [object() for _ in range(12)] snap = mprofile.take_snapshot() mprofile.stop() # Snapshot references should remain valid after stopping profiler. found_it = False for trace in snap.traces: for frame in trace.traceback: if frame.name == "test_profile": found_it = True self.assertTrue(found_it)
def test_profile_sampler(self): n = 100000 mprofile.start(sample_rate=1024) alloc_obj = [object() for _ in range(n)] snap = mprofile.take_snapshot() mprofile.stop() self.assertGreaterEqual(len(snap.traces), 0) self.assertLessEqual(len(snap.traces), n) self.assertGreaterEqual(len(snap.traces[0].traceback), 2) stats = snap.statistics("traceback") self.assertGreaterEqual(len(stats), 1) self.assertGreaterEqual(stats[0].count, len(snap.traces)) self.assertGreaterEqual(stats[0].size, sum(t.size for t in snap.traces))
def test_set_traceback_limit(self): obj_size = 10 mprofile.stop() self.assertRaises(ValueError, mprofile.start, -1) mprofile.stop() mprofile.start(10) obj2, obj2_traceback = allocate_bytes(obj_size) traceback = mprofile.get_object_traceback(obj2) self.assertEqual(len(traceback), 10) self.assertEqual(traceback, obj2_traceback) mprofile.stop() mprofile.start(1) obj, obj_traceback = allocate_bytes(obj_size) traceback = mprofile.get_object_traceback(obj) self.assertEqual(len(traceback), 1) self.assertEqual(traceback, obj_traceback)
def setUpClass(cls): mprofile.start(sample_rate=512) cls.host = "localhost" cls.port = 8083 cls.server = start_pprof_server(cls.host, cls.port)
def setUp(self): if mprofile.is_tracing(): self.skipTest("mprofile must be stopped before the test") mprofile.start(1)
def test_is_tracing(self): mprofile.stop() self.assertFalse(mprofile.is_tracing()) mprofile.start() self.assertTrue(mprofile.is_tracing())