def raw_sql_profiler(): profiler = Profile() for _ in range(10): profiler.runctx( "list(HttcOrder.objects.raw('''SELECT * FROM httc_order WHERE mk_contract_id = 1000 ORDER BY price'''))", locals(), globals()) convert(profiler.getstats(), 'raw_sql_profiler.kgrind')
def generate_profiler_entry(): def func(): a = 1 + 2 return a prof = Profile() prof.runctx("func()", locals(), globals()) return prof.getstats()
def cursor_execute_profiler(): cursor = connection.cursor() profiler = Profile() for _ in range(10): profiler.runctx( "cursor.execute('''SELECT * FROM httc_order WHERE mk_contract_id = 1000 ORDER BY price''')", locals(), globals()) convert(profiler.getstats(), 'cursor_execute_profiler.kgrind')
def _profile(self, goal, func): from cProfile import Profile from pypy.tool.lsprofcalltree import KCacheGrind d = {'func':func} prof = Profile() prof.runctx("res = func()", globals(), d) KCacheGrind(prof).output(open(goal + ".out", "w")) return d['res']
def _profile(self, goal, func): from cProfile import Profile from rpython.tool.lsprofcalltree import KCacheGrind d = {'func': func} prof = Profile() prof.runctx("res = func()", globals(), d) KCacheGrind(prof).output(open(goal + ".out", "w")) return d['res']
def profile(): #cProfile.run('main()','stats') #Tweede test met RGB topo coloring zonder schaduw.py from cProfile import Profile from pyprof2calltree import convert, visualize profiler = Profile() profiler.runctx('main()', locals(), globals()) visualize(profiler.getstats())
def profile_create_offices_for_departement(departement): """ Run create_offices_for_departement with profiling. """ profiler = Profile() command = "create_offices_for_departement('%s')" % departement profiler.runctx(command, locals(), globals()) relative_filename = 'profiling_results/create_index_dpt%s.kgrind' % departement filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), relative_filename) convert(profiler.getstats(), filename)
def _call(self, *args, **kw): profile = RawProfile() def _run(): with DisableGc(): for _ in range(self._iterations): _run.result = super(Profile, self)._call(*args, **kw) profile.runctx('_run()', {}, {'_run': _run}) profile.create_stats() stats = Stats(profile) stats.sort_stats('cumulative') stats.fcn_list = stats.fcn_list[:self._max_lines] self._reporter(stats) return _run.result
def __call__(self, environ, start_response): """ This will be called when the application is being run. This can be either: - a request to the __profiler__ framework to display profiled information, or - a normal request that will be profiled. Returns the WSGI application. """ # If we're not accessing the profiler, profile the request. req = Request(environ) self.profiling_enabled = os.path.exists(ENABLED_FLAG_FILE) if req.path_info_peek() != self.profiler_path.strip('/'): if not self.profiling_enabled: return self.app(environ, start_response) _locals = locals() prof = Profile() start_timestamp = datetime.now() prof.runctx( "app = self.app(environ, start_response)", globals(), _locals) stats = prof.getstats() session = ProfilingSession(stats, environ, start_timestamp) self._backend.add(session) return _locals['app'] req.path_info_pop() # We could import `routes` and use something like that here, but since # not all frameworks use this, it might become an external dependency # that isn't needed. So parse the URL manually using :class:`webob`. query_param = req.path_info_pop() if not query_param: wsgi_app = self.list_profiles(req) elif query_param == "graph": wsgi_app = self.render_graph(req) elif query_param == "media": wsgi_app = self.media(req) elif query_param == "profiles": wsgi_app = self.show_profile(req) elif query_param == "delete": wsgi_app = self.delete_profile(req) else: wsgi_app = HTTPNotFound() return wsgi_app(environ, start_response)
def __call__(self, environ, start_response): """ This will be called when the application is being run. This can be either: - a request to the __profiler__ framework to display profiled information, or - a normal request that will be profiled. Returns the WSGI application. """ # If we're not accessing the profiler, profile the request. req = Request(environ) self.profiling_enabled = os.path.exists(ENABLED_FLAG_FILE) if req.path_info_peek() != self.profiler_path.strip('/'): if not self.profiling_enabled: return self.app(environ, start_response) _locals = locals() prof = Profile() start_timestamp = datetime.now() prof.runctx("app = self.app(environ, start_response)", globals(), _locals) stats = prof.getstats() session = ProfilingSession(stats, environ, start_timestamp) self._backend.add(session) return _locals['app'] req.path_info_pop() # We could import `routes` and use something like that here, but since # not all frameworks use this, it might become an external dependency # that isn't needed. So parse the URL manually using :class:`webob`. query_param = req.path_info_pop() if not query_param: wsgi_app = self.list_profiles(req) elif query_param == "graph": wsgi_app = self.render_graph(req) elif query_param == "media": wsgi_app = self.media(req) elif query_param == "profiles": wsgi_app = self.show_profile(req) elif query_param == "delete": wsgi_app = self.delete_profile(req) else: wsgi_app = HTTPNotFound() return wsgi_app(environ, start_response)
def home_p(request): """Profiled version of home""" prof = Profile() prof = prof.runctx("home(request)", globals(), locals()) stream = StringIO() stats = Stats(prof, stream=stream) stats.sort_stats("time").print_stats(80) log.info("Profile data:\n%s", stream.getvalue()) return HttpResponse(u"OK")
def test_create_graph(self): """ Test that a graph gets generated for a test function """ def test_func(): pass prof = Profile() prof.runctx("test_func()", locals(), globals()) graph = linesman.create_graph(prof.getstats()) # We should only ever have three items here assert_equals(len(graph), 3) # Assert that the three items we have are as expected assert_equals(graph.nodes(), ['<string>.<module>', 'linesman.tests.test_graphs.test_func', "<method 'disable' of '_lsprof.Profiler' objects>"]) # Assert that the correct edges are set-up assert_equals([('<string>.<module>', 'linesman.tests.test_graphs.test_func')], graph.edges())
def update_data_profiling_wrapper(create_full, create_partial, disable_parallel_computing=False): if Profiling.ACTIVATED: logger.info("STARTED run with profiling") profiler = Profile() profiler.runctx( "update_data(create_full, create_partial, disable_parallel_computing)", locals(), globals()) relative_filename = 'profiling_results/create_index_run.kgrind' filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), relative_filename) convert(profiler.getstats(), filename) logger.info( "COMPLETED run with profiling: exported profiling result as %s", filename) else: logger.info("STARTED run without profiling") update_data(create_full, create_partial, disable_parallel_computing) logger.info("COMPLETED run without profiling")
def run(self): from cProfile import Profile from pstats import Stats prof = Profile() if hasattr(prof, "calibrate"): self.calibrate(prof) prof.runctx("self.do_run()", globals=globals(), locals=locals()) fd, path = mkstemp(".profile") prof.dump_stats(path) stats = Stats(path) stats.sort_stats("cumulative").print_stats(15) stats.sort_stats("time").print_stats(15)
def test_create_graph(self): """ Test that a graph gets generated for a test function """ def test_func(): pass prof = Profile() prof.runctx("test_func()", locals(), globals()) graph = linesman.create_graph(prof.getstats()) # We should only ever have three items here assert_equals(len(graph), 3) # Assert that the three items we have are as expected assert_equals(graph.nodes(), [ '<string>.<module>', 'linesman.tests.test_graphs.test_func', "<method 'disable' of '_lsprof.Profiler' objects>" ]) # Assert that the correct edges are set-up assert_equals( [('<string>.<module>', 'linesman.tests.test_graphs.test_func')], graph.edges())
def profile(f=None): from cProfile import Profile prof = Profile() try: prof = prof.runctx("f()", globals(), locals()) except SystemExit: pass import pstats stats = pstats.Stats(prof) stats.strip_dirs() stats.sort_stats('time') stats.print_stats(20)
def profile(f = None): from cProfile import Profile prof = Profile() try: prof = prof.runctx("f()", globals(), locals()) except SystemExit: pass import pstats stats = pstats.Stats(prof) stats.strip_dirs() stats.sort_stats('time') stats.print_stats(20)
def main(): N = 100000 # Number of tests # Different argument types variants = [ (1, 3), ((1, 3), ), ("fox", ), ] args = [] for i in range(N): args.append(variants[randint(0, len(variants) - 1)]) def run_ovl(): for arg in args: func_ovl(*arg) def run_normal(): for arg in args: func_normal(*arg) print("Running benchmark...") time_ovl = timeit.timeit(run_ovl, number=1) / N time_normal = timeit.timeit(run_normal, number=1) / N profiler = Profile() profiler.runctx("run_ovl()", globals(), locals()) convert(profiler.getstats(), "C:/Users/andreasxp/Desktop/callgrind.profile") print(f"Average over {N} runs:") print( f"Overloaded function: {time_ovl * 1000000:.2f} mcs ({time_ovl / time_normal:.2f}x)" ) print(f"Non-overloaded function: {time_normal * 1000000:.2f} mcs")
def main(): """Profiling main function.""" profiler = Profile() profiler = profiler.runctx("run_app()", globals(), locals()) iostream = StringIO() stats = Stats(profiler, stream=iostream) stats.sort_stats("time") # or cumulative stats.print_stats(80) # 80 == how many to print # optional: # stats.print_callees() # stats.print_callers() logging.info("Profile data:\n%s", iostream.getvalue())
event.loop() while event.has_next(): e, flags, fd = event.next() e.data(flags) except: end = time.time() print '#set/sec', N / (end - start) raise from cProfile import Profile prof = Profile() try: prof = prof.runctx("main()", globals(), locals()) except Exception: pass import pstats stats = pstats.Stats(prof) stats.strip_dirs() stats.sort_stats('time') stats.print_stats(20) #with gil, using deque #=============================================================================== # henk@henk-worktop:~/workspace/concurrence{speedup}$ stackless sandbox/test_basic.py # #set/sec 27512.9132082 # henk@henk-worktop:~/workspace/concurrence{speedup}$ stackless sandbox/test_basic.py # #set/sec 32166.8986723
def run(cmd, globals, locals): prof = Profile() try: prof.runctx(cmd, globals, locals) finally: show(prof)
from xml.etree import ElementTree from cProfile import Profile import pstats xml_content = '<a>\n' + '\t<b/><c><d>text</d></c>\n' * 100 + '</a>' profiler = Profile() profiler.runctx("ElementTree.fromstring(xml_content)", locals(), globals()) from pyprof2calltree import convert, visualize stats = pstats.Stats(profiler) visualize(stats) # run kcachegrind
"C_lambda": C_LAMBDA_TRUE, "delta_S": DELTA_S_TRUE } price_path = preisSim(param_true) # p_true_SS = all_summary_stats(price_path, price_path) return (price_path) p_true_SS = main() p_true_SS.to_csv("new_test_case.csv", index=False, header=False) original = pd.read_csv("original_test_case.csv", header=None) new_case = pd.read_csv("new_test_case.csv", header=None) print( all( round(original.apply(float, axis=1), 0) == round( new_case.apply(float, axis=1), 0))) profiler = Profile() profiler.runctx("main()", locals(), globals()) # from pyprof2calltree import convert, visualize visualize(profiler.getstats()) # run kcachegrind convert(profiler.getstats(), 'profiling_results.kgrind') # save for later
from xml.etree import ElementTree from cProfile import Profile import pstats xml_content = '<a>\n' + '\t<b/><c><d>text</d></c>\n' * 100 + '</a>' profiler = Profile() profiler.runctx( "ElementTree.fromstring(xml_content)", locals(), globals()) from pyprof2calltree import convert, visualize stats = pstats.Stats(profiler) visualize(stats) # run kcachegrind