OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // FieldTrial is a class for handling details of statistical experiments | 5 // FieldTrial is a class for handling details of statistical experiments |
6 // performed by actual users in the field (i.e., in a shipped or beta product). | 6 // performed by actual users in the field (i.e., in a shipped or beta product). |
7 // All code is called exclusively on the UI thread currently. | 7 // All code is called exclusively on the UI thread currently. |
8 // | 8 // |
9 // The simplest example is an experiment to see whether one of two options | 9 // The simplest example is an experiment to see whether one of two options |
10 // produces "better" results across our user population. In that scenario, UMA | 10 // produces "better" results across our user population. In that scenario, UMA |
11 // data is uploaded to aggregate the test results, and this FieldTrial class | 11 // data is uploaded to aggregate the test results, and this FieldTrial class |
12 // manages the state of each such experiment (state == which option was | 12 // manages the state of each such experiment (state == which option was |
13 // pseudo-randomly selected). | 13 // pseudo-randomly selected). |
14 // | 14 // |
15 // States are typically generated randomly, either based on a one time | 15 // States are typically generated randomly, either based on a one time |
16 // randomization (generated randomly once, and then persistently reused in the | 16 // randomization (which will yield the same results, in terms of selecting |
17 // client during each future run of the program), or by a startup randomization | 17 // the client for a field trial or not, for every run of the program on a |
18 // (generated each time the application starts up, but held constant during the | 18 // given machine), or by a startup randomization (generated each time the |
19 // duration of the process), or by continuous randomization across a run (where | 19 // application starts up, but held constant during the duration of the |
20 // the state can be recalculated again and again, many times during a process). | 20 // process), or by continuous randomization across a run (where the state |
21 // Only startup randomization is implemented thus far. | 21 // can be recalculated again and again, many times during a process). |
22 // Continuous randomization is not yet implemented. | |
22 | 23 |
23 //------------------------------------------------------------------------------ | 24 //------------------------------------------------------------------------------ |
24 // Example: Suppose we have an experiment involving memory, such as determining | 25 // Example: Suppose we have an experiment involving memory, such as determining |
25 // the impact of some pruning algorithm. | 26 // the impact of some pruning algorithm. |
26 // We assume that we already have a histogram of memory usage, such as: | 27 // We assume that we already have a histogram of memory usage, such as: |
27 | 28 |
28 // HISTOGRAM_COUNTS("Memory.RendererTotal", count); | 29 // HISTOGRAM_COUNTS("Memory.RendererTotal", count); |
29 | 30 |
30 // Somewhere in main thread initialization code, we'd probably define an | 31 // Somewhere in main thread initialization code, we'd probably define an |
31 // instance of a FieldTrial, with code such as: | 32 // instance of a FieldTrial, with code such as: |
(...skipping 13 matching lines...) Expand all Loading... | |
45 // if (trial->group() == kHighMemGroup) | 46 // if (trial->group() == kHighMemGroup) |
46 // SetPruningAlgorithm(kType1); // Sample setting of browser state. | 47 // SetPruningAlgorithm(kType1); // Sample setting of browser state. |
47 // else if (trial->group() == kLowMemGroup) | 48 // else if (trial->group() == kLowMemGroup) |
48 // SetPruningAlgorithm(kType2); // Sample alternate setting. | 49 // SetPruningAlgorithm(kType2); // Sample alternate setting. |
49 | 50 |
50 // We then, in addition to our original histogram, output histograms which have | 51 // We then, in addition to our original histogram, output histograms which have |
51 // slightly different names depending on what group the trial instance happened | 52 // slightly different names depending on what group the trial instance happened |
52 // to randomly be assigned: | 53 // to randomly be assigned: |
53 | 54 |
54 // HISTOGRAM_COUNTS("Memory.RendererTotal", count); // The original histogram. | 55 // HISTOGRAM_COUNTS("Memory.RendererTotal", count); // The original histogram. |
55 // static bool use_memoryexperiment_histogram( | 56 // if (FieldTrialList::Find("Memory.RendererTotal")) { |
56 // base::FieldTrialList::Find("MemoryExperiment") && | |
57 // !base::FieldTrialList::Find("MemoryExperiment")->group_name().empty()); | |
jar (doing other things)
2011/04/21 22:10:02
Removal of this code is a VERY good improvement.
Jói
2011/04/28 01:03:50
I've updated all the call-sites now. I introduced
Jói
2011/05/02 18:55:03
FYI, there were some merge changes to resolve, I u
| |
58 // if (use_memoryexperiment_histogram) { | |
59 // HISTOGRAM_COUNTS(FieldTrial::MakeName("Memory.RendererTotal", | 57 // HISTOGRAM_COUNTS(FieldTrial::MakeName("Memory.RendererTotal", |
60 // "MemoryExperiment"), count); | 58 // "MemoryExperiment"), count); |
61 // } | 59 // } |
62 | 60 |
63 // The above code will create four distinct histograms, with each run of the | 61 // The above code will create four distinct histograms, with each run of the |
64 // application being assigned to of of the three groups, and for each group, the | 62 // application being assigned to of of the three groups, and for each group, the |
65 // correspondingly named histogram will be populated: | 63 // correspondingly named histogram will be populated: |
66 | 64 |
67 // Memory.RendererTotal // 100% of users still fill this histogram. | 65 // Memory.RendererTotal // 100% of users still fill this histogram. |
68 // Memory.RendererTotal_HighMem // 2% of users will fill this histogram. | 66 // Memory.RendererTotal_HighMem // 2% of users will fill this histogram. |
(...skipping 26 matching lines...) Expand all Loading... | |
95 // A return value to indicate that a given instance has not yet had a group | 93 // A return value to indicate that a given instance has not yet had a group |
96 // assignment (and hence is not yet participating in the trial). | 94 // assignment (and hence is not yet participating in the trial). |
97 static const int kNotFinalized; | 95 static const int kNotFinalized; |
98 | 96 |
99 // This is the group number of the 'default' group. This provides an easy way | 97 // This is the group number of the 'default' group. This provides an easy way |
100 // to assign all the remaining probability to a group ('default'). | 98 // to assign all the remaining probability to a group ('default'). |
101 static const int kDefaultGroupNumber; | 99 static const int kDefaultGroupNumber; |
102 | 100 |
103 // The name is used to register the instance with the FieldTrialList class, | 101 // The name is used to register the instance with the FieldTrialList class, |
104 // and can be used to find the trial (only one trial can be present for each | 102 // and can be used to find the trial (only one trial can be present for each |
105 // name). | 103 // name). |name| and |default_group_name| may not be empty. |
104 // | |
106 // Group probabilities that are later supplied must sum to less than or equal | 105 // Group probabilities that are later supplied must sum to less than or equal |
107 // to the total_probability. Arguments year, month and day_of_month specify | 106 // to the total_probability. Arguments year, month and day_of_month specify |
108 // the expiration time. If the build time is after the expiration time then | 107 // the expiration time. If the build time is after the expiration time then |
109 // the field trial reverts to the 'default' group. | 108 // the field trial reverts to the 'default' group. |
109 // | |
110 // Using this constructor creates a startup-randomized FieldTrial. If you | |
111 // want a one-time randomized trial, call UseOneTimeRandomization() right | |
112 // after construction. | |
110 FieldTrial(const std::string& name, Probability total_probability, | 113 FieldTrial(const std::string& name, Probability total_probability, |
111 const std::string& default_group_name, const int year, | 114 const std::string& default_group_name, const int year, |
112 const int month, const int day_of_month); | 115 const int month, const int day_of_month); |
113 | 116 |
117 // Changes the field trial to use one-time randomization, i.e. produce the | |
118 // same result for the current trial on every run of this client. Must be | |
119 // called right after construction. | |
120 // | |
121 // Before using this method, |FieldTrialList::EnableOneTimeRandomization()| | |
122 // must be called exactly once. | |
123 void UseOneTimeRandomization(); | |
124 | |
125 // Disables this trial, meaning it always determines the default group | |
126 // has been selected. May be called immediately after construction, or | |
127 // at any time after initialization (should not be interleaved with | |
128 // AppendGroup calls). Once disabled, there is no way to re-enable a | |
129 // trial. | |
130 void Disable(); | |
131 | |
114 // Establish the name and probability of the next group in this trial. | 132 // Establish the name and probability of the next group in this trial. |
115 // Sometimes, based on construction randomization, this call may cause the | 133 // Sometimes, based on construction randomization, this call may cause the |
116 // provided group to be *THE* group selected for use in this instance. | 134 // provided group to be *THE* group selected for use in this instance. |
117 // The return value is the group number of the new group. | 135 // The return value is the group number of the new group. |
118 int AppendGroup(const std::string& name, Probability group_probability); | 136 int AppendGroup(const std::string& name, Probability group_probability); |
119 | 137 |
120 // Return the name of the FieldTrial (excluding the group name). | 138 // Return the name of the FieldTrial (excluding the group name). |
121 std::string name() const { return name_; } | 139 std::string name() const { return name_; } |
122 | 140 |
123 // Return the randomly selected group number that was assigned. | 141 // Return the randomly selected group number that was assigned. |
124 // Return kDefaultGroupNumber if the instance is in the 'default' group. | 142 // Return kDefaultGroupNumber if the instance is in the 'default' group. |
125 // Note that this will force an instance to participate, and make it illegal | 143 // Note that this will force an instance to participate, and make it illegal |
126 // to attempt to probabalistically add any other groups to the trial. | 144 // to attempt to probabilistically add any other groups to the trial. |
127 int group(); | 145 int group(); |
128 | 146 |
129 // If the field trial is not in an experiment, this returns the empty string. | 147 // If the group's name is empty, a string version containing the group |
130 // if the group's name is empty, a name of "_" concatenated with the group | |
131 // number is used as the group name. | 148 // number is used as the group name. |
132 std::string group_name(); | 149 std::string group_name(); |
133 | 150 |
134 // Return the default group name of the FieldTrial. | 151 // Return the default group name of the FieldTrial. |
135 std::string default_group_name() const { return default_group_name_; } | 152 std::string default_group_name() const { return default_group_name_; } |
136 | 153 |
137 // Helper function for the most common use: as an argument to specifiy the | 154 // Helper function for the most common use: as an argument to specify the |
138 // name of a HISTOGRAM. Use the original histogram name as the name_prefix. | 155 // name of a HISTOGRAM. Use the original histogram name as the name_prefix. |
139 static std::string MakeName(const std::string& name_prefix, | 156 static std::string MakeName(const std::string& name_prefix, |
140 const std::string& trial_name); | 157 const std::string& trial_name); |
141 | 158 |
142 // Enable benchmarking sets field trials to a common setting. | 159 // Enable benchmarking sets field trials to a common setting. |
143 static void EnableBenchmarking(); | 160 static void EnableBenchmarking(); |
144 | 161 |
145 private: | 162 private: |
146 // Allow tests to access our innards for testing purposes. | 163 // Allow tests to access our innards for testing purposes. |
147 FRIEND_TEST(FieldTrialTest, Registration); | 164 FRIEND_TEST(FieldTrialTest, Registration); |
148 FRIEND_TEST(FieldTrialTest, AbsoluteProbabilities); | 165 FRIEND_TEST(FieldTrialTest, AbsoluteProbabilities); |
149 FRIEND_TEST(FieldTrialTest, RemainingProbability); | 166 FRIEND_TEST(FieldTrialTest, RemainingProbability); |
150 FRIEND_TEST(FieldTrialTest, FiftyFiftyProbability); | 167 FRIEND_TEST(FieldTrialTest, FiftyFiftyProbability); |
151 FRIEND_TEST(FieldTrialTest, MiddleProbabilities); | 168 FRIEND_TEST(FieldTrialTest, MiddleProbabilities); |
152 FRIEND_TEST(FieldTrialTest, OneWinner); | 169 FRIEND_TEST(FieldTrialTest, OneWinner); |
153 FRIEND_TEST(FieldTrialTest, DisableProbability); | 170 FRIEND_TEST(FieldTrialTest, DisableProbability); |
154 FRIEND_TEST(FieldTrialTest, Save); | 171 FRIEND_TEST(FieldTrialTest, Save); |
155 FRIEND_TEST(FieldTrialTest, DuplicateRestore); | 172 FRIEND_TEST(FieldTrialTest, DuplicateRestore); |
156 FRIEND_TEST(FieldTrialTest, MakeName); | 173 FRIEND_TEST(FieldTrialTest, MakeName); |
174 FRIEND_TEST(FieldTrialTest, HashClientId); | |
175 FRIEND_TEST(FieldTrialTest, HashClientIdIsUniform); | |
176 FRIEND_TEST(FieldTrialTest, UseOneTimeRandomization); | |
157 | 177 |
158 friend class base::FieldTrialList; | 178 friend class base::FieldTrialList; |
159 | 179 |
160 friend class RefCounted<FieldTrial>; | 180 friend class RefCounted<FieldTrial>; |
161 | 181 |
162 virtual ~FieldTrial(); | 182 virtual ~FieldTrial(); |
163 | 183 |
164 // Returns the group_name. A winner need not have been chosen. | 184 // Returns the group_name. A winner need not have been chosen. |
165 std::string group_name_internal() const { return group_name_; } | 185 std::string group_name_internal() const { return group_name_; } |
166 | 186 |
167 // Get build time. | 187 // Get build time. |
168 static Time GetBuildTime(); | 188 static Time GetBuildTime(); |
169 | 189 |
190 // Calculates a uniformly-distributed double between [0.0, 1.0) given | |
191 // a |client_id| and a |trial_name| (the latter is used as salt to avoid | |
192 // separate one-time randomized trials from all having the same results). | |
193 static double HashClientId(const std::string& client_id, | |
194 const std::string& trial_name); | |
jar (doing other things)
2011/04/21 22:10:02
I would have expected the client_id to be a consta
Jói
2011/04/28 01:03:50
2011/04/21 22:10:02, jar wrote:
| |
195 | |
170 // The name of the field trial, as can be found via the FieldTrialList. | 196 // The name of the field trial, as can be found via the FieldTrialList. |
171 // This is empty of the trial is not in the experiment. | 197 // This is empty if the trial is not in the experiment. |
jar (doing other things)
2011/04/21 22:10:02
hmmm.... is this ever empty now?
Jói
2011/04/28 01:03:50
You're right, I updated the documentation and adde
| |
172 const std::string name_; | 198 const std::string name_; |
173 | 199 |
174 // The maximum sum of all probabilities supplied, which corresponds to 100%. | 200 // The maximum sum of all probabilities supplied, which corresponds to 100%. |
175 // This is the scaling factor used to adjust supplied probabilities. | 201 // This is the scaling factor used to adjust supplied probabilities. |
176 const Probability divisor_; | 202 const Probability divisor_; |
177 | 203 |
178 // The name of the default group. | 204 // The name of the default group. |
179 const std::string default_group_name_; | 205 const std::string default_group_name_; |
180 | 206 |
181 // The randomly selected probability that is used to select a group (or have | 207 // The randomly selected probability that is used to select a group (or have |
182 // the instance not participate). It is the product of divisor_ and a random | 208 // the instance not participate). It is the product of divisor_ and a random |
183 // number between [0, 1). | 209 // number between [0, 1). |
184 const Probability random_; | 210 Probability random_; |
185 | 211 |
186 // Sum of the probabilities of all appended groups. | 212 // Sum of the probabilities of all appended groups. |
187 Probability accumulated_group_probability_; | 213 Probability accumulated_group_probability_; |
188 | 214 |
189 int next_group_number_; | 215 int next_group_number_; |
190 | 216 |
191 // The pseudo-randomly assigned group number. | 217 // The pseudo-randomly assigned group number. |
192 // This is kNotFinalized if no group has been assigned. | 218 // This is kNotFinalized if no group has been assigned. |
193 int group_; | 219 int group_; |
194 | 220 |
195 // A textual name for the randomly selected group. If this Trial is not a | 221 // A textual name for the randomly selected group. Valid after |group()| |
196 // member of an group, this string is empty. | 222 // has been called. |
197 std::string group_name_; | 223 std::string group_name_; |
198 | 224 |
199 // When disable_field_trial_ is true, field trial reverts to the 'default' | 225 // When enable_field_trial_ is false, field trial reverts to the 'default' |
200 // group. | 226 // group. |
201 bool disable_field_trial_; | 227 bool enable_field_trial_; |
jar (doing other things)
2011/04/21 22:10:02
Thanks for making this change!
Jói
2011/04/28 01:03:50
No prob.
| |
202 | 228 |
203 // When benchmarking is enabled, field trials all revert to the 'default' | 229 // When benchmarking is enabled, field trials all revert to the 'default' |
204 // group. | 230 // group. |
205 static bool enable_benchmarking_; | 231 static bool enable_benchmarking_; |
206 | 232 |
207 DISALLOW_COPY_AND_ASSIGN(FieldTrial); | 233 DISALLOW_COPY_AND_ASSIGN(FieldTrial); |
208 }; | 234 }; |
209 | 235 |
210 //------------------------------------------------------------------------------ | 236 //------------------------------------------------------------------------------ |
211 // Class with a list of all active field trials. A trial is active if it has | 237 // Class with a list of all active field trials. A trial is active if it has |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
258 static TimeTicks application_start_time() { | 284 static TimeTicks application_start_time() { |
259 if (global_) | 285 if (global_) |
260 return global_->application_start_time_; | 286 return global_->application_start_time_; |
261 // For testing purposes only, or when we don't yet have a start time. | 287 // For testing purposes only, or when we don't yet have a start time. |
262 return TimeTicks::Now(); | 288 return TimeTicks::Now(); |
263 } | 289 } |
264 | 290 |
265 // Return the number of active field trials. | 291 // Return the number of active field trials. |
266 static size_t GetFieldTrialCount(); | 292 static size_t GetFieldTrialCount(); |
267 | 293 |
294 // Sets an opaque, diverse ID for this client that does not change | |
295 // between sessions. This must be called exactly once before any call to | |
296 // |FieldTrial::UseOneTimeRandomization()| and does not need to be called | |
297 // unless such a call is made. | |
298 static void EnableOneTimeRandomization(const std::string& client_id); | |
jar (doing other things)
2011/04/21 22:10:02
I'd be tempted to make this part of required initi
Jói
2011/04/28 01:03:50
I disagree, but am willing to be overridden. Let
jar (doing other things)
2011/04/29 00:57:11
I'm not sold on your arguments. To be specific, f
Jói
2011/05/02 18:55:03
I've switched to having client_id as an argument t
| |
299 | |
300 // Returns true if you can call |FieldTrial::UseOneTimeRandomization()| | |
301 // without error, i.e. if |EnableOneTimeRandomization()| has been called. | |
302 static bool IsOneTimeRandomizationEnabled(); | |
303 | |
304 // Returns an opaque, diverse ID for this client that does not change | |
305 // between sessions. | |
306 // | |
307 // Returns the empty string if |EnableOneTimeRandomization()| has not | |
308 // been called. | |
309 static const std::string& client_id(); | |
310 | |
268 private: | 311 private: |
269 // A map from FieldTrial names to the actual instances. | 312 // A map from FieldTrial names to the actual instances. |
270 typedef std::map<std::string, FieldTrial*> RegistrationList; | 313 typedef std::map<std::string, FieldTrial*> RegistrationList; |
271 | 314 |
272 // Helper function should be called only while holding lock_. | 315 // Helper function should be called only while holding lock_. |
273 FieldTrial* PreLockedFind(const std::string& name); | 316 FieldTrial* PreLockedFind(const std::string& name); |
274 | 317 |
275 static FieldTrialList* global_; // The singleton of this class. | 318 static FieldTrialList* global_; // The singleton of this class. |
276 | 319 |
277 // This will tell us if there is an attempt to register a field trial without | 320 // This will tell us if there is an attempt to register a field trial without |
278 // creating the FieldTrialList. This is not an error, unless a FieldTrialList | 321 // creating the FieldTrialList. This is not an error, unless a FieldTrialList |
279 // is created after that. | 322 // is created after that. |
280 static bool register_without_global_; | 323 static bool register_without_global_; |
281 | 324 |
282 // A helper value made availabel to users, that shows when the FieldTrialList | 325 // A helper value made available to users, that shows when the FieldTrialList |
283 // was initialized. Note that this is a singleton instance, and hence is a | 326 // was initialized. Note that this is a singleton instance, and hence is a |
284 // good approximation to the start of the process. | 327 // good approximation to the start of the process. |
285 TimeTicks application_start_time_; | 328 TimeTicks application_start_time_; |
286 | 329 |
287 // Lock for access to registered_. | 330 // Lock for access to registered_. |
288 base::Lock lock_; | 331 base::Lock lock_; |
289 RegistrationList registered_; | 332 RegistrationList registered_; |
290 | 333 |
334 // An opaque, diverse ID for this client that does not change | |
335 // between sessions, or the empty string if not initialized. | |
336 std::string client_id_; | |
337 | |
291 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); | 338 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); |
292 }; | 339 }; |
293 | 340 |
294 } // namespace base | 341 } // namespace base |
295 | 342 |
296 #endif // BASE_METRICS_FIELD_TRIAL_H_ | 343 #endif // BASE_METRICS_FIELD_TRIAL_H_ |
297 | 344 |
OLD | NEW |