1// Copyright 2014 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
5#ifndef COMPONENTS_VARIATIONS_VARIATIONS_SEED_SIMULATOR_H_
6#define COMPONENTS_VARIATIONS_VARIATIONS_SEED_SIMULATOR_H_
7
8#include <string>
9#include <vector>
10
11#include "base/compiler_specific.h"
12#include "base/gtest_prod_util.h"
13#include "base/metrics/field_trial.h"
14#include "base/version.h"
15#include "components/variations/proto/study.pb.h"
16#include "components/variations/proto/variations_seed.pb.h"
17
18namespace variations {
19
20class ProcessedStudy;
21class VariationsSeed;
22
23// VariationsSeedSimulator simulates the result of creating a set of studies
24// and detecting which studies would result in group changes.
25class VariationsSeedSimulator {
26 public:
27  // The result of variations seed simulation, counting the number of experiment
28  // group changes of each type that are expected to occur on a restart with the
29  // seed.
30  struct Result {
31    // The number of expected group changes that do not fall into any special
32    // category. This is a lower bound due to session randomized studies.
33    int normal_group_change_count;
34
35    // The number of expected group changes that fall in the category of killed
36    // experiments that should trigger the "best effort" restart mechanism.
37    int kill_best_effort_group_change_count;
38
39    // The number of expected group changes that fall in the category of killed
40    // experiments that should trigger the "critical" restart mechanism.
41    int kill_critical_group_change_count;
42
43    Result();
44    ~Result();
45  };
46
47  // Creates the simulator with the given entropy |provider|.
48  explicit VariationsSeedSimulator(
49      const base::FieldTrial::EntropyProvider& provider);
50  virtual ~VariationsSeedSimulator();
51
52  // Computes differences between the current process' field trial state and
53  // the result of evaluating |seed| with the given parameters. Returns the
54  // results of the simulation as a set of expected group change counts of each
55  // type.
56  Result SimulateSeedStudies(const VariationsSeed& seed,
57                             const std::string& locale,
58                             const base::Time& reference_date,
59                             const base::Version& version,
60                             Study_Channel channel,
61                             Study_FormFactor form_factor,
62                             const std::string& hardware_class);
63
64 private:
65  friend class VariationsSeedSimulatorTest;
66
67  enum ChangeType {
68    NO_CHANGE,
69    CHANGED,
70    CHANGED_KILL_BEST_EFFORT,
71    CHANGED_KILL_CRITICAL,
72  };
73
74  // Computes differences between the current process' field trial state and
75  // the result of evaluating the |processed_studies| list. It is expected that
76  // |processed_studies| have already been filtered and only contain studies
77  // that apply to the configuration being simulated. Returns the results of the
78  // simulation as a set of expected group change counts of each type.
79  Result ComputeDifferences(
80      const std::vector<ProcessedStudy>& processed_studies);
81
82  // Maps proto enum |type| to a ChangeType.
83  ChangeType ConvertExperimentTypeToChangeType(Study_Experiment_Type type);
84
85  // For the given |processed_study| with PERMANENT consistency, simulates group
86  // assignment and returns a corresponding ChangeType if the result differs
87  // from that study's group in the current process.
88  ChangeType PermanentStudyGroupChanged(const ProcessedStudy& processed_study,
89                                        const std::string& selected_group);
90
91  // For the given |processed_study| with SESSION consistency, determines if
92  // there are enough changes in the study config that restarting will result
93  // in a guaranteed different group assignment (or different params) and
94  // returns the corresponding ChangeType.
95  ChangeType SessionStudyGroupChanged(const ProcessedStudy& filtered_study,
96                                      const std::string& selected_group);
97
98  const base::FieldTrial::EntropyProvider& entropy_provider_;
99
100  DISALLOW_COPY_AND_ASSIGN(VariationsSeedSimulator);
101};
102
103}  // namespace variations
104
105#endif  // COMPONENTS_VARIATIONS_VARIATIONS_SEED_SIMULATOR_H_
106