1# Copyright 2016 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4import os
5
6from tracing.mre import function_handle
7from tracing.mre import gtest_progress_reporter
8from tracing.mre import map_runner
9from tracing.mre import file_handle
10from tracing.mre import job as job_module
11
12_METRIC_MAP_FUNCTION_FILENAME = 'metric_map_function.html'
13
14_METRIC_MAP_FUNCTION_NAME = 'metricMapFunction'
15
16def _GetMetricsDir():
17  return os.path.dirname(os.path.abspath(__file__))
18
19def _GetMetricRunnerHandle(metrics):
20  assert isinstance(metrics, list)
21  for metric in metrics:
22    assert isinstance(metric, basestring)
23  metrics_dir = _GetMetricsDir()
24  metric_mapper_path = os.path.join(metrics_dir, _METRIC_MAP_FUNCTION_FILENAME)
25
26  modules_to_load = [function_handle.ModuleToLoad(filename=metric_mapper_path)]
27  options = {'metrics': metrics}
28  map_function_handle = function_handle.FunctionHandle(
29      modules_to_load, _METRIC_MAP_FUNCTION_NAME, options)
30
31  return job_module.Job(map_function_handle, None)
32
33def RunMetric(filename, metrics, extra_import_options=None):
34  result = RunMetricOnTraces([filename], metrics, extra_import_options)
35  return result[filename]
36
37def RunMetricOnTraces(filenames, metrics,
38                      extra_import_options=None):
39  trace_handles = [
40      file_handle.URLFileHandle(f, 'file://%s' % f) for f in filenames]
41  job = _GetMetricRunnerHandle(metrics)
42  runner = map_runner.MapRunner(
43      trace_handles, job, extra_import_options=extra_import_options,
44      progress_reporter=gtest_progress_reporter.GTestProgressReporter())
45  map_results = runner.RunMapper()
46  return map_results
47