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 // static const bool memory_renderer_total_trial_exists = |
56 // base::FieldTrialList::Find("MemoryExperiment") && | 57 // FieldTrialList::TrialExists("Memory.RendererTotal"); |
57 // !base::FieldTrialList::Find("MemoryExperiment")->group_name().empty()); | 58 // if (memory_renderer_total_trial_exists) { |
58 // if (use_memoryexperiment_histogram) { | |
59 // HISTOGRAM_COUNTS(FieldTrial::MakeName("Memory.RendererTotal", | 59 // HISTOGRAM_COUNTS(FieldTrial::MakeName("Memory.RendererTotal", |
60 // "MemoryExperiment"), count); | 60 // "MemoryExperiment"), count); |
61 // } | 61 // } |
62 | 62 |
63 // The above code will create four distinct histograms, with each run of the | 63 // 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 | 64 // application being assigned to of of the three groups, and for each group, the |
65 // correspondingly named histogram will be populated: | 65 // correspondingly named histogram will be populated: |
66 | 66 |
67 // Memory.RendererTotal // 100% of users still fill this histogram. | 67 // Memory.RendererTotal // 100% of users still fill this histogram. |
68 // Memory.RendererTotal_HighMem // 2% of users will fill this histogram. | 68 // 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 | 95 // 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). | 96 // assignment (and hence is not yet participating in the trial). |
97 static const int kNotFinalized; | 97 static const int kNotFinalized; |
98 | 98 |
99 // This is the group number of the 'default' group. This provides an easy way | 99 // 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'). | 100 // to assign all the remaining probability to a group ('default'). |
101 static const int kDefaultGroupNumber; | 101 static const int kDefaultGroupNumber; |
102 | 102 |
103 // The name is used to register the instance with the FieldTrialList class, | 103 // 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 | 104 // and can be used to find the trial (only one trial can be present for each |
105 // name). | 105 // name). |name| and |default_group_name| may not be empty. |
| 106 // |
106 // Group probabilities that are later supplied must sum to less than or equal | 107 // 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 | 108 // 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 | 109 // the expiration time. If the build time is after the expiration time then |
109 // the field trial reverts to the 'default' group. | 110 // the field trial reverts to the 'default' group. |
| 111 // |
| 112 // Using this constructor creates a startup-randomized FieldTrial. If you |
| 113 // want a one-time randomized trial, call UseOneTimeRandomization() right |
| 114 // after construction. |
110 FieldTrial(const std::string& name, Probability total_probability, | 115 FieldTrial(const std::string& name, Probability total_probability, |
111 const std::string& default_group_name, const int year, | 116 const std::string& default_group_name, const int year, |
112 const int month, const int day_of_month); | 117 const int month, const int day_of_month); |
113 | 118 |
| 119 // Changes the field trial to use one-time randomization, i.e. produce the |
| 120 // same result for the current trial on every run of this client. Must be |
| 121 // called right after construction. |
| 122 // |
| 123 // Before using this method, |FieldTrialList::EnableOneTimeRandomization()| |
| 124 // must be called exactly once. |
| 125 void UseOneTimeRandomization(); |
| 126 |
| 127 // Disables this trial, meaning it always determines the default group |
| 128 // has been selected. May be called immediately after construction, or |
| 129 // at any time after initialization (should not be interleaved with |
| 130 // AppendGroup calls). Once disabled, there is no way to re-enable a |
| 131 // trial. |
| 132 void Disable(); |
| 133 |
114 // Establish the name and probability of the next group in this trial. | 134 // Establish the name and probability of the next group in this trial. |
115 // Sometimes, based on construction randomization, this call may cause the | 135 // Sometimes, based on construction randomization, this call may cause the |
116 // provided group to be *THE* group selected for use in this instance. | 136 // provided group to be *THE* group selected for use in this instance. |
117 // The return value is the group number of the new group. | 137 // The return value is the group number of the new group. |
118 int AppendGroup(const std::string& name, Probability group_probability); | 138 int AppendGroup(const std::string& name, Probability group_probability); |
119 | 139 |
120 // Return the name of the FieldTrial (excluding the group name). | 140 // Return the name of the FieldTrial (excluding the group name). |
121 std::string name() const { return name_; } | 141 std::string name() const { return name_; } |
122 | 142 |
123 // Return the randomly selected group number that was assigned. | 143 // Return the randomly selected group number that was assigned. |
124 // Return kDefaultGroupNumber if the instance is in the 'default' group. | 144 // Return kDefaultGroupNumber if the instance is in the 'default' group. |
125 // Note that this will force an instance to participate, and make it illegal | 145 // 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. | 146 // to attempt to probabilistically add any other groups to the trial. |
127 int group(); | 147 int group(); |
128 | 148 |
129 // If the field trial is not in an experiment, this returns the empty string. | 149 // 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. | 150 // number is used as the group name. |
132 std::string group_name(); | 151 std::string group_name(); |
133 | 152 |
134 // Return the default group name of the FieldTrial. | 153 // Return the default group name of the FieldTrial. |
135 std::string default_group_name() const { return default_group_name_; } | 154 std::string default_group_name() const { return default_group_name_; } |
136 | 155 |
137 // Helper function for the most common use: as an argument to specifiy the | 156 // 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. | 157 // name of a HISTOGRAM. Use the original histogram name as the name_prefix. |
139 static std::string MakeName(const std::string& name_prefix, | 158 static std::string MakeName(const std::string& name_prefix, |
140 const std::string& trial_name); | 159 const std::string& trial_name); |
141 | 160 |
142 // Enable benchmarking sets field trials to a common setting. | 161 // Enable benchmarking sets field trials to a common setting. |
143 static void EnableBenchmarking(); | 162 static void EnableBenchmarking(); |
144 | 163 |
145 private: | 164 private: |
146 // Allow tests to access our innards for testing purposes. | 165 // Allow tests to access our innards for testing purposes. |
147 FRIEND_TEST(FieldTrialTest, Registration); | 166 FRIEND_TEST(FieldTrialTest, Registration); |
148 FRIEND_TEST(FieldTrialTest, AbsoluteProbabilities); | 167 FRIEND_TEST(FieldTrialTest, AbsoluteProbabilities); |
149 FRIEND_TEST(FieldTrialTest, RemainingProbability); | 168 FRIEND_TEST(FieldTrialTest, RemainingProbability); |
150 FRIEND_TEST(FieldTrialTest, FiftyFiftyProbability); | 169 FRIEND_TEST(FieldTrialTest, FiftyFiftyProbability); |
151 FRIEND_TEST(FieldTrialTest, MiddleProbabilities); | 170 FRIEND_TEST(FieldTrialTest, MiddleProbabilities); |
152 FRIEND_TEST(FieldTrialTest, OneWinner); | 171 FRIEND_TEST(FieldTrialTest, OneWinner); |
153 FRIEND_TEST(FieldTrialTest, DisableProbability); | 172 FRIEND_TEST(FieldTrialTest, DisableProbability); |
154 FRIEND_TEST(FieldTrialTest, Save); | 173 FRIEND_TEST(FieldTrialTest, Save); |
155 FRIEND_TEST(FieldTrialTest, DuplicateRestore); | 174 FRIEND_TEST(FieldTrialTest, DuplicateRestore); |
156 FRIEND_TEST(FieldTrialTest, MakeName); | 175 FRIEND_TEST(FieldTrialTest, MakeName); |
| 176 FRIEND_TEST(FieldTrialTest, HashClientId); |
| 177 FRIEND_TEST(FieldTrialTest, HashClientIdIsUniform); |
| 178 FRIEND_TEST(FieldTrialTest, UseOneTimeRandomization); |
157 | 179 |
158 friend class base::FieldTrialList; | 180 friend class base::FieldTrialList; |
159 | 181 |
160 friend class RefCounted<FieldTrial>; | 182 friend class RefCounted<FieldTrial>; |
161 | 183 |
162 virtual ~FieldTrial(); | 184 virtual ~FieldTrial(); |
163 | 185 |
164 // Returns the group_name. A winner need not have been chosen. | 186 // Returns the group_name. A winner need not have been chosen. |
165 std::string group_name_internal() const { return group_name_; } | 187 std::string group_name_internal() const { return group_name_; } |
166 | 188 |
167 // Get build time. | 189 // Get build time. |
168 static Time GetBuildTime(); | 190 static Time GetBuildTime(); |
169 | 191 |
| 192 // Calculates a uniformly-distributed double between [0.0, 1.0) given |
| 193 // a |client_id| and a |trial_name| (the latter is used as salt to avoid |
| 194 // separate one-time randomized trials from all having the same results). |
| 195 static double HashClientId(const std::string& client_id, |
| 196 const std::string& trial_name); |
| 197 |
170 // The name of the field trial, as can be found via the FieldTrialList. | 198 // 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. | |
172 const std::string name_; | 199 const std::string name_; |
173 | 200 |
174 // The maximum sum of all probabilities supplied, which corresponds to 100%. | 201 // The maximum sum of all probabilities supplied, which corresponds to 100%. |
175 // This is the scaling factor used to adjust supplied probabilities. | 202 // This is the scaling factor used to adjust supplied probabilities. |
176 const Probability divisor_; | 203 const Probability divisor_; |
177 | 204 |
178 // The name of the default group. | 205 // The name of the default group. |
179 const std::string default_group_name_; | 206 const std::string default_group_name_; |
180 | 207 |
181 // The randomly selected probability that is used to select a group (or have | 208 // 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 | 209 // the instance not participate). It is the product of divisor_ and a random |
183 // number between [0, 1). | 210 // number between [0, 1). |
184 const Probability random_; | 211 Probability random_; |
185 | 212 |
186 // Sum of the probabilities of all appended groups. | 213 // Sum of the probabilities of all appended groups. |
187 Probability accumulated_group_probability_; | 214 Probability accumulated_group_probability_; |
188 | 215 |
189 int next_group_number_; | 216 int next_group_number_; |
190 | 217 |
191 // The pseudo-randomly assigned group number. | 218 // The pseudo-randomly assigned group number. |
192 // This is kNotFinalized if no group has been assigned. | 219 // This is kNotFinalized if no group has been assigned. |
193 int group_; | 220 int group_; |
194 | 221 |
195 // A textual name for the randomly selected group. If this Trial is not a | 222 // A textual name for the randomly selected group. Valid after |group()| |
196 // member of an group, this string is empty. | 223 // has been called. |
197 std::string group_name_; | 224 std::string group_name_; |
198 | 225 |
199 // When disable_field_trial_ is true, field trial reverts to the 'default' | 226 // When enable_field_trial_ is false, field trial reverts to the 'default' |
200 // group. | 227 // group. |
201 bool disable_field_trial_; | 228 bool enable_field_trial_; |
202 | 229 |
203 // When benchmarking is enabled, field trials all revert to the 'default' | 230 // When benchmarking is enabled, field trials all revert to the 'default' |
204 // group. | 231 // group. |
205 static bool enable_benchmarking_; | 232 static bool enable_benchmarking_; |
206 | 233 |
207 DISALLOW_COPY_AND_ASSIGN(FieldTrial); | 234 DISALLOW_COPY_AND_ASSIGN(FieldTrial); |
208 }; | 235 }; |
209 | 236 |
210 //------------------------------------------------------------------------------ | 237 //------------------------------------------------------------------------------ |
211 // Class with a list of all active field trials. A trial is active if it has | 238 // Class with a list of all active field trials. A trial is active if it has |
(...skipping 12 matching lines...) Expand all Loading... |
224 ~FieldTrialList(); | 251 ~FieldTrialList(); |
225 | 252 |
226 // Register() stores a pointer to the given trial in a global map. | 253 // Register() stores a pointer to the given trial in a global map. |
227 // This method also AddRef's the indicated trial. | 254 // This method also AddRef's the indicated trial. |
228 static void Register(FieldTrial* trial); | 255 static void Register(FieldTrial* trial); |
229 | 256 |
230 // The Find() method can be used to test to see if a named Trial was already | 257 // The Find() method can be used to test to see if a named Trial was already |
231 // registered, or to retrieve a pointer to it from the global map. | 258 // registered, or to retrieve a pointer to it from the global map. |
232 static FieldTrial* Find(const std::string& name); | 259 static FieldTrial* Find(const std::string& name); |
233 | 260 |
| 261 // Returns the group number chosen for the named trial, or |
| 262 // FieldTrial::kNotFinalized if the trial does not exist. |
234 static int FindValue(const std::string& name); | 263 static int FindValue(const std::string& name); |
235 | 264 |
| 265 // Returns the group name chosen for the named trial, or the |
| 266 // empty string if the trial does not exist. |
236 static std::string FindFullName(const std::string& name); | 267 static std::string FindFullName(const std::string& name); |
237 | 268 |
| 269 // Returns true if the named trial has been registered. |
| 270 static bool TrialExists(const std::string& name); |
| 271 |
238 // Create a persistent representation of all FieldTrial instances for | 272 // Create a persistent representation of all FieldTrial instances for |
239 // resurrection in another process. This allows randomization to be done in | 273 // resurrection in another process. This allows randomization to be done in |
240 // one process, and secondary processes can by synchronized on the result. | 274 // one process, and secondary processes can by synchronized on the result. |
241 // The resulting string contains only the names, the trial name, and a "/" | 275 // The resulting string contains only the names, the trial name, and a "/" |
242 // separator. | 276 // separator. |
243 static void StatesToString(std::string* output); | 277 static void StatesToString(std::string* output); |
244 | 278 |
245 // Use a previously generated state string (re: StatesToString()) augment the | 279 // Use a previously generated state string (re: StatesToString()) augment the |
246 // current list of field tests to include the supplied tests, and using a 100% | 280 // current list of field tests to include the supplied tests, and using a 100% |
247 // probability for each test, force them to have the same group string. This | 281 // probability for each test, force them to have the same group string. This |
(...skipping 10 matching lines...) Expand all Loading... |
258 static TimeTicks application_start_time() { | 292 static TimeTicks application_start_time() { |
259 if (global_) | 293 if (global_) |
260 return global_->application_start_time_; | 294 return global_->application_start_time_; |
261 // For testing purposes only, or when we don't yet have a start time. | 295 // For testing purposes only, or when we don't yet have a start time. |
262 return TimeTicks::Now(); | 296 return TimeTicks::Now(); |
263 } | 297 } |
264 | 298 |
265 // Return the number of active field trials. | 299 // Return the number of active field trials. |
266 static size_t GetFieldTrialCount(); | 300 static size_t GetFieldTrialCount(); |
267 | 301 |
| 302 // Sets an opaque, diverse ID for this client that does not change |
| 303 // between sessions. This must be called exactly once before any call to |
| 304 // |FieldTrial::UseOneTimeRandomization()| and does not need to be called |
| 305 // unless such a call is made. |
| 306 static void EnableOneTimeRandomization(const std::string& client_id); |
| 307 |
| 308 // Returns true if you can call |FieldTrial::UseOneTimeRandomization()| |
| 309 // without error, i.e. if |EnableOneTimeRandomization()| has been called. |
| 310 static bool IsOneTimeRandomizationEnabled(); |
| 311 |
| 312 // Returns an opaque, diverse ID for this client that does not change |
| 313 // between sessions. |
| 314 // |
| 315 // Returns the empty string if |EnableOneTimeRandomization()| has not |
| 316 // been called. |
| 317 static const std::string& client_id(); |
| 318 |
268 private: | 319 private: |
269 // A map from FieldTrial names to the actual instances. | 320 // A map from FieldTrial names to the actual instances. |
270 typedef std::map<std::string, FieldTrial*> RegistrationList; | 321 typedef std::map<std::string, FieldTrial*> RegistrationList; |
271 | 322 |
272 // Helper function should be called only while holding lock_. | 323 // Helper function should be called only while holding lock_. |
273 FieldTrial* PreLockedFind(const std::string& name); | 324 FieldTrial* PreLockedFind(const std::string& name); |
274 | 325 |
275 static FieldTrialList* global_; // The singleton of this class. | 326 static FieldTrialList* global_; // The singleton of this class. |
276 | 327 |
277 // This will tell us if there is an attempt to register a field trial without | 328 // 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 | 329 // creating the FieldTrialList. This is not an error, unless a FieldTrialList |
279 // is created after that. | 330 // is created after that. |
280 static bool register_without_global_; | 331 static bool register_without_global_; |
281 | 332 |
282 // A helper value made availabel to users, that shows when the FieldTrialList | 333 // 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 | 334 // was initialized. Note that this is a singleton instance, and hence is a |
284 // good approximation to the start of the process. | 335 // good approximation to the start of the process. |
285 TimeTicks application_start_time_; | 336 TimeTicks application_start_time_; |
286 | 337 |
287 // Lock for access to registered_. | 338 // Lock for access to registered_. |
288 base::Lock lock_; | 339 base::Lock lock_; |
289 RegistrationList registered_; | 340 RegistrationList registered_; |
290 | 341 |
| 342 // An opaque, diverse ID for this client that does not change |
| 343 // between sessions, or the empty string if not initialized. |
| 344 std::string client_id_; |
| 345 |
291 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); | 346 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); |
292 }; | 347 }; |
293 | 348 |
294 } // namespace base | 349 } // namespace base |
295 | 350 |
296 #endif // BASE_METRICS_FIELD_TRIAL_H_ | 351 #endif // BASE_METRICS_FIELD_TRIAL_H_ |
297 | 352 |
OLD | NEW |