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

Side by Side Diff: base/metrics/field_trial.h

Issue 9705074: Supporting command line argument to force field trials (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: No more static default group number Created 8 years, 8 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
« no previous file with comments | « no previous file | base/metrics/field_trial.cc » ('j') | base/metrics/field_trial.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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.
136 void Disable(); 116 void Disable();
137 117
138 // Establish the name and probability of the next group in this trial. 118 // Establish the name and probability of the next group in this trial.
139 // Sometimes, based on construction randomization, this call may cause the 119 // Sometimes, based on construction randomization, this call may cause the
140 // provided group to be *THE* group selected for use in this instance. 120 // provided group to be *THE* group selected for use in this instance.
141 // The return value is the group number of the new group. 121 // The return value is the group number of the new group.
142 int AppendGroup(const std::string& name, Probability group_probability); 122 int AppendGroup(const std::string& name, Probability group_probability);
143 123
144 // Return the name of the FieldTrial (excluding the group name). 124 // Return the name of the FieldTrial (excluding the group name).
145 std::string name() const { return name_; } 125 std::string name() const { return name_; }
146 126
147 // Return the randomly selected group number that was assigned. 127 // 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 128 // 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. 129 // to attempt to probabilistically add any other groups to the trial.
151 int group(); 130 int group();
152 131
153 // If the group's name is empty, a string version containing the group 132 // If the group's name is empty, a string version containing the group
154 // number is used as the group name. 133 // number is used as the group name.
155 std::string group_name(); 134 std::string group_name();
156 135
157 // Gets the unique identifier of the Field Trial, but only if a group was 136 // 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 137 // 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 138 // is returned. When true is returned, the name and group ids were
160 // set in name_group_id. 139 // successfully set in name_group_id.
161 bool GetNameGroupId(NameGroupId* name_group_id); 140 bool GetNameGroupId(NameGroupId* name_group_id);
162 141
163 // Return the default group name of the FieldTrial. 142 // Return the default group name of the FieldTrial.
164 std::string default_group_name() const { return default_group_name_; } 143 std::string default_group_name() const { return default_group_name_; }
Alexei Svitkine (slow) 2012/03/29 19:19:07 Can you also remove this/make this private for the
MAD 2012/04/02 18:31:15 Good idea... Done!
165 144
166 // Helper function for the most common use: as an argument to specify the 145 // 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. 146 // name of a HISTOGRAM. Use the original histogram name as the name_prefix.
168 static std::string MakeName(const std::string& name_prefix, 147 static std::string MakeName(const std::string& name_prefix,
169 const std::string& trial_name); 148 const std::string& trial_name);
170 149
171 // Enable benchmarking sets field trials to a common setting. 150 // Enable benchmarking sets field trials to a common setting.
172 static void EnableBenchmarking(); 151 static void EnableBenchmarking();
173 152
174 private: 153 private:
(...skipping 11 matching lines...) Expand all
186 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, HashClientId); 165 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, HashClientId);
187 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, HashClientIdIsUniform); 166 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, HashClientIdIsUniform);
188 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, HashName); 167 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, HashName);
189 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, NameGroupIds); 168 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, NameGroupIds);
190 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, UseOneTimeRandomization); 169 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, UseOneTimeRandomization);
191 170
192 friend class base::FieldTrialList; 171 friend class base::FieldTrialList;
193 172
194 friend class RefCounted<FieldTrial>; 173 friend class RefCounted<FieldTrial>;
195 174
175 // This is the group number of the 'default' group when a choice wasn't forced
176 // by a call to FieldTrialList::CreateFieldTrial. It is kept private so that
177 // consumers don't use it by mistake in cases where the group was forced.
178 static const int kDefaultGroupNumber;
179
180 FieldTrial(const std::string& name, Probability total_probability,
181 const std::string& default_group_name, const int year,
182 const int month, const int day_of_month);
196 virtual ~FieldTrial(); 183 virtual ~FieldTrial();
197 184
198 // Returns the group_name. A winner need not have been chosen. 185 // Returns the group_name. A winner need not have been chosen.
199 std::string group_name_internal() const { return group_name_; } 186 std::string group_name_internal() const { return group_name_; }
200 187
201 // Calculates a uniformly-distributed double between [0.0, 1.0) given 188 // 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 189 // 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). 190 // separate one-time randomized trials from all having the same results).
204 static double HashClientId(const std::string& client_id, 191 static double HashClientId(const std::string& client_id,
205 const std::string& trial_name); 192 const std::string& trial_name);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 std::string group_name_; 227 std::string group_name_;
241 228
242 // The hashed name of the group to be sent as a unique identifier. 229 // The hashed name of the group to be sent as a unique identifier.
243 // Is not valid while group_ is equal to kNotFinalized. 230 // Is not valid while group_ is equal to kNotFinalized.
244 uint32 group_name_hash_; 231 uint32 group_name_hash_;
245 232
246 // When enable_field_trial_ is false, field trial reverts to the 'default' 233 // When enable_field_trial_ is false, field trial reverts to the 'default'
247 // group. 234 // group.
248 bool enable_field_trial_; 235 bool enable_field_trial_;
249 236
237 // When forced_ is true, we return the chosen group from AppendGroup when
238 // appropriate.
239 bool forced_;
240
250 // When benchmarking is enabled, field trials all revert to the 'default' 241 // When benchmarking is enabled, field trials all revert to the 'default'
251 // group. 242 // group.
252 static bool enable_benchmarking_; 243 static bool enable_benchmarking_;
253 244
254 // This value is reserved for an uninitialized hash value. 245 // This value is reserved for an uninitialized hash value.
255 static const uint32 kReservedHashValue; 246 static const uint32 kReservedHashValue;
256 247
257 DISALLOW_COPY_AND_ASSIGN(FieldTrial); 248 DISALLOW_COPY_AND_ASSIGN(FieldTrial);
258 }; 249 };
259 250
260 //------------------------------------------------------------------------------ 251 //------------------------------------------------------------------------------
261 // Class with a list of all active field trials. A trial is active if it has 252 // 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. 253 // been registered, which includes evaluating its state based on its probaility.
263 // Only one instance of this class exists. 254 // Only one instance of this class exists.
264 class BASE_EXPORT FieldTrialList { 255 class BASE_EXPORT FieldTrialList {
265 public: 256 public:
266 // Define a separator charactor to use when creating a persistent form of an 257 // 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 258 // 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). 259 // second process to mimic our state (i.e., provide the same group name).
269 static const char kPersistentStringSeparator; // Currently a slash. 260 static const char kPersistentStringSeparator; // Currently a slash.
270 261
271 // Define expiration year in future. It is initialized to two years from Now. 262 // Define expiration year in future. It is initialized to two years from Now.
272 static int kExpirationYearInFuture; 263 static int kExpirationYearInFuture;
273 264
274 // Observer is notified when a FieldTrial's group is selected. 265 // Observer is notified when a FieldTrial's group is selected.
275 class Observer { 266 class Observer {
276 public: 267 public:
277 // Notify observers when FieldTrials's group is selected. 268 // Notify observers when FieldTrials's group is selected.
278 virtual void OnFieldTrialGroupFinalized(const std::string& trial_name, 269 virtual void OnFieldTrialGroupFinalized(const std::string& trial_name,
279 const std::string& group_name) = 0; 270 const std::string& group_name) = 0;
280 271
281 protected: 272 protected:
282 virtual ~Observer() {} 273 virtual ~Observer() {}
283 }; 274 };
284 275
285 // This singleton holds the global list of registered FieldTrials. 276 // This singleton holds the global list of registered FieldTrials.
286 // 277 //
287 // |client_id| should be an opaque, diverse ID for this client that does not 278 // |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 279 // change between sessions, to enable one-time randomized trials. The empty
289 // string may be provided, in which case one-time randomized trials will 280 // string may be provided, in which case one-time randomized trials will
290 // not be available. 281 // not be available.
291 explicit FieldTrialList(const std::string& client_id); 282 explicit FieldTrialList(const std::string& client_id);
292 // Destructor Release()'s references to all registered FieldTrial instances. 283 // Destructor Release()'s references to all registered FieldTrial instances.
293 ~FieldTrialList(); 284 ~FieldTrialList();
294 285
295 // Register() stores a pointer to the given trial in a global map. 286 // Creates a new FieldTrial instance if none exists already. If one exists,
296 // This method also AddRef's the indicated trial. 287 // it has to be because it was forced by CreateFieldTrial.
297 static void Register(FieldTrial* trial); 288 //
289 // The name is used to register the instance with the FieldTrialList class,
290 // and can be used to find the trial (only one trial can be present for each
291 // name). |name| and |default_group_name| may not be empty but
Alexei Svitkine (slow) 2012/03/29 19:19:07 Nit: Either remove the |'s around |name| and |defa
MAD 2012/04/02 18:31:15 Done.
292 // default_group_number can be NULL if the value is not needed.
Alexei Svitkine (slow) 2012/03/29 19:19:07 Please explain what |default_group_number| is for,
MAD 2012/04/02 18:31:15 Done.
293 //
294 // Group probabilities that are later supplied must sum to less than or equal
295 // to the total_probability. Arguments year, month and day_of_month specify
296 // the expiration time. If the build time is after the expiration time then
297 // the field trial reverts to the 'default' group.
298 //
299 // Use this static method to get a startup-randomized FieldTrial or a
300 // previously created forced FieldTrial. If you want a one-time randomized
301 // trial, call UseOneTimeRandomization() right after creation.
302 static FieldTrial* GetFieldTrialInstance(
303 const std::string& name, FieldTrial::Probability total_probability,
304 const std::string& default_group_name, int* default_group_number,
305 const int year, const int month, const int day_of_month);
298 306
299 // The Find() method can be used to test to see if a named Trial was already 307 // 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. 308 // registered, or to retrieve a pointer to it from the global map.
301 static FieldTrial* Find(const std::string& name); 309 static FieldTrial* Find(const std::string& name);
302 310
303 // Returns the group number chosen for the named trial, or 311 // Returns the group number chosen for the named trial, or
304 // FieldTrial::kNotFinalized if the trial does not exist. 312 // FieldTrial::kNotFinalized if the trial does not exist.
305 static int FindValue(const std::string& name); 313 static int FindValue(const std::string& name);
306 314
307 // Returns the group name chosen for the named trial, or the 315 // Returns the group name chosen for the named trial, or the
308 // empty string if the trial does not exist. 316 // empty string if the trial does not exist.
309 static std::string FindFullName(const std::string& name); 317 static std::string FindFullName(const std::string& name);
310 318
311 // Returns true if the named trial has been registered. 319 // Returns true if the named trial has been registered.
312 static bool TrialExists(const std::string& name); 320 static bool TrialExists(const std::string& name);
313 321
314 // Create a persistent representation of all FieldTrial instances and the 322 // Creates a persistent representation of all FieldTrial instances for
315 // |client_id()| state for resurrection in another process. This allows 323 // resurrection in another process. This allows randomization to be done in
316 // randomization to be done in one process, and secondary processes can be 324 // one process, and secondary processes can be synchronized on the result.
317 // synchronized on the result. The resulting string contains the 325 // The resulting string contains the name and group name pairs for all trials,
318 // |client_id()|, the names, the trial name, and a "/" separator. 326 // with "/" used to separate all names and to terminate the string. This
327 // string is parsed by CreateTrialsFromString().
319 static void StatesToString(std::string* output); 328 static void StatesToString(std::string* output);
320 329
321 // Returns an array of Unique IDs for each Field Trial that has a chosen 330 // 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 331 // group. Field Trials for which a group has not been chosen yet are NOT
323 // returned in this list. 332 // returned in this list.
324 static void GetFieldTrialNameGroupIds( 333 static void GetFieldTrialNameGroupIds(
325 std::vector<FieldTrial::NameGroupId>* name_group_ids); 334 std::vector<FieldTrial::NameGroupId>* name_group_ids);
326 335
327 // Use a previously generated state string (re: StatesToString()) augment the 336 // 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% 337 // 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 338 // 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 339 // 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 340 // process into this non-browser process, but could also be invoked through a
332 // CreateFieldTrial to create the FieldTrial in the non-browser process. 341 // command line argument to the browser process.
333 // Currently only the group_name_ and name_ are restored, as well as the 342 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 343
338 // Create a FieldTrial with the given |name| and using 100% probability for 344 // Create a FieldTrial with the given |name| and using 100% probability for
339 // the FieldTrial, force FieldTrial to have the same group string as 345 // 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 346 // |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. 347 // 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 348 // 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 349 // 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, 350 static FieldTrial* CreateFieldTrial(const std::string& name,
346 const std::string& group_name); 351 const std::string& group_name);
347 352
348 // Add an observer to be notified when a field trial is irrevocably committed 353 // 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 354 // to being part of some specific field_group (and hence the group_name is
350 // also finalized for that field_trial). 355 // also finalized for that field_trial).
351 static void AddObserver(Observer* observer); 356 static void AddObserver(Observer* observer);
352 357
353 // Remove an observer. 358 // Remove an observer.
354 static void RemoveObserver(Observer* observer); 359 static void RemoveObserver(Observer* observer);
(...skipping 28 matching lines...) Expand all
383 // Returns the empty string if one-time randomization is not enabled. 388 // Returns the empty string if one-time randomization is not enabled.
384 static const std::string& client_id(); 389 static const std::string& client_id();
385 390
386 private: 391 private:
387 // A map from FieldTrial names to the actual instances. 392 // A map from FieldTrial names to the actual instances.
388 typedef std::map<std::string, FieldTrial*> RegistrationList; 393 typedef std::map<std::string, FieldTrial*> RegistrationList;
389 394
390 // Helper function should be called only while holding lock_. 395 // Helper function should be called only while holding lock_.
391 FieldTrial* PreLockedFind(const std::string& name); 396 FieldTrial* PreLockedFind(const std::string& name);
392 397
398 // Register() stores a pointer to the given trial in a global map.
399 // This method also AddRef's the indicated trial.
400 // This should always be called after creating a new FieldTrial instance.
401 static void Register(FieldTrial* trial);
402
393 static FieldTrialList* global_; // The singleton of this class. 403 static FieldTrialList* global_; // The singleton of this class.
394 404
395 // This will tell us if there is an attempt to register a field 405 // This will tell us if there is an attempt to register a field
396 // trial or check if one-time randomization is enabled without 406 // trial or check if one-time randomization is enabled without
397 // creating the FieldTrialList. This is not an error, unless a 407 // creating the FieldTrialList. This is not an error, unless a
398 // FieldTrialList is created after that. 408 // FieldTrialList is created after that.
399 static bool used_without_global_; 409 static bool used_without_global_;
400 410
401 // A helper value made available to users, that shows when the FieldTrialList 411 // 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 412 // was initialized. Note that this is a singleton instance, and hence is a
(...skipping 10 matching lines...) Expand all
413 423
414 // List of observers to be notified when a group is selected for a FieldTrial. 424 // List of observers to be notified when a group is selected for a FieldTrial.
415 ObserverList<Observer> observer_list_; 425 ObserverList<Observer> observer_list_;
416 426
417 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); 427 DISALLOW_COPY_AND_ASSIGN(FieldTrialList);
418 }; 428 };
419 429
420 } // namespace base 430 } // namespace base
421 431
422 #endif // BASE_METRICS_FIELD_TRIAL_H_ 432 #endif // BASE_METRICS_FIELD_TRIAL_H_
OLDNEW
« no previous file with comments | « no previous file | base/metrics/field_trial.cc » ('j') | base/metrics/field_trial.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698