Avoid using GL_EXT_disjoint_timer_query extension on Nexus 9 in WebView.
[chromium-blink-merge.git] / components / variations / variations_seed_simulator.cc
blob358516e994fdb361e431effa36aa3102901b74bc
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.
5 #include "components/variations/variations_seed_simulator.h"
7 #include <map>
9 #include "base/metrics/field_trial.h"
10 #include "components/variations/processed_study.h"
11 #include "components/variations/proto/study.pb.h"
12 #include "components/variations/study_filtering.h"
13 #include "components/variations/variations_associated_data.h"
15 namespace variations {
17 namespace {
19 // Fills in |current_state| with the current process' active field trials, as a
20 // map of trial names to group names.
21 void GetCurrentTrialState(std::map<std::string, std::string>* current_state) {
22 base::FieldTrial::ActiveGroups trial_groups;
23 base::FieldTrialList::GetActiveFieldTrialGroups(&trial_groups);
24 for (size_t i = 0; i < trial_groups.size(); ++i)
25 (*current_state)[trial_groups[i].trial_name] = trial_groups[i].group_name;
28 // Simulate group assignment for the specified study with PERMANENT consistency.
29 // Returns the experiment group that will be selected. Mirrors logic in
30 // VariationsSeedProcessor::CreateTrialFromStudy().
31 std::string SimulateGroupAssignment(
32 const base::FieldTrial::EntropyProvider& entropy_provider,
33 const ProcessedStudy& processed_study) {
34 const Study& study = *processed_study.study();
35 DCHECK_EQ(Study_Consistency_PERMANENT, study.consistency());
37 const double entropy_value =
38 entropy_provider.GetEntropyForTrial(study.name(),
39 study.randomization_seed());
40 scoped_refptr<base::FieldTrial> trial(
41 base::FieldTrial::CreateSimulatedFieldTrial(
42 study.name(), processed_study.total_probability(),
43 study.default_experiment_name(), entropy_value));
45 for (int i = 0; i < study.experiment_size(); ++i) {
46 const Study_Experiment& experiment = study.experiment(i);
47 // TODO(asvitkine): This needs to properly handle the case where a group was
48 // forced via forcing_flag in the current state, so that it is not treated
49 // as changed.
50 if (!experiment.has_forcing_flag() &&
51 experiment.name() != study.default_experiment_name()) {
52 trial->AppendGroup(experiment.name(), experiment.probability_weight());
55 if (processed_study.is_expired())
56 trial->Disable();
57 return trial->group_name();
60 // Finds an experiment in |study| with name |experiment_name| and returns it,
61 // or NULL if it does not exist.
62 const Study_Experiment* FindExperiment(const Study& study,
63 const std::string& experiment_name) {
64 for (int i = 0; i < study.experiment_size(); ++i) {
65 if (study.experiment(i).name() == experiment_name)
66 return &study.experiment(i);
68 return NULL;
71 // Checks whether experiment params set for |experiment| on |study| are exactly
72 // equal to the params registered for the corresponding field trial in the
73 // current process.
74 bool VariationParamsAreEqual(const Study& study,
75 const Study_Experiment& experiment) {
76 std::map<std::string, std::string> params;
77 GetVariationParams(study.name(), &params);
79 if (static_cast<int>(params.size()) != experiment.param_size())
80 return false;
82 for (int i = 0; i < experiment.param_size(); ++i) {
83 std::map<std::string, std::string>::const_iterator it =
84 params.find(experiment.param(i).name());
85 if (it == params.end() || it->second != experiment.param(i).value())
86 return false;
89 return true;
92 } // namespace
94 VariationsSeedSimulator::Result::Result()
95 : normal_group_change_count(0),
96 kill_best_effort_group_change_count(0),
97 kill_critical_group_change_count(0) {
100 VariationsSeedSimulator::Result::~Result() {
103 VariationsSeedSimulator::VariationsSeedSimulator(
104 const base::FieldTrial::EntropyProvider& entropy_provider)
105 : entropy_provider_(entropy_provider) {
108 VariationsSeedSimulator::~VariationsSeedSimulator() {
111 VariationsSeedSimulator::Result VariationsSeedSimulator::SimulateSeedStudies(
112 const VariationsSeed& seed,
113 const std::string& locale,
114 const base::Time& reference_date,
115 const base::Version& version,
116 Study_Channel channel,
117 Study_FormFactor form_factor,
118 const std::string& hardware_class,
119 const std::string& permanent_consistency_country) {
120 std::vector<ProcessedStudy> filtered_studies;
121 FilterAndValidateStudies(seed, locale, reference_date, version, channel,
122 form_factor, hardware_class,
123 permanent_consistency_country, &filtered_studies);
125 return ComputeDifferences(filtered_studies);
128 VariationsSeedSimulator::Result VariationsSeedSimulator::ComputeDifferences(
129 const std::vector<ProcessedStudy>& processed_studies) {
130 std::map<std::string, std::string> current_state;
131 GetCurrentTrialState(&current_state);
133 Result result;
134 for (size_t i = 0; i < processed_studies.size(); ++i) {
135 const Study& study = *processed_studies[i].study();
136 std::map<std::string, std::string>::const_iterator it =
137 current_state.find(study.name());
139 // Skip studies that aren't activated in the current state.
140 // TODO(asvitkine): This should be handled more intelligently. There are
141 // several cases that fall into this category:
142 // 1) There's an existing field trial with this name but it is not active.
143 // 2) There's an existing expired field trial with this name, which is
144 // also not considered as active.
145 // 3) This is a new study config that previously didn't exist.
146 // The above cases should be differentiated and handled explicitly.
147 if (it == current_state.end())
148 continue;
150 // Study exists in the current state, check whether its group will change.
151 // Note: The logic below does the right thing if study consistency changes,
152 // as it doesn't rely on the previous study consistency.
153 const std::string& selected_group = it->second;
154 ChangeType change_type = NO_CHANGE;
155 if (study.consistency() == Study_Consistency_PERMANENT) {
156 change_type = PermanentStudyGroupChanged(processed_studies[i],
157 selected_group);
158 } else if (study.consistency() == Study_Consistency_SESSION) {
159 change_type = SessionStudyGroupChanged(processed_studies[i],
160 selected_group);
163 switch (change_type) {
164 case NO_CHANGE:
165 break;
166 case CHANGED:
167 ++result.normal_group_change_count;
168 break;
169 case CHANGED_KILL_BEST_EFFORT:
170 ++result.kill_best_effort_group_change_count;
171 break;
172 case CHANGED_KILL_CRITICAL:
173 ++result.kill_critical_group_change_count;
174 break;
178 // TODO(asvitkine): Handle removed studies (i.e. studies that existed in the
179 // old seed, but were removed). This will require tracking the set of studies
180 // that were created from the original seed.
182 return result;
185 VariationsSeedSimulator::ChangeType
186 VariationsSeedSimulator::ConvertExperimentTypeToChangeType(
187 Study_Experiment_Type type) {
188 switch (type) {
189 case Study_Experiment_Type_NORMAL:
190 return CHANGED;
191 case Study_Experiment_Type_IGNORE_CHANGE:
192 return NO_CHANGE;
193 case Study_Experiment_Type_KILL_BEST_EFFORT:
194 return CHANGED_KILL_BEST_EFFORT;
195 case Study_Experiment_Type_KILL_CRITICAL:
196 return CHANGED_KILL_CRITICAL;
198 return CHANGED;
201 VariationsSeedSimulator::ChangeType
202 VariationsSeedSimulator::PermanentStudyGroupChanged(
203 const ProcessedStudy& processed_study,
204 const std::string& selected_group) {
205 const Study& study = *processed_study.study();
206 DCHECK_EQ(Study_Consistency_PERMANENT, study.consistency());
208 const std::string simulated_group = SimulateGroupAssignment(entropy_provider_,
209 processed_study);
210 const Study_Experiment* experiment = FindExperiment(study, selected_group);
211 if (simulated_group != selected_group) {
212 if (experiment)
213 return ConvertExperimentTypeToChangeType(experiment->type());
214 return CHANGED;
217 // Current group exists in the study - check whether its params changed.
218 DCHECK(experiment);
219 if (!VariationParamsAreEqual(study, *experiment))
220 return ConvertExperimentTypeToChangeType(experiment->type());
221 return NO_CHANGE;
224 VariationsSeedSimulator::ChangeType
225 VariationsSeedSimulator::SessionStudyGroupChanged(
226 const ProcessedStudy& processed_study,
227 const std::string& selected_group) {
228 const Study& study = *processed_study.study();
229 DCHECK_EQ(Study_Consistency_SESSION, study.consistency());
231 const Study_Experiment* experiment = FindExperiment(study, selected_group);
232 if (processed_study.is_expired() &&
233 selected_group != study.default_experiment_name()) {
234 // An expired study will result in the default group being selected - mark
235 // it as changed if the current group differs from the default.
236 if (experiment)
237 return ConvertExperimentTypeToChangeType(experiment->type());
238 return CHANGED;
241 if (!experiment)
242 return CHANGED;
243 if (experiment->probability_weight() == 0 &&
244 !experiment->has_forcing_flag()) {
245 return ConvertExperimentTypeToChangeType(experiment->type());
248 // Current group exists in the study - check whether its params changed.
249 if (!VariationParamsAreEqual(study, *experiment))
250 return ConvertExperimentTypeToChangeType(experiment->type());
251 return NO_CHANGE;
254 } // namespace variations