def test_gzip_call(self): p = vmprof.Profiler() with p.measure(native=True): for i in range(1000): self.lib.native_gzipgzipgzip() stats = p.get_stats() top = stats.get_top(stats.profiles) pp = PrettyPrinter() pp._print_tree(stats.get_tree()) def walk(parent): if parent is None or len(parent.children) == 0: return False if 'n:native_gzipgzipgzip:' in parent.name: return True for child in parent.children.values(): if 'n:native_gzipgzipgzip:' in child.name: p = float(child.count) / parent.count assert p >= 0.3 # usually bigger than 0.4 return True else: found = walk(child) if found: return True parent = stats.get_tree() assert walk(parent)
def test_nested_call(): prof = vmprof.Profiler() with prof.measure(): function_bar() # now jitted, on pypy with prof.measure(): function_bar() stats = prof.get_stats() tprof = stats.top_profile() d = dict(tprof) assert d[bar_full_name] > 0 assert d[foo_full_name] > 0 for k, v in stats.adr_dict.items(): if v == bar_full_name: bar_adr = k break names = [stats._get_name(i[0]) for i in stats.function_profile(bar_adr)[0]] if '__pypy__' in sys.builtin_module_names: names.sort() assert len([x for x in names if str(x).startswith('jit:')]) > 0 assert len([x for x in names if x == foo_full_name]) == 1 else: assert foo_full_name in names t = stats.get_tree() while 'function_bar' not in t.name: t = t[''] assert len(t.children) == 1 assert 'function_foo' in t[''].name if PY3K: assert len(t[''].children) == 1 assert '<listcomp>' in t[''][''].name else: assert len(t[''].children) == 0
def test_enable_disable(): prof = vmprof.Profiler() with prof.measure(): function_foo() stats = prof.get_stats() d = dict(stats.top_profile()) assert d[foo_full_name] > 0
def test_multithreaded(): if '__pypy__' in sys.builtin_module_names or PY3K: py.test.skip("not supported on pypy and python3 just yet") import threading finished = [] def f(): for k in range(1000): l = [a for a in xrange(COUNT)] finished.append("foo") threads = [threading.Thread(target=f), threading.Thread(target=f)] prof = vmprof.Profiler() with prof.measure(): for t in threads: t.start() f() for t in threads: t.join() stats = prof.get_stats() all_ids = set([x[2] for x in stats.profiles]) cur_id = list(all_ids)[0] assert len(all_ids) in (3, 4) # maybe 0 lgt1 = len([x[2] for x in stats.profiles if x[2] == cur_id]) total = len(stats.profiles) # between 33-10% and 33+10% is within one profile # this is too close of a call - thread scheduling can leave us # unlucky, especially on badly behaved systems # assert (0.23 * total) <= lgt1 <= (0.43 * total) assert len(finished) == 3
def test_multithreaded(): if '__pypy__' in sys.builtin_module_names: py.test.skip("not supported on pypy just yet") import threading finished = [] def f(): t0 = time.time() while time.time() - t0 < 1.5: pass # busy loop finished.append("foo") threads = [threading.Thread(target=f), threading.Thread(target=f)] prof = vmprof.Profiler() with prof.measure(): for t in threads: t.start() f() for t in threads: t.join() stats = prof.get_stats() all_ids = set([x[2] for x in stats.profiles]) cur_id = threading.currentThread().ident assert all_ids == set( [threading.currentThread().ident, threads[0].ident, threads[1].ident]) lgt1 = len([x[2] for x in stats.profiles if x[2] == cur_id]) total = len(stats.profiles) # between 33-10% and 33+10% is within one profile assert (0.23 * total) <= lgt1 <= (0.43 * total) assert len(finished) == 3
def test_vmprof_real_time(): prof = vmprof.Profiler() with prof.measure(real_time=True): functime_foo() stats = prof.get_stats() tprof = stats.top_profile() d = dict(tprof) assert d[foo_time_name] > 0
def test_get_runtime(self): p = vmprof.Profiler() with p.measure(): time.sleep(2.5) stats = p.get_stats() micros = stats.get_runtime_in_microseconds() ts = stats.end_time - stats.start_time print(ts) assert 2500000 <= micros <= 3000000
def test_vmprof_real_time_threaded(insert_foo, remove_bar): import threading prof = vmprof.Profiler() wait = 0.5 thread = threading.Thread(target=functime_foo, args=[wait, insert_foo]) with prof.measure(period=0.25, real_time=True): thread.start() functime_bar(wait, remove_bar) thread.join() stats = prof.get_stats() tprof = stats.top_profile() d = dict(tprof) assert insert_foo == (foo_time_name in d) assert remove_bar != (bar_time_name in d)
def test_memory_measurment(): if not sys.platform.startswith('linux') or '__pypy__' in sys.builtin_module_names: py.test.skip("unsupported platform") def function_foo(): all = [] for k in range(1000): all.append([a for a in xrange(COUNT)]) return all def function_bar(): return function_foo() prof = vmprof.Profiler() with prof.measure(memory=True): function_bar() prof.get_stats()
def test_nested_call(): prof = vmprof.Profiler() with prof.measure(): function_bar() # now jitted with prof.measure(): function_bar() stats = prof.get_stats() tprof = stats.top_profile() d = dict(tprof) assert d[bar_full_name] > 0 assert d[foo_full_name] > 0 for k, v in stats.adr_dict.iteritems(): if v == bar_full_name: bar_adr = k break assert stats._get_name( stats.function_profile(bar_adr)[0][0][0]) == foo_full_name
def test_start_end_time(): prof = vmprof.Profiler() before_profile = datetime.now() if sys.platform == 'win32': # it seems that the windows implementation of vmp_write_time_now # is borken, and cuts of some micro second precision. import time time.sleep(1) with prof.measure(): function_foo() after_profile = datetime.now() stats = prof.get_stats() s = stats.start_time e = stats.end_time assert before_profile <= s and s <= after_profile assert s <= e assert e <= after_profile and s <= after_profile assert before_profile <= after_profile assert before_profile <= e
def test_insert_other_real_time_thread(insert_foo, remove_bar): import threading prof = vmprof.Profiler() wait = 0.5 # This test is the same as above, except that we manually add/remove # all threads explicitly by id from the main thread. thread = threading.Thread(target=functime_foo, args=[wait, False]) with prof.measure(period=0.25, real_time=True): thread.start() if insert_foo: vmprof.insert_real_time_thread(thread.ident) if remove_bar: vmprof.remove_real_time_thread(threading.current_thread().ident) functime_bar(wait, False) thread.join() stats = prof.get_stats() tprof = stats.top_profile() d = dict(tprof) assert insert_foo == (foo_time_name in d) assert remove_bar != (bar_time_name in d)
def test_multithreaded(): if '__pypy__' in sys.builtin_module_names: py.test.skip("not supported on pypy just yet") import threading finished = [] def f(): for k in range(1000): l = [a for a in xrange(COUNT)] finished.append("foo") threads = [threading.Thread(target=f), threading.Thread(target=f)] prof = vmprof.Profiler() with prof.measure(): for t in threads: t.start() f() for t in threads: t.join() stats = prof.get_stats() all_ids = set([x[2] for x in stats.profiles]) if sys.platform == 'darwin': # on travis CI, these mac builds sometimes fail because of scheduling # issues. Having only 1 thread id is legit, which means that # only one thread has been interrupted. (Usually 2 are at least in this list) assert len(all_ids) >= 1 else: assert len(all_ids) in (3, 4) # maybe 0 #cur_id = list(all_ids)[0] #lgt1 = len([x[2] for x in stats.profiles if x[2] == cur_id]) #total = len(stats.profiles) # between 33-10% and 33+10% is within one profile # this is too close of a call - thread scheduling can leave us # unlucky, especially on badly behaved systems # assert (0.23 * total) <= lgt1 <= (0.43 * total) assert len(finished) == 3
def test_vmprof_real_time_many_threads(): import threading prof = vmprof.Profiler() wait = 0.5 # 12 is chosen to force multiple reallocs of the thread_size_step. n_threads = 12 threads = [] for _ in range(n_threads): thread = threading.Thread(target=functime_foo, args=[wait, True]) threads.append(thread) with prof.measure(period=0.1, real_time=True): for thread in threads: thread.start() functime_bar(wait, False) for thread in threads: thread.join() stats = prof.get_stats() tprof = stats.top_profile() d = dict(tprof) assert foo_time_name in d assert bar_time_name in d
def test_nested_call(): prof = vmprof.Profiler() with prof.measure(): function_bar() # now jitted, on pypy with prof.measure(): function_bar() stats = prof.get_stats() tprof = stats.top_profile() d = dict(tprof) assert d[bar_full_name] > 0 assert d[foo_full_name] > 0 for k, v in stats.adr_dict.items(): if v == bar_full_name: bar_adr = k break names = [stats._get_name(i[0]) for i in stats.function_profile(bar_adr)[0]] if len(names) == 1: # cpython assert names == [foo_full_name] else: names.sort() assert names[1] == foo_full_name assert names[0].startswith('jit:')