def download(self, log_files, sort='time', limit=-1, nfl_filter='', output_format='default'): if len(log_files) == 0: raise NotFoundException(_('no log file found')) try: nfl_esc = nfl_filter.replace('(', '\(').replace(')', '\)') # remove the slash that is intentionally added in the URL # to avoid failure of filtering stats data. if nfl_esc.startswith('/'): nfl_esc = nfl_esc[1:] stats = Stats2(*log_files) stats.sort_stats(sort) if output_format == 'python': data = self.format_source_code(nfl_filter) elif output_format == 'json': data = stats.to_json(nfl_esc, limit) elif output_format == 'csv': data = stats.to_csv(nfl_esc, limit) elif output_format == 'ods': data = stats.to_ods(nfl_esc, limit) else: profile_tmp_all = tempfile.mktemp('.profile', 'all') stats.dump_stats(profile_tmp_all) data = open(profile_tmp_all).read() os.remove(profile_tmp_all) return data, [('content-type', self.format_dict[output_format])] except ODFLIBNotInstalled as ex: raise ex except Exception as ex: raise ProfileException(_('Data download error: %s') % ex)
def index_page(self, log_files=None, sort='time', limit=-1, fulldirs=0, nfl_filter='', profile_id='current', url='#'): headers = [('content-type', 'text/html')] if len(log_files) == 0: return empty_description, headers try: stats = Stats2(*log_files) except (IOError, ValueError): raise DataLoadFailure(_('Can not load profile data from %s.') % log_files) if not fulldirs: stats.strip_dirs() stats.sort_stats(sort) nfl_filter_esc =\ nfl_filter.replace('(', '\(').replace(')', '\)') amount = [nfl_filter_esc, limit] if nfl_filter_esc else [limit] profile_html = self.generate_stats_html(stats, self.app_path, profile_id, *amount) description = "Profiling information is generated by using\ '%s' profiler." % self.profile_module sort_repl = '<option value="%s">' % sort sort_selected = '<option value="%s" selected>' % sort sort = sort_tmpl.replace(sort_repl, sort_selected) plist = ''.join(['<option value="%s">%s</option>' % (p, p) for p in self.profile_log.get_all_pids()]) profile_element = string.Template(profile_tmpl).substitute( {'profile_list': plist}) profile_repl = '<option value="%s">' % profile_id profile_selected = '<option value="%s" selected>' % profile_id profile_element = profile_element.replace(profile_repl, profile_selected) limit_repl = '<option value="%s">' % limit limit_selected = '<option value="%s" selected>' % limit limit = limit_tmpl.replace(limit_repl, limit_selected) fulldirs_checked = 'checked' if fulldirs else '' fulldirs_element = string.Template(fulldirs_tmpl).substitute( {'fulldir_checked': fulldirs_checked}) nfl_filter_element = string.Template(nfl_filter_tmpl).\ substitute({'nfl_filter': nfl_filter}) form_elements = string.Template(formelements_tmpl).substitute( {'description': description, 'action': url, 'profile': profile_element, 'sort': sort, 'limit': limit, 'fulldirs': fulldirs_element, 'nfl_filter': nfl_filter_element, } ) content = string.Template(index_tmpl).substitute( {'formelements': form_elements, 'action': url, 'description': description, 'profilehtml': profile_html, }) return content, headers
def plot(self, log_files, sort='time', limit=10, nfl_filter='', metric_selected='cc', plot_type='bar'): if not PLOTLIB_INSTALLED: raise PLOTLIBNotInstalled(_('python-matplotlib not installed.')) if len(log_files) == 0: raise NotFoundException(_('no log file found')) try: stats = Stats2(*log_files) stats.sort_stats(sort) stats_dict = stats.stats __, func_list = stats.get_print_list([nfl_filter, limit]) nfls = [] performance = [] names = { 'nc': 'Total Call Count', 'cc': 'Primitive Call Count', 'tt': 'Total Time', 'ct': 'Cumulative Time' } for func in func_list: cc, nc, tt, ct, __ = stats_dict[func] metric = {'cc': cc, 'nc': nc, 'tt': tt, 'ct': ct} nfls.append(func[2]) performance.append(metric[metric_selected]) y_pos = range(len(nfls)) error = [random.random() for __ in y_pos] plt.clf() if plot_type == 'pie': plt.pie(x=performance, explode=None, labels=nfls, autopct='%1.1f%%') else: plt.barh(y_pos, performance, xerr=error, align='center', alpha=0.4) plt.yticks(y_pos, nfls) plt.xlabel(names[metric_selected]) plt.title('Profile Statistics (by %s)' % names[metric_selected]) #plt.gcf().tight_layout(pad=1.2) profile_img = tempfile.TemporaryFile() plt.savefig(profile_img, format='png', dpi=300) profile_img.seek(0) data = profile_img.read() os.close(profile_img) return data, [('content-type', 'image/jpg')] except Exception as ex: raise ProfileException(_('plotting results failed due to %s') % ex)