1# Copyright 2013 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.
4
5from telemetry import benchmark
6
7from py_utils import discover
8
9
10class ProjectConfig(object):
11  """Contains information about the benchmark runtime environment.
12
13  Attributes:
14    top_level_dir: A dir that contains benchmark, page test, and/or story
15        set dirs and associated artifacts.
16    benchmark_dirs: A list of dirs containing benchmarks.
17    benchmark_aliases: A dict of name:alias string pairs to be matched against
18        exactly during benchmark selection.
19    client_configs: A list of paths to a ProjectDependencies json files.
20    default_chrome_root: A path to chromium source directory. Many telemetry
21      features depend on chromium source tree's presence and those won't work
22      in case this is not specified.
23    expectations_file: A path to expectations file.
24  """
25  def __init__(self, top_level_dir, benchmark_dirs=None,
26               benchmark_aliases=None, client_configs=None,
27               default_chrome_root=None, expectations_files=None):
28    self._top_level_dir = top_level_dir
29    self._benchmark_dirs = benchmark_dirs or []
30    self._benchmark_aliases = benchmark_aliases or dict()
31    self._client_configs = client_configs or []
32    self._default_chrome_root = default_chrome_root
33    self._expectations_files = expectations_files or []
34    self._benchmarks = None
35
36  @property
37  def top_level_dir(self):
38    return self._top_level_dir
39
40  @property
41  def start_dirs(self):
42    return self._benchmark_dirs
43
44  @property
45  def benchmark_dirs(self):
46    return self._benchmark_dirs
47
48  @property
49  def benchmark_aliases(self):
50    return self._benchmark_aliases
51
52  @property
53  def client_configs(self):
54    return self._client_configs
55
56  @property
57  def default_chrome_root(self):
58    return self._default_chrome_root
59
60  @property
61  def expectations_files(self):
62    return self._expectations_files
63
64  def AdjustStartupFlags(self, args):
65    """Returns a new list of adjusted startup flags.
66
67    Subclasses should override this method to change startup flags before use.
68    """
69    return args
70
71  def GetBenchmarks(self):
72    """Return a list of all benchmark classes found in this configuration."""
73    if self._benchmarks is None:
74      benchmarks = []
75      for search_dir in self.benchmark_dirs:
76        benchmarks.extend(discover.DiscoverClasses(
77            search_dir,
78            self.top_level_dir,
79            benchmark.Benchmark,
80            index_by_class_name=True).values())
81      self._benchmarks = benchmarks
82    return list(self._benchmarks)
83
84  def GetBenchmarkByName(self, benchmark_name):
85    """Find a benchmark by an exact name match.
86
87    Args:
88      benchmark_name: The name or a alias of a benchmark.
89
90    Returns:
91      The benchmark class if an exact match for the name is found, or None
92      otherwise.
93    """
94    # Allow using aliases to find benchmarks.
95    benchmark_name = self.benchmark_aliases.get(benchmark_name, benchmark_name)
96    for benchmark_class in self.GetBenchmarks():
97      if benchmark_name == benchmark_class.Name():
98        return benchmark_class
99    return None
100