jar@google.com | 87df361 | 2008-10-20 02:45:21 +0900 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 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). |
| 7 | // All code is called exclusively on the UI thread currently. |
| 8 | // |
| 9 | // The simplest example is a test to see whether one of two options produces |
| 10 | // "better" results across our user population. In that scenario, UMA data |
| 11 | // is uploaded to show the test results, and this class manages the state of |
| 12 | // each such test (state == which option was pseudo-randomly selected). |
| 13 | // States are typically generated randomly, either based on a one time |
| 14 | // randomization (reused during each run of the program), or by a startup |
| 15 | // randomization (keeping that tests state constant across a run), or by |
| 16 | // continuous randomization across a run. |
| 17 | // Only startup randomization is implemented (thus far). |
| 18 | |
| 19 | #ifndef BASE_FIELD_TRIAL_H_ |
| 20 | #define BASE_FIELD_TRIAL_H_ |
| 21 | |
| 22 | #include <map> |
| 23 | #include <string> |
| 24 | |
| 25 | #include "base/non_thread_safe.h" |
| 26 | #include "base/ref_counted.h" |
| 27 | #include "base/time.h" |
| 28 | |
| 29 | class FieldTrial : public base::RefCounted<FieldTrial> { |
| 30 | public: |
| 31 | // Constructor for a 2-state (boolean) trial. |
| 32 | // The name is used to register the instance with the FieldTrialList class, |
| 33 | // and can be used to find the trial (only one trial can be present for each |
| 34 | // name) using the Find() method. |
| 35 | // The probability is a number in the range [0, 1], and is the likliehood that |
| 36 | // the assigned boolean value will be true. |
| 37 | FieldTrial(const std::wstring& name, double probability); |
| 38 | |
| 39 | // Return the selected boolean value. |
| 40 | bool boolean_value() const { return boolean_value_; } |
| 41 | std::wstring name() const { return name_; } |
| 42 | |
| 43 | private: |
| 44 | const std::wstring name_; |
| 45 | bool boolean_value_; |
| 46 | |
| 47 | DISALLOW_COPY_AND_ASSIGN(FieldTrial); |
| 48 | }; |
| 49 | |
| 50 | // Class with a list of all active field trials. A trial is active if it has |
| 51 | // been registered, which includes evaluating its state based on its probaility. |
| 52 | // Only one instance of this class exists. |
| 53 | class FieldTrialList : NonThreadSafe { |
| 54 | public: |
| 55 | // This singleton holds the global list of registered FieldTrials. |
| 56 | FieldTrialList(); |
| 57 | // Destructor Release()'s references to all registered FieldTrial instances. |
| 58 | ~FieldTrialList(); |
| 59 | |
| 60 | // Register() stores a pointer to the given trial in a global map. |
| 61 | // This method also AddRef's the indicated trial. |
| 62 | static void Register(FieldTrial* trial); |
| 63 | |
| 64 | // The Find() method can be used to test to see if a named Trial was already |
| 65 | // registered, or to retrieve a pointer to it from the global map. |
| 66 | static FieldTrial* Find(const std::wstring& name); |
| 67 | |
| 68 | // The time of construction of the global map is recorded in a static variable |
| 69 | // and is commonly used by experiments to identify the time since the start |
| 70 | // of the application. In some experiments it may be useful to discount |
| 71 | // data that is gathered before the application has reach sufficient |
| 72 | // stability (example: most DLL have loaded, etc.) |
| 73 | static Time application_start_time() { |
| 74 | return global_->application_start_time_; |
| 75 | } |
| 76 | |
| 77 | private: |
| 78 | typedef std::map<std::wstring, FieldTrial*> RegistrationList; |
| 79 | |
| 80 | friend class FieldTrialTest; |
| 81 | static void ResetConstructorCountForTestingOnly() { constructor_count_ = 0; } |
| 82 | |
| 83 | static FieldTrialList* global_; // The singleton of this class. |
| 84 | static int constructor_count_; // Prevent having more than one. |
| 85 | |
| 86 | Time application_start_time_; |
| 87 | RegistrationList registered_; |
| 88 | |
| 89 | DISALLOW_COPY_AND_ASSIGN(FieldTrialList); |
| 90 | }; |
| 91 | |
| 92 | #endif // BASE_FIELD_TRIAL_H_ |