[email protected] | ac262c9f | 2008-10-19 17:45:21 | [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 | // |
[email protected] | 9660b97 | 2009-03-02 19:02:56 | [diff] [blame] | 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 |
| 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 |
| 13 | // pseudo-randomly selected). |
| 14 | // |
[email protected] | ac262c9f | 2008-10-19 17:45:21 | [diff] [blame] | 15 | // States are typically generated randomly, either based on a one time |
[email protected] | 510e854f | 2009-04-20 18:39:08 | [diff] [blame^] | 16 | // randomization (generated randomly once, and then persistently reused in the |
[email protected] | 9660b97 | 2009-03-02 19:02:56 | [diff] [blame] | 17 | // client during each future run of the program), or by a startup randomization |
| 18 | // (generated each time the application starts up, but held constant during the |
| 19 | // duration of the process), or by continuous randomization across a run (where |
| 20 | // the state can be recalculated again and again, many times during a process). |
| 21 | // Only startup randomization is implemented thus far. |
| 22 | |
| 23 | //------------------------------------------------------------------------------ |
| 24 | // Example: Suppose we have an experiment involving memory, such as determining |
| 25 | // the impact of memory model command line flags actual memory use. |
| 26 | // We assume that we already have a histogram of memory usage, such as: |
| 27 | |
| 28 | // HISTOGRAM_COUNTS("Memory.RendererTotal", count); |
| 29 | |
| 30 | // Somewhere in main thread initialization code, we'd probably define an |
| 31 | // instance of a FieldTrial, with code such as: |
| 32 | |
| 33 | // // Note, FieldTrials are reference counted, and persist automagically until |
| 34 | // // process teardown, courtesy of their automatic registration in |
| 35 | // // FieldTrialList. |
[email protected] | eab00b0 | 2009-03-02 22:50:07 | [diff] [blame] | 36 | // scoped_refptr<FieldTrial> trial = new FieldTrial("MemoryExperiment", 1000); |
[email protected] | 1ea501f | 2009-03-03 01:30:51 | [diff] [blame] | 37 | // int group1 = trial->AppendGroup("_high_mem", 20); // 2% in _high_mem group. |
| 38 | // int group2 = trial->AppendGroup("_low_mem", 20); // 2% in _low_mem group. |
[email protected] | 9660b97 | 2009-03-02 19:02:56 | [diff] [blame] | 39 | // // Take action depending of which group we randomly land in. |
[email protected] | eab00b0 | 2009-03-02 22:50:07 | [diff] [blame] | 40 | // if (trial->group() == group1) |
| 41 | // SetMemoryModel(HIGH); // Sample setting of browser state. |
| 42 | // else if (trial->group() == group2) |
| 43 | // SetMemoryModel(LOW); // Sample alternate setting. |
[email protected] | 9660b97 | 2009-03-02 19:02:56 | [diff] [blame] | 44 | |
| 45 | // We then modify any histograms we wish to correlate with our experiment to |
| 46 | // have slighly different names, depending on what group the trial instance |
| 47 | // happened (randomly) to be assigned to: |
| 48 | |
| 49 | // HISTOGRAM_COUNTS(FieldTrial::MakeName("Memory.RendererTotal", |
[email protected] | 0b48db4 | 2009-03-23 02:45:11 | [diff] [blame] | 50 | // "MemoryExperiment").data(), count); |
[email protected] | 9660b97 | 2009-03-02 19:02:56 | [diff] [blame] | 51 | |
| 52 | // The above code will create 3 distinct histograms, with each run of the |
| 53 | // application being assigned to of of teh three groups, and for each group, the |
| 54 | // correspondingly named histogram will be populated: |
| 55 | |
| 56 | // Memory.RendererTotal // 96% of users still fill this histogram. |
| 57 | // Memory.RendererTotal_high_mem // 2% of users will fill this histogram. |
| 58 | // Memory.RendererTotal_low_mem // 2% of users will fill this histogram. |
| 59 | |
| 60 | //------------------------------------------------------------------------------ |
[email protected] | ac262c9f | 2008-10-19 17:45:21 | [diff] [blame] | 61 | |
| 62 | #ifndef BASE_FIELD_TRIAL_H_ |
| 63 | #define BASE_FIELD_TRIAL_H_ |
| 64 | |
| 65 | #include <map> |
| 66 | #include <string> |
| 67 | |
[email protected] | 0b48db4 | 2009-03-23 02:45:11 | [diff] [blame] | 68 | #include "base/lock.h" |
[email protected] | ac262c9f | 2008-10-19 17:45:21 | [diff] [blame] | 69 | #include "base/non_thread_safe.h" |
| 70 | #include "base/ref_counted.h" |
| 71 | #include "base/time.h" |
| 72 | |
[email protected] | 9660b97 | 2009-03-02 19:02:56 | [diff] [blame] | 73 | |
[email protected] | ac262c9f | 2008-10-19 17:45:21 | [diff] [blame] | 74 | class FieldTrial : public base::RefCounted<FieldTrial> { |
| 75 | public: |
[email protected] | e84387f | 2009-03-02 19:21:07 | [diff] [blame] | 76 | static const int kNotParticipating; |
[email protected] | 9660b97 | 2009-03-02 19:02:56 | [diff] [blame] | 77 | |
| 78 | typedef int Probability; // Use scaled up probability. |
| 79 | |
[email protected] | ac262c9f | 2008-10-19 17:45:21 | [diff] [blame] | 80 | // The name is used to register the instance with the FieldTrialList class, |
| 81 | // and can be used to find the trial (only one trial can be present for each |
[email protected] | 9660b97 | 2009-03-02 19:02:56 | [diff] [blame] | 82 | // name). |
| 83 | // Group probabilities that are later supplied must sum to less than or equal |
| 84 | // to the total_probability. |
| 85 | FieldTrial(const std::string& name, Probability total_probability); |
[email protected] | ac262c9f | 2008-10-19 17:45:21 | [diff] [blame] | 86 | |
[email protected] | 9660b97 | 2009-03-02 19:02:56 | [diff] [blame] | 87 | // Establish the name and probability of the next group in this trial. |
| 88 | // Sometimes, based on construction randomization, this call may causes the |
| 89 | // provided group to be *THE* group selected for use in this instance. |
| 90 | int AppendGroup(const std::string& name, Probability group_probability); |
| 91 | |
| 92 | // Return the name of the FieldTrial (excluding the group name). |
| 93 | std::string name() const { return name_; } |
| 94 | |
| 95 | // Return the randomly selected group number that was assigned. |
| 96 | // Return kNotParticipating if the instance is not participating in the |
| 97 | // experiment. |
| 98 | int group() const { return group_; } |
| 99 | |
| 100 | // If the field trial is not in an experiment, this returns the empty string. |
| 101 | // if the group's name is empty, a name of "_" concatenated with the group |
| 102 | // number is used as the group name. |
| 103 | std::string group_name() const { return group_name_; } |
| 104 | |
| 105 | // Helper function for the most common use: as an argument to specifiy the |
| 106 | // name of a HISTOGRAM. Use the original histogram name as the name_prefix. |
| 107 | static std::string MakeName(const std::string& name_prefix, |
| 108 | const std::string& trial_name); |
[email protected] | ac262c9f | 2008-10-19 17:45:21 | [diff] [blame] | 109 | |
| 110 | private: |
[email protected] | 9660b97 | 2009-03-02 19:02:56 | [diff] [blame] | 111 | // The name of the field trial, as can be found via the FieldTrialList. |
| 112 | // This is empty of the trial is not in the experiment. |
| 113 | const std::string name_; |
| 114 | |
| 115 | // The maximu sum of all probabilities supplied, which corresponds to 100%. |
| 116 | // This is the scaling factor used to adjust supplied probabilities. |
| 117 | Probability divisor_; |
| 118 | |
| 119 | // The randomly selected probability that is used to select a group (or have |
| 120 | // the instance not participate). It is the product of divisor_ and a random |
| 121 | // number between [0, 1). |
| 122 | Probability random_; |
| 123 | |
| 124 | // Sum of the probabilities of all appended groups. |
| 125 | Probability accumulated_group_probability_; |
| 126 | |
| 127 | int next_group_number_; |
| 128 | |
| 129 | // The pseudo-randomly assigned group number. |
| 130 | // This is kNotParticipating if no group has been assigned. |
| 131 | int group_; |
| 132 | |
| 133 | // A textual name for the randomly selected group, including the Trial name. |
| 134 | // If this Trial is not a member of an group, this string is empty. |
| 135 | std::string group_name_; |
[email protected] | ac262c9f | 2008-10-19 17:45:21 | [diff] [blame] | 136 | |
| 137 | DISALLOW_COPY_AND_ASSIGN(FieldTrial); |
| 138 | }; |
| 139 | |
[email protected] | 9660b97 | 2009-03-02 19:02:56 | [diff] [blame] | 140 | //------------------------------------------------------------------------------ |
[email protected] | ac262c9f | 2008-10-19 17:45:21 | [diff] [blame] | 141 | // Class with a list of all active field trials. A trial is active if it has |
| 142 | // been registered, which includes evaluating its state based on its probaility. |
| 143 | // Only one instance of this class exists. |
[email protected] | 0b48db4 | 2009-03-23 02:45:11 | [diff] [blame] | 144 | class FieldTrialList { |
[email protected] | ac262c9f | 2008-10-19 17:45:21 | [diff] [blame] | 145 | public: |
| 146 | // This singleton holds the global list of registered FieldTrials. |
| 147 | FieldTrialList(); |
| 148 | // Destructor Release()'s references to all registered FieldTrial instances. |
| 149 | ~FieldTrialList(); |
| 150 | |
| 151 | // Register() stores a pointer to the given trial in a global map. |
| 152 | // This method also AddRef's the indicated trial. |
| 153 | static void Register(FieldTrial* trial); |
| 154 | |
| 155 | // The Find() method can be used to test to see if a named Trial was already |
| 156 | // registered, or to retrieve a pointer to it from the global map. |
[email protected] | 9660b97 | 2009-03-02 19:02:56 | [diff] [blame] | 157 | static FieldTrial* Find(const std::string& name); |
| 158 | |
| 159 | static int FindValue(const std::string& name); |
| 160 | |
| 161 | static std::string FindFullName(const std::string& name); |
[email protected] | ac262c9f | 2008-10-19 17:45:21 | [diff] [blame] | 162 | |
| 163 | // The time of construction of the global map is recorded in a static variable |
| 164 | // and is commonly used by experiments to identify the time since the start |
| 165 | // of the application. In some experiments it may be useful to discount |
[email protected] | 9660b97 | 2009-03-02 19:02:56 | [diff] [blame] | 166 | // data that is gathered before the application has reached sufficient |
[email protected] | ac262c9f | 2008-10-19 17:45:21 | [diff] [blame] | 167 | // stability (example: most DLL have loaded, etc.) |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 168 | static base::Time application_start_time() { |
[email protected] | 423041b | 2008-10-27 17:39:28 | [diff] [blame] | 169 | if (global_) |
| 170 | return global_->application_start_time_; |
| 171 | // For testing purposes only, or when we don't yet have a start time. |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 172 | return base::Time::Now(); |
[email protected] | ac262c9f | 2008-10-19 17:45:21 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | private: |
[email protected] | 0b48db4 | 2009-03-23 02:45:11 | [diff] [blame] | 176 | // Helper function should be called only while holding lock_. |
| 177 | FieldTrial* PreLockedFind(const std::string& name); |
| 178 | |
[email protected] | 9660b97 | 2009-03-02 19:02:56 | [diff] [blame] | 179 | typedef std::map<std::string, FieldTrial*> RegistrationList; |
[email protected] | ac262c9f | 2008-10-19 17:45:21 | [diff] [blame] | 180 | |
[email protected] | ac262c9f | 2008-10-19 17:45:21 | [diff] [blame] | 181 | static FieldTrialList* global_; // The singleton of this class. |
[email protected] | ac262c9f | 2008-10-19 17:45:21 | [diff] [blame] | 182 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 183 | base::Time application_start_time_; |
[email protected] | 0b48db4 | 2009-03-23 02:45:11 | [diff] [blame] | 184 | |
| 185 | // Lock for access to registered_. |
| 186 | Lock lock_; |
[email protected] | ac262c9f | 2008-10-19 17:45:21 | [diff] [blame] | 187 | RegistrationList registered_; |
| 188 | |
| 189 | DISALLOW_COPY_AND_ASSIGN(FieldTrialList); |
| 190 | }; |
| 191 | |
| 192 | #endif // BASE_FIELD_TRIAL_H_ |