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

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: Created 8 years, 9 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 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 109
110 // The name is used to register the instance with the FieldTrialList class, 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 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. 112 // name). |name| and |default_group_name| may not be empty.
113 // 113 //
114 // Group probabilities that are later supplied must sum to less than or equal 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 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 116 // the expiration time. If the build time is after the expiration time then
117 // the field trial reverts to the 'default' group. 117 // the field trial reverts to the 'default' group.
118 // 118 //
119 // Using this constructor creates a startup-randomized FieldTrial. If you 119 // Using this constructor creates a startup-randomized FieldTrial. If you
Alexei Svitkine (slow) 2012/03/23 18:41:55 Update this comment, since this is no longer a con
MAD 2012/03/23 20:20:12 Done.
120 // want a one-time randomized trial, call UseOneTimeRandomization() right 120 // want a one-time randomized trial, call UseOneTimeRandomization() right
121 // after construction. 121 // after construction.
122 FieldTrial(const std::string& name, Probability total_probability, 122 static FieldTrial* CreateInstance(
123 const std::string& default_group_name, const int year, 123 const std::string& name, Probability total_probability,
124 const int month, const int day_of_month); 124 const std::string& default_group_name, const int year, const int month,
125 const int day_of_month);
125 126
126 // Changes the field trial to use one-time randomization, i.e. produce the 127 // 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 128 // same result for the current trial on every run of this client. Must be
128 // called right after construction. 129 // called right after construction.
129 void UseOneTimeRandomization(); 130 void UseOneTimeRandomization();
130 131
131 // Disables this trial, meaning it always determines the default group 132 // Disables this trial, meaning it always determines the default group
132 // has been selected. May be called immediately after construction, or 133 // has been selected. May be called immediately after construction, or
133 // at any time after initialization (should not be interleaved with 134 // at any time after initialization (should not be interleaved with
134 // AppendGroup calls). Once disabled, there is no way to re-enable a 135 // AppendGroup calls). Once disabled, there is no way to re-enable a
(...skipping 14 matching lines...) Expand all
149 // Note that this will force an instance to participate, and make it illegal 150 // 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. 151 // to attempt to probabilistically add any other groups to the trial.
151 int group(); 152 int group();
152 153
153 // If the group's name is empty, a string version containing the group 154 // If the group's name is empty, a string version containing the group
154 // number is used as the group name. 155 // number is used as the group name.
155 std::string group_name(); 156 std::string group_name();
156 157
157 // Gets the unique identifier of the Field Trial, but only if a group was 158 // 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 159 // 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 160 // is returned. When true is returned, the name and group ids were
160 // set in name_group_id. 161 // successfully set in name_group_id.
161 bool GetNameGroupId(NameGroupId* name_group_id); 162 bool GetNameGroupId(NameGroupId* name_group_id);
162 163
163 // Return the default group name of the FieldTrial. 164 // Return the default group name of the FieldTrial.
164 std::string default_group_name() const { return default_group_name_; } 165 std::string default_group_name() const { return default_group_name_; }
165 166
166 // Helper function for the most common use: as an argument to specify the 167 // 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. 168 // name of a HISTOGRAM. Use the original histogram name as the name_prefix.
168 static std::string MakeName(const std::string& name_prefix, 169 static std::string MakeName(const std::string& name_prefix,
169 const std::string& trial_name); 170 const std::string& trial_name);
170 171
(...skipping 15 matching lines...) Expand all
186 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, HashClientId); 187 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, HashClientId);
187 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, HashClientIdIsUniform); 188 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, HashClientIdIsUniform);
188 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, HashName); 189 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, HashName);
189 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, NameGroupIds); 190 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, NameGroupIds);
190 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, UseOneTimeRandomization); 191 FRIEND_TEST_ALL_PREFIXES(FieldTrialTest, UseOneTimeRandomization);
191 192
192 friend class base::FieldTrialList; 193 friend class base::FieldTrialList;
193 194
194 friend class RefCounted<FieldTrial>; 195 friend class RefCounted<FieldTrial>;
195 196
197 FieldTrial(const std::string& name, Probability total_probability,
198 const std::string& default_group_name, const int year,
199 const int month, const int day_of_month);
196 virtual ~FieldTrial(); 200 virtual ~FieldTrial();
197 201
198 // Returns the group_name. A winner need not have been chosen. 202 // Returns the group_name. A winner need not have been chosen.
199 std::string group_name_internal() const { return group_name_; } 203 std::string group_name_internal() const { return group_name_; }
200 204
201 // Calculates a uniformly-distributed double between [0.0, 1.0) given 205 // 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 206 // 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). 207 // separate one-time randomized trials from all having the same results).
204 static double HashClientId(const std::string& client_id, 208 static double HashClientId(const std::string& client_id,
205 const std::string& trial_name); 209 const std::string& trial_name);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 std::string group_name_; 244 std::string group_name_;
241 245
242 // The hashed name of the group to be sent as a unique identifier. 246 // The hashed name of the group to be sent as a unique identifier.
243 // Is not valid while group_ is equal to kNotFinalized. 247 // Is not valid while group_ is equal to kNotFinalized.
244 uint32 group_name_hash_; 248 uint32 group_name_hash_;
245 249
246 // When enable_field_trial_ is false, field trial reverts to the 'default' 250 // When enable_field_trial_ is false, field trial reverts to the 'default'
247 // group. 251 // group.
248 bool enable_field_trial_; 252 bool enable_field_trial_;
249 253
254 // When forced_ is true, we relaxed some of the checks for double init.
255 bool forced_;
256
250 // When benchmarking is enabled, field trials all revert to the 'default' 257 // When benchmarking is enabled, field trials all revert to the 'default'
251 // group. 258 // group.
252 static bool enable_benchmarking_; 259 static bool enable_benchmarking_;
253 260
254 // This value is reserved for an uninitialized hash value. 261 // This value is reserved for an uninitialized hash value.
255 static const uint32 kReservedHashValue; 262 static const uint32 kReservedHashValue;
256 263
257 DISALLOW_COPY_AND_ASSIGN(FieldTrial); 264 DISALLOW_COPY_AND_ASSIGN(FieldTrial);
258 }; 265 };
259 266
260 //------------------------------------------------------------------------------ 267 //------------------------------------------------------------------------------
261 // Class with a list of all active field trials. A trial is active if it has 268 // 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. 269 // been registered, which includes evaluating its state based on its probaility.
263 // Only one instance of this class exists. 270 // Only one instance of this class exists.
264 class BASE_EXPORT FieldTrialList { 271 class BASE_EXPORT FieldTrialList {
265 public: 272 public:
266 // Define a separator charactor to use when creating a persistent form of an 273 // 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 274 // 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). 275 // second process to mimic our state (i.e., provide the same group name).
269 static const char kPersistentStringSeparator; // Currently a slash. 276 static const char kPersistentStringSeparator; // Currently a slash.
270 277
271 // Define expiration year in future. It is initialized to two years from Now. 278 // Define expiration year in future. It is initialized to two years from Now.
272 static int kExpirationYearInFuture; 279 static int kExpirationYearInFuture;
273 280
274 // Observer is notified when a FieldTrial's group is selected. 281 // Observer is notified when a FieldTrial's group is selected.
275 class Observer { 282 class Observer {
276 public: 283 public:
277 // Notify observers when FieldTrials's group is selected. 284 // Notify observers when FieldTrials's group is selected.
278 virtual void OnFieldTrialGroupFinalized(const std::string& trial_name, 285 virtual void OnFieldTrialGroupFinalized(const std::string& trial_name,
279 const std::string& group_name) = 0; 286 const std::string& group_name) = 0;
280 287
281 protected: 288 protected:
282 virtual ~Observer() {} 289 virtual ~Observer() {}
283 }; 290 };
284 291
285 // This singleton holds the global list of registered FieldTrials. 292 // This singleton holds the global list of registered FieldTrials.
286 // 293 //
287 // |client_id| should be an opaque, diverse ID for this client that does not 294 // |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 295 // change between sessions, to enable one-time randomized trials. The empty
289 // string may be provided, in which case one-time randomized trials will 296 // string may be provided, in which case one-time randomized trials will
290 // not be available. 297 // not be available.
291 explicit FieldTrialList(const std::string& client_id); 298 explicit FieldTrialList(const std::string& client_id);
292 // Destructor Release()'s references to all registered FieldTrial instances. 299 // Destructor Release()'s references to all registered FieldTrial instances.
293 ~FieldTrialList(); 300 ~FieldTrialList();
294 301
295 // Register() stores a pointer to the given trial in a global map.
296 // This method also AddRef's the indicated trial.
297 static void Register(FieldTrial* trial);
298
299 // The Find() method can be used to test to see if a named Trial was already 302 // 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. 303 // registered, or to retrieve a pointer to it from the global map.
301 static FieldTrial* Find(const std::string& name); 304 static FieldTrial* Find(const std::string& name);
302 305
303 // Returns the group number chosen for the named trial, or 306 // Returns the group number chosen for the named trial, or
304 // FieldTrial::kNotFinalized if the trial does not exist. 307 // FieldTrial::kNotFinalized if the trial does not exist.
305 static int FindValue(const std::string& name); 308 static int FindValue(const std::string& name);
306 309
307 // Returns the group name chosen for the named trial, or the 310 // Returns the group name chosen for the named trial, or the
308 // empty string if the trial does not exist. 311 // empty string if the trial does not exist.
309 static std::string FindFullName(const std::string& name); 312 static std::string FindFullName(const std::string& name);
310 313
311 // Returns true if the named trial has been registered. 314 // Returns true if the named trial has been registered.
312 static bool TrialExists(const std::string& name); 315 static bool TrialExists(const std::string& name);
313 316
314 // Create a persistent representation of all FieldTrial instances and the 317 // Creates a persistent representation of all FieldTrial instances for
315 // |client_id()| state for resurrection in another process. This allows 318 // resurrection in another process. This allows randomization to be done in
316 // randomization to be done in one process, and secondary processes can be 319 // one process, and secondary processes can be synchronized on the result.
317 // synchronized on the result. The resulting string contains the 320 // The resulting string contains the name and group name pairs for all trials,
318 // |client_id()|, the names, the trial name, and a "/" separator. 321 // with "/" used to separate all names and to terminate the string. This
322 // string is parsed by CreateTrialsFromString().
319 static void StatesToString(std::string* output); 323 static void StatesToString(std::string* output);
320 324
321 // Returns an array of Unique IDs for each Field Trial that has a chosen 325 // 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 326 // group. Field Trials for which a group has not been chosen yet are NOT
323 // returned in this list. 327 // returned in this list.
324 static void GetFieldTrialNameGroupIds( 328 static void GetFieldTrialNameGroupIds(
325 std::vector<FieldTrial::NameGroupId>* name_group_ids); 329 std::vector<FieldTrial::NameGroupId>* name_group_ids);
326 330
327 // Use a previously generated state string (re: StatesToString()) augment the 331 // 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% 332 // 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 333 // 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 334 // 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 335 // process into this non-browser process, but could also be invoked through a
332 // CreateFieldTrial to create the FieldTrial in the non-browser process. 336 // command line argument to the browser process.
333 // Currently only the group_name_ and name_ are restored, as well as the 337 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 338
338 // Create a FieldTrial with the given |name| and using 100% probability for 339 // Create a FieldTrial with the given |name| and using 100% probability for
339 // the FieldTrial, force FieldTrial to have the same group string as 340 // 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 341 // |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. 342 // 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 343 // Currently only the group_name_ and name_ are set in the FieldTrial. It
343 // returns NULL if there is a FieldTrial that is already registered with the 344 // returns NULL if there is a FieldTrial that is already registered with the
344 // same |name| but has different finalized group string (|group_name|). 345 // same |name| but has different finalized group string (|group_name|).
345 static FieldTrial* CreateFieldTrial(const std::string& name, 346 static FieldTrial* CreateFieldTrial(const std::string& name,
346 const std::string& group_name); 347 const std::string& group_name);
(...skipping 30 matching lines...) Expand all
377 // when constructing the FieldTrialList singleton. 378 // when constructing the FieldTrialList singleton.
378 static bool IsOneTimeRandomizationEnabled(); 379 static bool IsOneTimeRandomizationEnabled();
379 380
380 // Returns an opaque, diverse ID for this client that does not change 381 // Returns an opaque, diverse ID for this client that does not change
381 // between sessions. 382 // between sessions.
382 // 383 //
383 // Returns the empty string if one-time randomization is not enabled. 384 // Returns the empty string if one-time randomization is not enabled.
384 static const std::string& client_id(); 385 static const std::string& client_id();
385 386
386 private: 387 private:
388 friend class base::FieldTrial;
389
387 // A map from FieldTrial names to the actual instances. 390 // A map from FieldTrial names to the actual instances.
388 typedef std::map<std::string, FieldTrial*> RegistrationList; 391 typedef std::map<std::string, FieldTrial*> RegistrationList;
389 392
390 // Helper function should be called only while holding lock_. 393 // Helper function should be called only while holding lock_.
391 FieldTrial* PreLockedFind(const std::string& name); 394 FieldTrial* PreLockedFind(const std::string& name);
392 395
396 // Register() stores a pointer to the given trial in a global map.
397 // This method also AddRef's the indicated trial.
398 // This should only be called by FieldTrial::CreateInstance.
399 static void Register(FieldTrial* trial);
400
393 static FieldTrialList* global_; // The singleton of this class. 401 static FieldTrialList* global_; // The singleton of this class.
394 402
395 // This will tell us if there is an attempt to register a field 403 // This will tell us if there is an attempt to register a field
396 // trial or check if one-time randomization is enabled without 404 // trial or check if one-time randomization is enabled without
397 // creating the FieldTrialList. This is not an error, unless a 405 // creating the FieldTrialList. This is not an error, unless a
398 // FieldTrialList is created after that. 406 // FieldTrialList is created after that.
399 static bool used_without_global_; 407 static bool used_without_global_;
400 408
401 // A helper value made available to users, that shows when the FieldTrialList 409 // 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 410 // was initialized. Note that this is a singleton instance, and hence is a
(...skipping 10 matching lines...) Expand all
413 421
414 // List of observers to be notified when a group is selected for a FieldTrial. 422 // List of observers to be notified when a group is selected for a FieldTrial.
415 ObserverList<Observer> observer_list_; 423 ObserverList<Observer> observer_list_;
416 424
417 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); 425 DISALLOW_COPY_AND_ASSIGN(FieldTrialList);
418 }; 426 };
419 427
420 } // namespace base 428 } // namespace base
421 429
422 #endif // BASE_METRICS_FIELD_TRIAL_H_ 430 #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