def results(self): """Return cProfile results in a dictionary for template context.""" # Make sure nothing is printed to stdout output = StringIO.StringIO() stats = pstats.Stats(self.c_profile, stream=output) stats.sort_stats("cumulative") self.c_profile.create_stats() results = { "raw_stats": base64.b64encode(marshal.dumps(self.c_profile.stats)), "total_call_count": stats.total_calls, "total_time": util.seconds_fmt(stats.total_tt), "calls": [] } width, list_func_names = stats.get_print_list([80]) for func_name in list_func_names: primitive_call_count, total_call_count, total_time, cumulative_time, callers = stats.stats[ func_name] func_desc = pstats.func_std_string(func_name) callers_names = map( lambda func_name: pstats.func_std_string(func_name), callers.keys()) callers_desc = map( lambda name: { "func_desc": name, "func_desc_short": util.short_method_fmt(name) }, callers_names) results["calls"].append({ "primitive_call_count": primitive_call_count, "total_call_count": total_call_count, "cumulative_time": util.seconds_fmt(cumulative_time, 2), "total_time": util.seconds_fmt(total_time, 2), "per_call_cumulative": util.seconds_fmt(cumulative_time / primitive_call_count, 2) if primitive_call_count else "", "func_desc": func_desc, "func_desc_short": util.short_method_fmt(func_desc), "callers_desc": callers_desc, }) output.close() return results
def results(self): """Return cProfile results in a dictionary for template context.""" # Make sure nothing is printed to stdout output = StringIO.StringIO() stats = pstats.Stats(self.c_profile, stream=output) stats.sort_stats("cumulative") self.c_profile.create_stats() results = { "raw_stats": base64.b64encode(marshal.dumps(self.c_profile.stats)), "total_call_count": stats.total_calls, "total_time": util.seconds_fmt(stats.total_tt), "calls": [] } width, list_func_names = stats.get_print_list([80]) for func_name in list_func_names: primitive_call_count, total_call_count, total_time, cumulative_time, callers = stats.stats[func_name] func_desc = pstats.func_std_string(func_name) callers_names = map(lambda func_name: pstats.func_std_string(func_name), callers.keys()) callers_desc = map( lambda name: {"func_desc": name, "func_desc_short": util.short_method_fmt(name)}, callers_names) results["calls"].append({ "primitive_call_count": primitive_call_count, "total_call_count": total_call_count, "cumulative_time": util.seconds_fmt(cumulative_time, 2), "total_time": util.seconds_fmt(total_time, 2), "per_call_cumulative": util.seconds_fmt(cumulative_time / primitive_call_count, 2) if primitive_call_count else "", "func_desc": func_desc, "func_desc_short": util.short_method_fmt(func_desc), "callers_desc": callers_desc, }) output.close() return results
def profiler_results(self): """Return the CPU profiler results for this request, if any. This will return a dictionary containing results for either the sampling profiler, instrumented profiler results, or a simple start/stop timer if both profilers are disabled.""" total_time = util.seconds_fmt(self.end - self.start, 0) results = {"total_time": total_time} if self.instrumented_prof: results.update(self.instrumented_prof.results()) elif self.sampling_prof: results.update(self.sampling_prof.results()) return results