Ejemplo n.º 1
0
 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:
             data = stats.print_stats()
         return data, [('content-type', self.format_dict[output_format])]
     except ODFLIBNotInstalled:
         raise
     except Exception as ex:
         raise ProfileException(_('Data download error: %s') % ex)
Ejemplo n.º 2
0
 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 _unused 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)
         with tempfile.TemporaryFile() as profile_img:
             plt.savefig(profile_img, format='png', dpi=300)
             profile_img.seek(0)
             data = profile_img.read()
             return data, [('content-type', 'image/jpg')]
     except Exception as ex:
         raise ProfileException(_('plotting results failed due to %s') % ex)