Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: components/variations/variations_seed_simulator.cc

Issue 41623002: Simulate field trial assignments with new variations seed. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
5 #include "components/variations/variations_seed_simulator.h"
6
7 #include <map>
8
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/variations_associated_data.h"
13
14 namespace chrome_variations {
15
16 namespace {
17
18 // Fills in |current_state| with the current process' active field trials, as a
19 // map of trial names to group names.
20 void GetCurrentTrialState(std::map<std::string, std::string>* current_state) {
21 base::FieldTrial::ActiveGroups trial_groups;
22 base::FieldTrialList::GetActiveFieldTrialGroups(&trial_groups);
23 for (size_t i = 0; i < trial_groups.size(); ++i)
24 (*current_state)[trial_groups[i].trial_name] = trial_groups[i].group_name;
25 }
26
27 // Simulate group assignment for the specified study with PERMANENT consistency.
28 // Returns the experiment group that will be selected. Mirrors logic in
29 // VariationsSeedProcessor::CreateTrialFromStudy().
30 std::string SimulateGroupAssignment(
31 const base::FieldTrial::EntropyProvider& entropy_provider,
32 const ProcessedStudy& processed_study) {
33 const Study& study = *processed_study.study();
34 DCHECK_EQ(Study_Consistency_PERMANENT, study.consistency());
35
36 const double entropy_value =
37 entropy_provider.GetEntropyForTrial(study.name(),
38 study.randomization_seed());
39 scoped_refptr<base::FieldTrial> trial(
40 base::FieldTrial::CreateSimulatedFieldTrial(
41 study.name(), processed_study.total_probability(),
42 study.default_experiment_name(), entropy_value));
43
44 for (int i = 0; i < study.experiment_size(); ++i) {
45 const Study_Experiment& experiment = study.experiment(i);
46 // TODO(asvitkine): This needs to properly handle the case where a group was
47 // forced via forcing_flag in the current state, so that it is not treated
48 // as changed.
49 if (!experiment.has_forcing_flag() &&
50 experiment.name() != study.default_experiment_name()) {
51 trial->AppendGroup(experiment.name(), experiment.probability_weight());
52 }
53 }
54 if (processed_study.is_expired())
55 trial->Disable();
56 return trial->group_name();
57 }
58
59 // Finds an experiment in |study| with name |experiment_name| and returns it,
60 // or NULL if it does not exist.
61 const Study_Experiment* FindExperiment(const Study& study,
62 const std::string& experiment_name) {
63 for (int i = 0; i < study.experiment_size(); ++i) {
64 if (study.experiment(i).name() == experiment_name)
65 return &study.experiment(i);
66 }
67 return NULL;
68 }
69
70 // Checks whether experiment params set for |experiment| on |study| are exactly
71 // equal to the params registered for the corresponding field trial in the
72 // current process.
73 bool VariationParamsAreEqual(const Study& study,
74 const Study_Experiment& experiment) {
75 std::map<std::string, std::string> params;
76 GetVariationParams(study.name(), &params);
77
78 if (static_cast<int>(params.size()) != experiment.param_size())
79 return false;
80
81 for (int i = 0; i < experiment.param_size(); ++i) {
82 std::map<std::string, std::string>::const_iterator it =
83 params.find(experiment.param(i).name());
84 if (it == params.end() || it->second != experiment.param(i).value())
85 return false;
86 }
87
88 return true;
89 }
90
91 } // namespace
92
93 VariationsSeedSimulator::VariationsSeedSimulator(
94 const base::FieldTrial::EntropyProvider& entropy_provider)
95 : entropy_provider_(entropy_provider) {
96 }
97
98 VariationsSeedSimulator::~VariationsSeedSimulator() {
99 }
100
101 int VariationsSeedSimulator::ComputeDifferences(
102 const std::vector<ProcessedStudy>& processed_studies) {
103 std::map<std::string, std::string> current_state;
104 GetCurrentTrialState(&current_state);
105 int group_change_count = 0;
106
107 for (size_t i = 0; i < processed_studies.size(); ++i) {
108 const Study& study = *processed_studies[i].study();
109 std::map<std::string, std::string>::const_iterator it =
110 current_state.find(study.name());
111
112 // Skip studies that aren't activated in the current state.
113 // TODO(asvitkine): This should be handled more intelligently. There are
114 // several cases that fall into this category:
115 // 1) There's an existing field trial with this name but it is not active.
116 // 2) There's an existing expired field trial with this name, which is
117 // also not considered as active.
118 // 3) This is a new study config that previously didn't exist.
119 // The above cases should be differentiated and handled explicitly.
120 if (it == current_state.end())
121 continue;
122
123 // Study exists in the current state, check whether its group will change.
124 const std::string& selected_group = it->second;
125 if (study.consistency() == Study_Consistency_PERMANENT) {
126 if (PermanentStudyGroupChanged(processed_studies[i], selected_group))
127 ++group_change_count;
128 } else if (study.consistency() == Study_Consistency_SESSION) {
129 if (SessionStudyGroupChanged(processed_studies[i], selected_group))
130 ++group_change_count;
131 }
jwd 2014/01/07 18:31:37 This seems to implicitly do the correct thing when
Alexei Svitkine (slow) 2014/01/08 15:17:35 Done.
132 }
133
134 // TODO(asvitkine): Handle removed studies (i.e. studies that existed in the
135 // old seed, but were removed). This will require tracking the set of studies
136 // that were created from the original seed.
137
138 return group_change_count;
139 }
140
141 bool VariationsSeedSimulator::PermanentStudyGroupChanged(
142 const ProcessedStudy& processed_study,
143 const std::string& selected_group) {
144 const Study& study = *processed_study.study();
145 DCHECK_EQ(Study_Consistency_PERMANENT, study.consistency());
146
147 const std::string simulated_group = SimulateGroupAssignment(entropy_provider_,
148 processed_study);
149 // TODO(asvitkine): Sometimes group names are changed without changing any
150 // behavior (e.g. if the behavior is controlled entirely via params). Support
151 // a mechanism to bypass this check.
152 if (simulated_group != selected_group)
153 return true;
154
155 const Study_Experiment* experiment = FindExperiment(study, selected_group);
156 DCHECK(experiment);
157 return !VariationParamsAreEqual(study, *experiment);
158 }
159
160 bool VariationsSeedSimulator::SessionStudyGroupChanged(
161 const ProcessedStudy& processed_study,
162 const std::string& selected_group) {
163 const Study& study = *processed_study.study();
164 DCHECK_EQ(Study_Consistency_SESSION, study.consistency());
165
166 if (processed_study.is_expired() &&
167 selected_group != study.default_experiment_name()) {
168 // An expired study will result in the default group being selected - mark
169 // it as changed if the current group differs from the default.
170 return true;
171 }
172
173 const Study_Experiment* experiment = FindExperiment(study, selected_group);
174 if (!experiment)
175 return true;
176 if (experiment->probability_weight() == 0 &&
177 !experiment->has_forcing_flag()) {
178 return true;
179 }
180
181 // Current group exists in the study - check whether its params changed.
182 return !VariationParamsAreEqual(study, *experiment);
183 }
184
185 } // namespace chrome_variations
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698