experiment_factory.py revision f395c26437cbdabc2960447fba89b226f4409e82
1#!/usr/bin/python
2
3# Copyright 2011 Google Inc. All Rights Reserved.
4
5from benchmark import Benchmark
6from experiment import Experiment
7from label import Label
8from results_cache import CacheConditions
9
10
11class ExperimentFactory(object):
12  """Factory class for building an Experiment, given an ExperimentFile as input.
13
14  This factory is currently hardcoded to produce an experiment for running
15  ChromeOS benchmarks, but the idea is that in the future, other types
16  of experiments could be produced.
17  """
18
19  def GetExperiment(self, experiment_file, working_directory):
20    """Construct an experiment from an experiment file."""
21    global_settings = experiment_file.GetGlobalSettings()
22    experiment_name = global_settings.GetField("name")
23    remote = global_settings.GetField("remote")
24    rerun_if_failed = global_settings.GetField("rerun_if_failed")
25    chromeos_root = global_settings.GetField("chromeos_root")
26
27    # Default cache hit conditions. The image checksum in the cache and the
28    # computed checksum of the image must match. Also a cache file must exist.
29    cache_conditions = [CacheConditions.CACHE_FILE_EXISTS,
30                        CacheConditions.CHECKSUMS_MATCH]
31    if global_settings.GetField("rerun_if_failed"):
32      cache_conditions.append(CacheConditions.RUN_SUCCEEDED)
33    if global_settings.GetField("rerun"):
34      cache_conditions.append(CacheConditions.FALSE)
35    if global_settings.GetField("exact_remote"):
36      cache_conditions.append(CacheConditions.MACHINES_MATCH)
37
38    # Construct benchmarks.
39    benchmarks = []
40    all_benchmark_settings = experiment_file.GetSettings("benchmark")
41    for benchmark_settings in all_benchmark_settings:
42      benchmark_name = benchmark_settings.name
43      autotest_name = benchmark_settings.GetField("autotest_name")
44      if not autotest_name:
45        autotest_name = benchmark_name
46      autotest_args = benchmark_settings.GetField("autotest_args")
47      iterations = benchmark_settings.GetField("iterations")
48      outlier_range = benchmark_settings.GetField("outlier_range")
49      perf_args = benchmark_settings.GetField("perf_args")
50      benchmark = Benchmark(benchmark_name, autotest_name, autotest_args,
51                            iterations, outlier_range, perf_args)
52      benchmarks.append(benchmark)
53
54    # Construct labels.
55    labels = []
56    all_label_settings = experiment_file.GetSettings("label")
57    for label_settings in all_label_settings:
58      label_name = label_settings.name
59      image = label_settings.GetField("chromeos_image")
60      chromeos_root = label_settings.GetField("chromeos_root")
61      board = label_settings.GetField("board")
62      label = Label(label_name, image, chromeos_root, board)
63      labels.append(label)
64
65    email = global_settings.GetField("email")
66
67    experiment = Experiment(experiment_name, remote, rerun_if_failed,
68                            working_directory, chromeos_root,
69                            cache_conditions, labels, benchmarks,
70                            experiment_file.Canonicalize(),
71                            email)
72
73    return experiment
74