OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 |
12 // manages the state of each such experiment (state == which option was | 12 // manages the state of each such experiment (state == which option was |
13 // pseudo-randomly selected). | 13 // pseudo-randomly selected). |
14 // | 14 // |
15 // States are typically generated randomly, either based on a one time | 15 // States are typically generated randomly, either based on a one time |
16 // randomization (generated randomly once, and then persistently reused in the | 16 // randomization (which will yield the same results, in terms of selecting |
17 // client during each future run of the program), or by a startup randomization | 17 // the client for a field trial or not, for every run of the program on a |
18 // given machine, with some exceptions), or by a startup randomization | |
18 // (generated each time the application starts up, but held constant during the | 19 // (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 // 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 // the state can be recalculated again and again, many times during a process). |
21 // Only startup randomization is implemented thus far. | 22 // Continuous randomization is not yet implemented. |
22 | 23 |
23 //------------------------------------------------------------------------------ | 24 //------------------------------------------------------------------------------ |
24 // Example: Suppose we have an experiment involving memory, such as determining | 25 // Example: Suppose we have an experiment involving memory, such as determining |
25 // the impact of some pruning algorithm. | 26 // the impact of some pruning algorithm. |
26 // We assume that we already have a histogram of memory usage, such as: | 27 // We assume that we already have a histogram of memory usage, such as: |
27 | 28 |
28 // HISTOGRAM_COUNTS("Memory.RendererTotal", count); | 29 // HISTOGRAM_COUNTS("Memory.RendererTotal", count); |
29 | 30 |
30 // Somewhere in main thread initialization code, we'd probably define an | 31 // Somewhere in main thread initialization code, we'd probably define an |
31 // instance of a FieldTrial, with code such as: | 32 // instance of a FieldTrial, with code such as: |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 // to assign all the remaining probability to a group ('default'). | 101 // to assign all the remaining probability to a group ('default'). |
101 static const int kDefaultGroupNumber; | 102 static const int kDefaultGroupNumber; |
102 | 103 |
103 // The name is used to register the instance with the FieldTrialList class, | 104 // The name is used to register the instance with the FieldTrialList class, |
104 // and can be used to find the trial (only one trial can be present for each | 105 // and can be used to find the trial (only one trial can be present for each |
105 // name). | 106 // name). |
106 // Group probabilities that are later supplied must sum to less than or equal | 107 // Group probabilities that are later supplied must sum to less than or equal |
107 // to the total_probability. Arguments year, month and day_of_month specify | 108 // to the total_probability. Arguments year, month and day_of_month specify |
108 // the expiration time. If the build time is after the expiration time then | 109 // the expiration time. If the build time is after the expiration time then |
109 // the field trial reverts to the 'default' group. | 110 // the field trial reverts to the 'default' group. |
111 // Using this constructor creates a startup-randomized FieldTrial. If you | |
112 // want a one-time randomized trial, call UseOneTimeRandomization() right | |
113 // after construction. | |
110 FieldTrial(const std::string& name, Probability total_probability, | 114 FieldTrial(const std::string& name, Probability total_probability, |
111 const std::string& default_group_name, const int year, | 115 const std::string& default_group_name, const int year, |
112 const int month, const int day_of_month); | 116 const int month, const int day_of_month); |
113 | 117 |
118 // Changes the field trial to use one-time randomization. Must be called | |
119 // right after construction. |machine_id| must be something that is fairly | |
120 // diverse, related to the current machine, and does not change, or changes | |
121 // very infrequently, while running on the same machine. The reason it must | |
122 // be fairly diverse is that users with the same |machine_id| and the | |
123 // same log-on user name may end up in the same FieldTrial group. | |
124 // | |
125 // A suggested |machine_id| might be the host name, or something fancier | |
126 // such as the primary network adapter's MAC address or the hard disk's | |
127 // serial number. | |
128 // | |
129 // If an empty |machine_id| is given, the field trial will be disabled. | |
130 void UseOneTimeRandomization(const std::string& machine_id); | |
jar (doing other things)
2011/04/21 01:03:50
I'm not convinced you need an argument here, and c
Jói
2011/04/21 19:50:33
Done.
| |
131 | |
132 // Disables this trial, meaning it always determines the default group | |
133 // has been selected. Must be called right after construction. | |
134 void Disable(); | |
jar (doing other things)
2011/04/21 01:03:50
Traditionally, disabling meant we were not part of
Jói
2011/04/21 19:50:33
So I actually took a closer look at this logic. I
| |
135 | |
114 // Establish the name and probability of the next group in this trial. | 136 // Establish the name and probability of the next group in this trial. |
115 // Sometimes, based on construction randomization, this call may cause the | 137 // Sometimes, based on construction randomization, this call may cause the |
116 // provided group to be *THE* group selected for use in this instance. | 138 // provided group to be *THE* group selected for use in this instance. |
117 // The return value is the group number of the new group. | 139 // The return value is the group number of the new group. |
118 int AppendGroup(const std::string& name, Probability group_probability); | 140 int AppendGroup(const std::string& name, Probability group_probability); |
119 | 141 |
120 // Return the name of the FieldTrial (excluding the group name). | 142 // Return the name of the FieldTrial (excluding the group name). |
121 std::string name() const { return name_; } | 143 std::string name() const { return name_; } |
122 | 144 |
123 // Return the randomly selected group number that was assigned. | 145 // Return the randomly selected group number that was assigned. |
124 // Return kDefaultGroupNumber if the instance is in the 'default' group. | 146 // Return kDefaultGroupNumber if the instance is in the 'default' group. |
125 // Note that this will force an instance to participate, and make it illegal | 147 // Note that this will force an instance to participate, and make it illegal |
126 // to attempt to probabalistically add any other groups to the trial. | 148 // to attempt to probabilistically add any other groups to the trial. |
127 int group(); | 149 int group(); |
128 | 150 |
129 // If the field trial is not in an experiment, this returns the empty string. | 151 // If the field trial is not in an experiment, this returns the empty string. |
130 // if the group's name is empty, a name of "_" concatenated with the group | 152 // if the group's name is empty, a name of "_" concatenated with the group |
131 // number is used as the group name. | 153 // number is used as the group name. |
132 std::string group_name(); | 154 std::string group_name(); |
133 | 155 |
134 // Return the default group name of the FieldTrial. | 156 // Return the default group name of the FieldTrial. |
135 std::string default_group_name() const { return default_group_name_; } | 157 std::string default_group_name() const { return default_group_name_; } |
136 | 158 |
137 // Helper function for the most common use: as an argument to specifiy the | 159 // Helper function for the most common use: as an argument to specify the |
138 // name of a HISTOGRAM. Use the original histogram name as the name_prefix. | 160 // name of a HISTOGRAM. Use the original histogram name as the name_prefix. |
139 static std::string MakeName(const std::string& name_prefix, | 161 static std::string MakeName(const std::string& name_prefix, |
140 const std::string& trial_name); | 162 const std::string& trial_name); |
141 | 163 |
142 // Enable benchmarking sets field trials to a common setting. | 164 // Enable benchmarking sets field trials to a common setting. |
143 static void EnableBenchmarking(); | 165 static void EnableBenchmarking(); |
144 | 166 |
145 private: | 167 private: |
146 // Allow tests to access our innards for testing purposes. | 168 // Allow tests to access our innards for testing purposes. |
147 FRIEND_TEST(FieldTrialTest, Registration); | 169 FRIEND_TEST(FieldTrialTest, Registration); |
148 FRIEND_TEST(FieldTrialTest, AbsoluteProbabilities); | 170 FRIEND_TEST(FieldTrialTest, AbsoluteProbabilities); |
149 FRIEND_TEST(FieldTrialTest, RemainingProbability); | 171 FRIEND_TEST(FieldTrialTest, RemainingProbability); |
150 FRIEND_TEST(FieldTrialTest, FiftyFiftyProbability); | 172 FRIEND_TEST(FieldTrialTest, FiftyFiftyProbability); |
151 FRIEND_TEST(FieldTrialTest, MiddleProbabilities); | 173 FRIEND_TEST(FieldTrialTest, MiddleProbabilities); |
152 FRIEND_TEST(FieldTrialTest, OneWinner); | 174 FRIEND_TEST(FieldTrialTest, OneWinner); |
153 FRIEND_TEST(FieldTrialTest, DisableProbability); | 175 FRIEND_TEST(FieldTrialTest, DisableProbability); |
154 FRIEND_TEST(FieldTrialTest, Save); | 176 FRIEND_TEST(FieldTrialTest, Save); |
155 FRIEND_TEST(FieldTrialTest, DuplicateRestore); | 177 FRIEND_TEST(FieldTrialTest, DuplicateRestore); |
156 FRIEND_TEST(FieldTrialTest, MakeName); | 178 FRIEND_TEST(FieldTrialTest, MakeName); |
179 FRIEND_TEST(FieldTrialTest, MachineIdToUniformDouble); | |
180 FRIEND_TEST(FieldTrialTest, ProbabilisticMachineIdToUniformDoubleIsUniform); | |
181 FRIEND_TEST(FieldTrialTest, UseOneTimeRandomization); | |
157 | 182 |
158 friend class base::FieldTrialList; | 183 friend class base::FieldTrialList; |
159 | 184 |
160 friend class RefCounted<FieldTrial>; | 185 friend class RefCounted<FieldTrial>; |
161 | 186 |
162 virtual ~FieldTrial(); | 187 virtual ~FieldTrial(); |
163 | 188 |
164 // Returns the group_name. A winner need not have been chosen. | 189 // Returns the group_name. A winner need not have been chosen. |
165 std::string group_name_internal() const { return group_name_; } | 190 std::string group_name_internal() const { return group_name_; } |
166 | 191 |
167 // Get build time. | 192 // Get build time. |
168 static Time GetBuildTime(); | 193 static Time GetBuildTime(); |
169 | 194 |
195 // Calculates a uniformly-distributed double between [0.0, 1.0) given | |
196 // a |machine_id|. | |
197 static double MachineIdToUniformDouble(const std::string& machine_id); | |
198 | |
170 // The name of the field trial, as can be found via the FieldTrialList. | 199 // The name of the field trial, as can be found via the FieldTrialList. |
171 // This is empty of the trial is not in the experiment. | 200 // This is empty of the trial is not in the experiment. |
172 const std::string name_; | 201 const std::string name_; |
173 | 202 |
174 // The maximum sum of all probabilities supplied, which corresponds to 100%. | 203 // The maximum sum of all probabilities supplied, which corresponds to 100%. |
175 // This is the scaling factor used to adjust supplied probabilities. | 204 // This is the scaling factor used to adjust supplied probabilities. |
176 const Probability divisor_; | 205 const Probability divisor_; |
177 | 206 |
178 // The name of the default group. | 207 // The name of the default group. |
179 const std::string default_group_name_; | 208 const std::string default_group_name_; |
180 | 209 |
181 // The randomly selected probability that is used to select a group (or have | 210 // The randomly selected probability that is used to select a group (or have |
182 // the instance not participate). It is the product of divisor_ and a random | 211 // the instance not participate). It is the product of divisor_ and a random |
183 // number between [0, 1). | 212 // number between [0, 1). |
184 const Probability random_; | 213 Probability random_; |
185 | 214 |
186 // Sum of the probabilities of all appended groups. | 215 // Sum of the probabilities of all appended groups. |
187 Probability accumulated_group_probability_; | 216 Probability accumulated_group_probability_; |
188 | 217 |
189 int next_group_number_; | 218 int next_group_number_; |
190 | 219 |
191 // The pseudo-randomly assigned group number. | 220 // The pseudo-randomly assigned group number. |
192 // This is kNotFinalized if no group has been assigned. | 221 // This is kNotFinalized if no group has been assigned. |
193 int group_; | 222 int group_; |
194 | 223 |
195 // A textual name for the randomly selected group. If this Trial is not a | 224 // A textual name for the randomly selected group. If this Trial is not a |
196 // member of an group, this string is empty. | 225 // member of an group, this string is empty. |
197 std::string group_name_; | 226 std::string group_name_; |
198 | 227 |
199 // When disable_field_trial_ is true, field trial reverts to the 'default' | 228 // When disable_field_trial_ is true, field trial reverts to the 'default' |
200 // group. | 229 // group. |
201 bool disable_field_trial_; | 230 bool disable_field_trial_; |
jar (doing other things)
2011/04/21 01:03:50
My apology for not noticing this in previous revie
Jói
2011/04/21 19:50:33
Fixed this, and also fixed handling of Disable() w
| |
202 | 231 |
203 // When benchmarking is enabled, field trials all revert to the 'default' | 232 // When benchmarking is enabled, field trials all revert to the 'default' |
204 // group. | 233 // group. |
205 static bool enable_benchmarking_; | 234 static bool enable_benchmarking_; |
206 | 235 |
207 DISALLOW_COPY_AND_ASSIGN(FieldTrial); | 236 DISALLOW_COPY_AND_ASSIGN(FieldTrial); |
208 }; | 237 }; |
209 | 238 |
210 //------------------------------------------------------------------------------ | 239 //------------------------------------------------------------------------------ |
211 // Class with a list of all active field trials. A trial is active if it has | 240 // Class with a list of all active field trials. A trial is active if it has |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
288 base::Lock lock_; | 317 base::Lock lock_; |
289 RegistrationList registered_; | 318 RegistrationList registered_; |
290 | 319 |
291 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); | 320 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); |
292 }; | 321 }; |
293 | 322 |
294 } // namespace base | 323 } // namespace base |
295 | 324 |
296 #endif // BASE_METRICS_FIELD_TRIAL_H_ | 325 #endif // BASE_METRICS_FIELD_TRIAL_H_ |
297 | 326 |
OLD | NEW |