OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 // hashes of the trial and group name strings. | 96 // hashes of the trial and group name strings. |
97 struct NameGroupId { | 97 struct NameGroupId { |
98 uint32 name; | 98 uint32 name; |
99 uint32 group; | 99 uint32 group; |
100 }; | 100 }; |
101 | 101 |
102 // A return value to indicate that a given instance has not yet had a group | 102 // A return value to indicate that a given instance has not yet had a group |
103 // assignment (and hence is not yet participating in the trial). | 103 // assignment (and hence is not yet participating in the trial). |
104 static const int kNotFinalized; | 104 static const int kNotFinalized; |
105 | 105 |
106 // This is the group number of the 'default' group. This provides an easy way | |
107 // to assign all the remaining probability to a group ('default'). | |
108 static const int kDefaultGroupNumber; | |
109 | |
110 // The name is used to register the instance with the FieldTrialList class, | |
111 // and can be used to find the trial (only one trial can be present for each | |
112 // name). |name| and |default_group_name| may not be empty. | |
113 // | |
114 // Group probabilities that are later supplied must sum to less than or equal | |
115 // to the total_probability. Arguments year, month and day_of_month specify | |
116 // the expiration time. If the build time is after the expiration time then | |
117 // the field trial reverts to the 'default' group. | |
118 // | |
119 // Using this constructor creates a startup-randomized FieldTrial. If you | |
120 // want a one-time randomized trial, call UseOneTimeRandomization() right | |
121 // after construction. | |
122 FieldTrial(const std::string& name, Probability total_probability, | |
123 const std::string& default_group_name, const int year, | |
124 const int month, const int day_of_month); | |
125 | |
126 // Changes the field trial to use one-time randomization, i.e. produce the | 106 // Changes the field trial to use one-time randomization, i.e. produce the |
127 // same result for the current trial on every run of this client. Must be | 107 // same result for the current trial on every run of this client. Must be |
128 // called right after construction. | 108 // called right after construction. |
129 void UseOneTimeRandomization(); | 109 void UseOneTimeRandomization(); |
130 | 110 |
131 // Disables this trial, meaning it always determines the default group | 111 // Disables this trial, meaning it always determines the default group |
132 // has been selected. May be called immediately after construction, or | 112 // has been selected. May be called immediately after construction, or |
133 // at any time after initialization (should not be interleaved with | 113 // at any time after initialization (should not be interleaved with |
134 // AppendGroup calls). Once disabled, there is no way to re-enable a | 114 // AppendGroup calls). Once disabled, there is no way to re-enable a |
135 // trial. | 115 // trial. |
| 116 // TODO(mad): http://code.google.com/p/chromium/issues/detail?id=121446 |
| 117 // This doesn't properly reset to Default when a group was forced. |
136 void Disable(); | 118 void Disable(); |
137 | 119 |
138 // Establish the name and probability of the next group in this trial. | 120 // Establish the name and probability of the next group in this trial. |
139 // Sometimes, based on construction randomization, this call may cause the | 121 // Sometimes, based on construction randomization, this call may cause the |
140 // provided group to be *THE* group selected for use in this instance. | 122 // provided group to be *THE* group selected for use in this instance. |
141 // The return value is the group number of the new group. | 123 // The return value is the group number of the new group. |
142 int AppendGroup(const std::string& name, Probability group_probability); | 124 int AppendGroup(const std::string& name, Probability group_probability); |
143 | 125 |
144 // Return the name of the FieldTrial (excluding the group name). | 126 // Return the name of the FieldTrial (excluding the group name). |
145 std::string name() const { return name_; } | 127 std::string name() const { return name_; } |
146 | 128 |
147 // Return the randomly selected group number that was assigned. | 129 // Return the randomly selected group number that was assigned. |
148 // Return kDefaultGroupNumber if the instance is in the 'default' group. | |
149 // Note that this will force an instance to participate, and make it illegal | 130 // Note that this will force an instance to participate, and make it illegal |
150 // to attempt to probabilistically add any other groups to the trial. | 131 // to attempt to probabilistically add any other groups to the trial. |
151 int group(); | 132 int group(); |
152 | 133 |
153 // If the group's name is empty, a string version containing the group | 134 // If the group's name is empty, a string version containing the group number |
154 // number is used as the group name. | 135 // is used as the group name. This causes a winner to be chosen if none was. |
155 std::string group_name(); | 136 std::string group_name(); |
156 | 137 |
157 // Gets the unique identifier of the Field Trial, but only if a group was | 138 // Gets the unique identifier of the Field Trial, but only if a group was |
158 // officially chosen, otherwise name_group_id is left untouched and false | 139 // officially chosen, otherwise name_group_id is left untouched and false |
159 // is returned. When true is returned, the name and group ids were successfuly | 140 // is returned. When true is returned, the name and group ids were |
160 // set in name_group_id. | 141 // successfully set in name_group_id. |
161 bool GetNameGroupId(NameGroupId* name_group_id); | 142 bool GetNameGroupId(NameGroupId* name_group_id); |
162 | 143 |
163 // Return the default group name of the FieldTrial. | |
164 std::string default_group_name() const { return default_group_name_; } | |
165 | |
166 // Helper function for the most common use: as an argument to specify the | 144 // Helper function for the most common use: as an argument to specify the |
167 // name of a HISTOGRAM. Use the original histogram name as the name_prefix. | 145 // name of a HISTOGRAM. Use the original histogram name as the name_prefix. |
168 static std::string MakeName(const std::string& name_prefix, | 146 static std::string MakeName(const std::string& name_prefix, |
169 const std::string& trial_name); | 147 const std::string& trial_name); |
170 | 148 |
171 // Enable benchmarking sets field trials to a common setting. | 149 // Enable benchmarking sets field trials to a common setting. |
172 static void EnableBenchmarking(); | 150 static void EnableBenchmarking(); |
173 | 151 |
174 private: | 152 private: |
175 // Allow tests to access our innards for testing purposes. | 153 // Allow tests to access our innards for testing purposes. |
(...skipping 10 matching lines...) Expand all Loading... |
186 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, HashClientId); | 164 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, HashClientId); |
187 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, HashClientIdIsUniform); | 165 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, HashClientIdIsUniform); |
188 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, HashName); | 166 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, HashName); |
189 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, NameGroupIds); | 167 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, NameGroupIds); |
190 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, UseOneTimeRandomization); | 168 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, UseOneTimeRandomization); |
191 | 169 |
192 friend class base::FieldTrialList; | 170 friend class base::FieldTrialList; |
193 | 171 |
194 friend class RefCounted<FieldTrial>; | 172 friend class RefCounted<FieldTrial>; |
195 | 173 |
| 174 // This is the group number of the 'default' group when a choice wasn't forced |
| 175 // by a call to FieldTrialList::CreateFieldTrial. It is kept private so that |
| 176 // consumers don't use it by mistake in cases where the group was forced. |
| 177 static const int kDefaultGroupNumber; |
| 178 |
| 179 FieldTrial(const std::string& name, Probability total_probability, |
| 180 const std::string& default_group_name, const int year, |
| 181 const int month, const int day_of_month); |
196 virtual ~FieldTrial(); | 182 virtual ~FieldTrial(); |
197 | 183 |
| 184 // Return the default group name of the FieldTrial. |
| 185 std::string default_group_name() const { return default_group_name_; } |
| 186 |
| 187 // Sets the group_name as well as group_name_hash to make sure they are sync. |
| 188 void SetGroupChoice(const std::string& name, int number); |
| 189 |
198 // Returns the group_name. A winner need not have been chosen. | 190 // Returns the group_name. A winner need not have been chosen. |
199 std::string group_name_internal() const { return group_name_; } | 191 std::string group_name_internal() const { return group_name_; } |
200 | 192 |
201 // Calculates a uniformly-distributed double between [0.0, 1.0) given | 193 // Calculates a uniformly-distributed double between [0.0, 1.0) given |
202 // a |client_id| and a |trial_name| (the latter is used as salt to avoid | 194 // a |client_id| and a |trial_name| (the latter is used as salt to avoid |
203 // separate one-time randomized trials from all having the same results). | 195 // separate one-time randomized trials from all having the same results). |
204 static double HashClientId(const std::string& client_id, | 196 static double HashClientId(const std::string& client_id, |
205 const std::string& trial_name); | 197 const std::string& trial_name); |
206 | 198 |
207 // Creates unique identifier for the trial by hashing a name string, whether | 199 // Creates unique identifier for the trial by hashing a name string, whether |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 std::string group_name_; | 232 std::string group_name_; |
241 | 233 |
242 // The hashed name of the group to be sent as a unique identifier. | 234 // The hashed name of the group to be sent as a unique identifier. |
243 // Is not valid while group_ is equal to kNotFinalized. | 235 // Is not valid while group_ is equal to kNotFinalized. |
244 uint32 group_name_hash_; | 236 uint32 group_name_hash_; |
245 | 237 |
246 // When enable_field_trial_ is false, field trial reverts to the 'default' | 238 // When enable_field_trial_ is false, field trial reverts to the 'default' |
247 // group. | 239 // group. |
248 bool enable_field_trial_; | 240 bool enable_field_trial_; |
249 | 241 |
| 242 // When forced_ is true, we return the chosen group from AppendGroup when |
| 243 // appropriate. |
| 244 bool forced_; |
| 245 |
250 // When benchmarking is enabled, field trials all revert to the 'default' | 246 // When benchmarking is enabled, field trials all revert to the 'default' |
251 // group. | 247 // group. |
252 static bool enable_benchmarking_; | 248 static bool enable_benchmarking_; |
253 | 249 |
254 // This value is reserved for an uninitialized hash value. | 250 // This value is reserved for an uninitialized hash value. |
255 static const uint32 kReservedHashValue; | 251 static const uint32 kReservedHashValue; |
256 | 252 |
257 DISALLOW_COPY_AND_ASSIGN(FieldTrial); | 253 DISALLOW_COPY_AND_ASSIGN(FieldTrial); |
258 }; | 254 }; |
259 | 255 |
260 //------------------------------------------------------------------------------ | 256 //------------------------------------------------------------------------------ |
261 // Class with a list of all active field trials. A trial is active if it has | 257 // Class with a list of all active field trials. A trial is active if it has |
262 // been registered, which includes evaluating its state based on its probaility. | 258 // been registered, which includes evaluating its state based on its probaility. |
263 // Only one instance of this class exists. | 259 // Only one instance of this class exists. |
264 class BASE_EXPORT FieldTrialList { | 260 class BASE_EXPORT FieldTrialList { |
265 public: | 261 public: |
266 // Define a separator charactor to use when creating a persistent form of an | 262 // Define a separator character to use when creating a persistent form of an |
267 // instance. This is intended for use as a command line argument, passed to a | 263 // instance. This is intended for use as a command line argument, passed to a |
268 // second process to mimic our state (i.e., provide the same group name). | 264 // second process to mimic our state (i.e., provide the same group name). |
269 static const char kPersistentStringSeparator; // Currently a slash. | 265 static const char kPersistentStringSeparator; // Currently a slash. |
270 | 266 |
271 // Define expiration year in future. It is initialized to two years from Now. | 267 // Define expiration year in future. It is initialized to two years from Now. |
272 static int kExpirationYearInFuture; | 268 static int kExpirationYearInFuture; |
273 | 269 |
274 // Observer is notified when a FieldTrial's group is selected. | 270 // Observer is notified when a FieldTrial's group is selected. |
275 class Observer { | 271 class Observer { |
276 public: | 272 public: |
277 // Notify observers when FieldTrials's group is selected. | 273 // Notify observers when FieldTrials's group is selected. |
278 virtual void OnFieldTrialGroupFinalized(const std::string& trial_name, | 274 virtual void OnFieldTrialGroupFinalized(const std::string& trial_name, |
279 const std::string& group_name) = 0; | 275 const std::string& group_name) = 0; |
280 | 276 |
281 protected: | 277 protected: |
282 virtual ~Observer() {} | 278 virtual ~Observer() {} |
283 }; | 279 }; |
284 | 280 |
285 // This singleton holds the global list of registered FieldTrials. | 281 // This singleton holds the global list of registered FieldTrials. |
286 // | 282 // |
287 // |client_id| should be an opaque, diverse ID for this client that does not | 283 // |client_id| should be an opaque, diverse ID for this client that does not |
288 // change between sessions, to enable one-time randomized trials. The empty | 284 // change between sessions, to enable one-time randomized trials. The empty |
289 // string may be provided, in which case one-time randomized trials will | 285 // string may be provided, in which case one-time randomized trials will |
290 // not be available. | 286 // not be available. |
291 explicit FieldTrialList(const std::string& client_id); | 287 explicit FieldTrialList(const std::string& client_id); |
292 // Destructor Release()'s references to all registered FieldTrial instances. | 288 // Destructor Release()'s references to all registered FieldTrial instances. |
293 ~FieldTrialList(); | 289 ~FieldTrialList(); |
294 | 290 |
295 // Register() stores a pointer to the given trial in a global map. | 291 // Get a FieldTrial instance from the factory. |
296 // This method also AddRef's the indicated trial. | 292 // |
297 static void Register(FieldTrial* trial); | 293 // |name| is used to register the instance with the FieldTrialList class, |
| 294 // and can be used to find the trial (only one trial can be present for each |
| 295 // name). |default_group_name| is the name of the default group which will |
| 296 // be chosen if none of the subsequent appended groups get to be chosen. |
| 297 // |default_group_number| can receive the group number of the default group as |
| 298 // AppendGroup returns the number of the subsequence groups. |name| and |
| 299 // |default_group_name| may not be empty but |default_group_number| can be |
| 300 // NULL if the value is not needed. |
| 301 // |
| 302 // Group probabilities that are later supplied must sum to less than or equal |
| 303 // to the |total_probability|. Arguments |year|, |month| and |day_of_month| |
| 304 // specify the expiration time. If the build time is after the expiration time |
| 305 // then the field trial reverts to the 'default' group. |
| 306 // |
| 307 // Use this static method to get a startup-randomized FieldTrial or a |
| 308 // previously created forced FieldTrial. If you want a one-time randomized |
| 309 // trial, call UseOneTimeRandomization() right after creation. |
| 310 static FieldTrial* FactoryGetFieldTrial( |
| 311 const std::string& name, |
| 312 FieldTrial::Probability total_probability, |
| 313 const std::string& default_group_name, |
| 314 const int year, |
| 315 const int month, |
| 316 const int day_of_month, |
| 317 int* default_group_number); |
298 | 318 |
299 // The Find() method can be used to test to see if a named Trial was already | 319 // The Find() method can be used to test to see if a named Trial was already |
300 // registered, or to retrieve a pointer to it from the global map. | 320 // registered, or to retrieve a pointer to it from the global map. |
301 static FieldTrial* Find(const std::string& name); | 321 static FieldTrial* Find(const std::string& name); |
302 | 322 |
303 // Returns the group number chosen for the named trial, or | 323 // Returns the group number chosen for the named trial, or |
304 // FieldTrial::kNotFinalized if the trial does not exist. | 324 // FieldTrial::kNotFinalized if the trial does not exist. |
305 static int FindValue(const std::string& name); | 325 static int FindValue(const std::string& name); |
306 | 326 |
307 // Returns the group name chosen for the named trial, or the | 327 // Returns the group name chosen for the named trial, or the |
308 // empty string if the trial does not exist. | 328 // empty string if the trial does not exist. |
309 static std::string FindFullName(const std::string& name); | 329 static std::string FindFullName(const std::string& name); |
310 | 330 |
311 // Returns true if the named trial has been registered. | 331 // Returns true if the named trial has been registered. |
312 static bool TrialExists(const std::string& name); | 332 static bool TrialExists(const std::string& name); |
313 | 333 |
314 // Create a persistent representation of all FieldTrial instances and the | 334 // Creates a persistent representation of all FieldTrial instances for |
315 // |client_id()| state for resurrection in another process. This allows | 335 // resurrection in another process. This allows randomization to be done in |
316 // randomization to be done in one process, and secondary processes can be | 336 // one process, and secondary processes can be synchronized on the result. |
317 // synchronized on the result. The resulting string contains the | 337 // The resulting string contains the name and group name pairs for all trials, |
318 // |client_id()|, the names, the trial name, and a "/" separator. | 338 // with "/" used to separate all names and to terminate the string. This |
| 339 // string is parsed by CreateTrialsFromString(). |
319 static void StatesToString(std::string* output); | 340 static void StatesToString(std::string* output); |
320 | 341 |
321 // Returns an array of Unique IDs for each Field Trial that has a chosen | 342 // Returns an array of Unique IDs for each Field Trial that has a chosen |
322 // group. Field Trials for which a group has not been chosen yet are NOT | 343 // group. Field Trials for which a group has not been chosen yet are NOT |
323 // returned in this list. | 344 // returned in this list. |
324 static void GetFieldTrialNameGroupIds( | 345 static void GetFieldTrialNameGroupIds( |
325 std::vector<FieldTrial::NameGroupId>* name_group_ids); | 346 std::vector<FieldTrial::NameGroupId>* name_group_ids); |
326 | 347 |
327 // Use a previously generated state string (re: StatesToString()) augment the | 348 // Use a state string (re: StatesToString()) to augment the current list of |
328 // current list of field tests to include the supplied tests, and using a 100% | 349 // field tests to include the supplied tests, and using a 100% probability for |
329 // probability for each test, force them to have the same group string. This | 350 // each test, force them to have the same group string. This is commonly used |
330 // is commonly used in a non-browser process, to carry randomly selected state | 351 // in a non-browser process, to carry randomly selected state in a browser |
331 // in a browser process into this non-browser process. This method calls | 352 // process into this non-browser process, but could also be invoked through a |
332 // CreateFieldTrial to create the FieldTrial in the non-browser process. | 353 // command line argument to the browser process. |
333 // Currently only the group_name_ and name_ are restored, as well as the | 354 static bool CreateTrialsFromString(const std::string& prior_trials); |
334 // |client_id()| state, that could be used for one-time randomized trials | |
335 // set up only in child processes. | |
336 static bool CreateTrialsInChildProcess(const std::string& prior_trials); | |
337 | 355 |
338 // Create a FieldTrial with the given |name| and using 100% probability for | 356 // Create a FieldTrial with the given |name| and using 100% probability for |
339 // the FieldTrial, force FieldTrial to have the same group string as | 357 // the FieldTrial, force FieldTrial to have the same group string as |
340 // |group_name|. This is commonly used in a non-browser process, to carry | 358 // |group_name|. This is commonly used in a non-browser process, to carry |
341 // randomly selected state in a browser process into this non-browser process. | 359 // randomly selected state in a browser process into this non-browser process. |
342 // Currently only the group_name_ and name_ are set in the FieldTrial. It | 360 // It returns NULL if there is a FieldTrial that is already registered with |
343 // returns NULL if there is a FieldTrial that is already registered with the | 361 // the same |name| but has different finalized group string (|group_name|). |
344 // same |name| but has different finalized group string (|group_name|). | |
345 static FieldTrial* CreateFieldTrial(const std::string& name, | 362 static FieldTrial* CreateFieldTrial(const std::string& name, |
346 const std::string& group_name); | 363 const std::string& group_name); |
347 | 364 |
348 // Add an observer to be notified when a field trial is irrevocably committed | 365 // Add an observer to be notified when a field trial is irrevocably committed |
349 // to being part of some specific field_group (and hence the group_name is | 366 // to being part of some specific field_group (and hence the group_name is |
350 // also finalized for that field_trial). | 367 // also finalized for that field_trial). |
351 static void AddObserver(Observer* observer); | 368 static void AddObserver(Observer* observer); |
352 | 369 |
353 // Remove an observer. | 370 // Remove an observer. |
354 static void RemoveObserver(Observer* observer); | 371 static void RemoveObserver(Observer* observer); |
(...skipping 28 matching lines...) Expand all Loading... |
383 // Returns the empty string if one-time randomization is not enabled. | 400 // Returns the empty string if one-time randomization is not enabled. |
384 static const std::string& client_id(); | 401 static const std::string& client_id(); |
385 | 402 |
386 private: | 403 private: |
387 // A map from FieldTrial names to the actual instances. | 404 // A map from FieldTrial names to the actual instances. |
388 typedef std::map<std::string, FieldTrial*> RegistrationList; | 405 typedef std::map<std::string, FieldTrial*> RegistrationList; |
389 | 406 |
390 // Helper function should be called only while holding lock_. | 407 // Helper function should be called only while holding lock_. |
391 FieldTrial* PreLockedFind(const std::string& name); | 408 FieldTrial* PreLockedFind(const std::string& name); |
392 | 409 |
| 410 // Register() stores a pointer to the given trial in a global map. |
| 411 // This method also AddRef's the indicated trial. |
| 412 // This should always be called after creating a new FieldTrial instance. |
| 413 static void Register(FieldTrial* trial); |
| 414 |
393 static FieldTrialList* global_; // The singleton of this class. | 415 static FieldTrialList* global_; // The singleton of this class. |
394 | 416 |
395 // This will tell us if there is an attempt to register a field | 417 // This will tell us if there is an attempt to register a field |
396 // trial or check if one-time randomization is enabled without | 418 // trial or check if one-time randomization is enabled without |
397 // creating the FieldTrialList. This is not an error, unless a | 419 // creating the FieldTrialList. This is not an error, unless a |
398 // FieldTrialList is created after that. | 420 // FieldTrialList is created after that. |
399 static bool used_without_global_; | 421 static bool used_without_global_; |
400 | 422 |
401 // A helper value made available to users, that shows when the FieldTrialList | 423 // A helper value made available to users, that shows when the FieldTrialList |
402 // was initialized. Note that this is a singleton instance, and hence is a | 424 // was initialized. Note that this is a singleton instance, and hence is a |
(...skipping 10 matching lines...) Expand all Loading... |
413 | 435 |
414 // List of observers to be notified when a group is selected for a FieldTrial. | 436 // List of observers to be notified when a group is selected for a FieldTrial. |
415 ObserverList<Observer> observer_list_; | 437 ObserverList<Observer> observer_list_; |
416 | 438 |
417 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); | 439 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); |
418 }; | 440 }; |
419 | 441 |
420 } // namespace base | 442 } // namespace base |
421 | 443 |
422 #endif // BASE_METRICS_FIELD_TRIAL_H_ | 444 #endif // BASE_METRICS_FIELD_TRIAL_H_ |
OLD | NEW |