def testRunInternalTBMv3Metric(self):
   """Run metric that is compiled into Trace Processor."""
   # This won't produce any histograms because trace_metadata proto is not
   # annotated. Check only that it doesn't throw errors. 'trace_metadata'
   # metric is relatively unlikely to be removed from Perfetto, but if it
   # is, we will have to pick a different metric.
   run_tbmv3_metric.Main([
       '--trace', self.trace_path,
       '--metric', 'trace_metadata',
       '--outfile', self.outfile_path,
   ])
  def testRunAllTbmv3Metrics(self):
    """Run all existing TBMv3 metrics on an empty trace.

    This test checks for syntax errors in SQL and proto files.
    """
    for filename in os.listdir(trace_processor.METRICS_PATH):
      name, ext = os.path.splitext(filename)
      if ext == '.sql':
        run_tbmv3_metric.Main([
            '--trace', self.trace_path,
            '--metric', name,
            '--outfile', self.outfile_path,
        ])
  def testRunTbmv3MetricOnDummyMetric(self):
    run_tbmv3_metric.Main([
        '--trace', self.trace_path,
        '--metric', 'dummy_metric',
        '--outfile', self.outfile_path,
    ])

    with open(self.outfile_path) as f:
      results = json.load(f)

    out_histograms = histogram_set.HistogramSet()
    out_histograms.ImportDicts(results)

    hist = out_histograms.GetHistogramNamed(DUMMY_HISTOGRAM_NAME)
    self.assertEqual(hist.unit, DUMMY_HISTOGRAM_UNIT)
    self.assertEqual(hist.num_values, 1)
    self.assertEqual(hist.average, 42)
def RunTBMv3Metric(tbmv3_name, proto_trace_filename, traces_dir,
                   trace_processor_path):
    with tempfile.NamedTemporaryFile(dir=traces_dir,
                                     suffix='_tbmv3.json',
                                     delete=False) as out_file:
        pass
        # Open temp file and close it so it's written to disk.
    run_tbmv3_metric.Main([
        '--trace',
        proto_trace_filename,
        '--metric',
        tbmv3_name,
        '--outfile',
        out_file.name,
        '--trace-processor-path',
        trace_processor_path,
    ])
    logging.debug('Saved TBMv3 metric to %s' % out_file.name)
    return out_file.name