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 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 | 59 |
60 //------------------------------------------------------------------------------ | 60 //------------------------------------------------------------------------------ |
61 | 61 |
62 #ifndef BASE_METRICS_FIELD_TRIAL_H_ | 62 #ifndef BASE_METRICS_FIELD_TRIAL_H_ |
63 #define BASE_METRICS_FIELD_TRIAL_H_ | 63 #define BASE_METRICS_FIELD_TRIAL_H_ |
64 #pragma once | 64 #pragma once |
65 | 65 |
66 #include <map> | 66 #include <map> |
67 #include <string> | 67 #include <string> |
68 | 68 |
| 69 #include "base/gtest_prod_util.h" |
69 #include "base/lock.h" | 70 #include "base/lock.h" |
70 #include "base/ref_counted.h" | 71 #include "base/ref_counted.h" |
71 #include "base/time.h" | 72 #include "base/time.h" |
72 | 73 |
73 namespace base { | 74 namespace base { |
74 | 75 |
75 class FieldTrial : public RefCounted<FieldTrial> { | 76 class FieldTrial : public RefCounted<FieldTrial> { |
76 public: | 77 public: |
77 typedef int Probability; // Probability type for being selected in a trial. | 78 typedef int Probability; // Probability type for being selected in a trial. |
78 | 79 |
79 // A return value to indicate that a given instance has not yet had a group | 80 // A return value to indicate that a given instance has not yet had a group |
80 // assignment (and hence is not yet participating in the trial). | 81 // assignment (and hence group assignment is not yet finalized in the trial). |
81 static const int kNotParticipating; | 82 static const int kNotFinalized; |
82 | 83 |
83 // Provide an easy way to assign all remaining probability to a group. Note | 84 // Every field trial is reserved a kDefaultGroupNumber. If a group assigement |
84 // that this will force an instance to participate, and make it illegal to | 85 // is not done, when group() is called, all the remaining probability is |
85 // attempt to probabalistically add any other groups to the trial. When doing | 86 // assigned to a group and its number is kDefaultGroupNumber. |
86 // A/B tests with timings, it is often best to define all groups, so that | 87 // When doing A/B tests with timings, it is often best to define all groups, |
87 // histograms will get unique names via the MakeName() methods. | 88 // so that histograms will get unique names via the MakeName() methods. |
88 static const Probability kAllRemainingProbability; | 89 static const int kDefaultGroupNumber; |
89 | 90 |
90 // The name is used to register the instance with the FieldTrialList class, | 91 // The name is used to register the instance with the FieldTrialList class, |
91 // and can be used to find the trial (only one trial can be present for each | 92 // and can be used to find the trial (only one trial can be present for each |
92 // name). | 93 // name). |
93 // Group probabilities that are later supplied must sum to less than or equal | 94 // Group probabilities that are later supplied must sum to less than or equal |
94 // to the total_probability. | 95 // to the total_probability. |
95 FieldTrial(const std::string& name, Probability total_probability); | 96 FieldTrial(const std::string& name, Probability total_probability, |
| 97 const std::string& default_group_name, const int year, |
| 98 const int month, const int day_of_month); |
96 | 99 |
97 // Establish the name and probability of the next group in this trial. | 100 // Establish the name and probability of the next group in this trial. |
98 // Sometimes, based on construction randomization, this call may causes the | 101 // Sometimes, based on construction randomization, this call may causes the |
99 // provided group to be *THE* group selected for use in this instance. | 102 // provided group to be *THE* group selected for use in this instance. |
100 int AppendGroup(const std::string& name, Probability group_probability); | 103 int AppendGroup(const std::string& name, Probability group_probability); |
101 | 104 |
102 // Return the name of the FieldTrial (excluding the group name). | 105 // Return the name of the FieldTrial (excluding the group name). |
103 std::string name() const { return name_; } | 106 std::string name() const { return name_; } |
104 | 107 |
105 // Return the randomly selected group number that was assigned. | 108 // Return the randomly selected group number that was assigned. |
106 // Return kNotParticipating if the instance is not participating in the | 109 // Return kDefaultGroupNumber if the instance is in the default group. This |
107 // experiment. | 110 // call finalizes the FieldTrial and group assignment is completed when this |
108 int group() const { return group_; } | 111 // method is called. |
| 112 int group(); |
109 | 113 |
110 // If the field trial is not in an experiment, this returns the empty string. | 114 // If the field trial is not in an experiment, this returns the empty string. |
111 // if the group's name is empty, a name of "_" concatenated with the group | 115 // if the group's name is empty, a name of "_" concatenated with the group |
112 // number is used as the group name. | 116 // number is used as the group name. |
113 std::string group_name() const { return group_name_; } | 117 std::string group_name() const { return group_name_; } |
114 | 118 |
115 // Helper function for the most common use: as an argument to specifiy the | 119 // Helper function for the most common use: as an argument to specifiy the |
116 // name of a HISTOGRAM. Use the original histogram name as the name_prefix. | 120 // name of a HISTOGRAM. Use the original histogram name as the name_prefix. |
117 static std::string MakeName(const std::string& name_prefix, | 121 static std::string MakeName(const std::string& name_prefix, |
118 const std::string& trial_name); | 122 const std::string& trial_name); |
119 | 123 |
120 // Enable benchmarking sets field trials to a common setting. | 124 // Enable benchmarking sets field trials to a common setting. |
121 static void EnableBenchmarking(); | 125 static void EnableBenchmarking(); |
122 | 126 |
123 private: | 127 private: |
| 128 // Allow tests to access our innards for testing purposes. |
| 129 FRIEND_TEST(FieldTrialTest, Registration); |
| 130 FRIEND_TEST(FieldTrialTest, AbsoluteProbabilities); |
| 131 FRIEND_TEST(FieldTrialTest, RemainingProbability); |
| 132 FRIEND_TEST(FieldTrialTest, FiftyFiftyProbability); |
| 133 FRIEND_TEST(FieldTrialTest, MiddleProbabilities); |
| 134 FRIEND_TEST(FieldTrialTest, OneWinner); |
| 135 FRIEND_TEST(FieldTrialTest, DisableProbability); |
| 136 FRIEND_TEST(FieldTrialTest, Save); |
| 137 FRIEND_TEST(FieldTrialTest, DuplicateRestore); |
| 138 FRIEND_TEST(FieldTrialTest, MakeName); |
| 139 |
124 friend class RefCounted<FieldTrial>; | 140 friend class RefCounted<FieldTrial>; |
125 | 141 |
| 142 // Create a Field Trial that runs forever (expires in 12/31/2099). |
| 143 FieldTrial(const std::string& name, Probability total_probability, |
| 144 const std::string& default_group_name); |
| 145 |
| 146 // Determine if the field trial is to be disabled or not based on |
| 147 // expiration time (year, month, day_of_month) and build time of the module. |
| 148 // This method sets disable_field_trial_ to true, if the build time is after |
| 149 // expiration_time_. If disable_field_trial_ is true then the field trial |
| 150 // reverts to the 'default' bucket. |
| 151 void Initialize(const int year, const int month, const int day_of_month); |
| 152 |
126 virtual ~FieldTrial(); | 153 virtual ~FieldTrial(); |
127 | 154 |
| 155 // Get build time. |
| 156 static Time GetBuildTime(); |
| 157 |
128 // The name of the field trial, as can be found via the FieldTrialList. | 158 // The name of the field trial, as can be found via the FieldTrialList. |
129 // This is empty of the trial is not in the experiment. | 159 // This is empty of the trial is not in the experiment. |
130 const std::string name_; | 160 const std::string name_; |
131 | 161 |
132 // The maximum sum of all probabilities supplied, which corresponds to 100%. | 162 // The maximum sum of all probabilities supplied, which corresponds to 100%. |
133 // This is the scaling factor used to adjust supplied probabilities. | 163 // This is the scaling factor used to adjust supplied probabilities. |
134 Probability divisor_; | 164 Probability divisor_; |
135 | 165 |
| 166 // The name of the default group. |
| 167 const std::string default_group_name_; |
| 168 |
136 // The randomly selected probability that is used to select a group (or have | 169 // The randomly selected probability that is used to select a group (or have |
137 // the instance not participate). It is the product of divisor_ and a random | 170 // the instance not participate). It is the product of divisor_ and a random |
138 // number between [0, 1). | 171 // number between [0, 1). |
139 Probability random_; | 172 Probability random_; |
140 | 173 |
141 // Sum of the probabilities of all appended groups. | 174 // Sum of the probabilities of all appended groups. |
142 Probability accumulated_group_probability_; | 175 Probability accumulated_group_probability_; |
143 | 176 |
144 int next_group_number_; | 177 int next_group_number_; |
145 | 178 |
146 // The pseudo-randomly assigned group number. | 179 // The pseudo-randomly assigned group number. |
147 // This is kNotParticipating if no group has been assigned. | 180 // This is kNotFinalized if no group has been assigned. |
148 int group_; | 181 int group_; |
149 | 182 |
150 // A textual name for the randomly selected group, including the Trial name. | 183 // A textual name for the randomly selected group, including the Trial name. |
151 // If this Trial is not a member of an group, this string is empty. | 184 // If this Trial is not a member of an group, this string is empty. |
152 std::string group_name_; | 185 std::string group_name_; |
153 | 186 |
| 187 // The default value (all remaining probabilities) can be selected after the |
| 188 // expiration_time, which is based on the build time of the module. |
| 189 base::Time expiration_time_; |
| 190 |
| 191 // When disable_field_trial_ is true, field trial reverts to the 'default' |
| 192 // bucket. |
| 193 bool disable_field_trial_; |
| 194 |
154 // When benchmarking is enabled, field trials all revert to the 'default' | 195 // When benchmarking is enabled, field trials all revert to the 'default' |
155 // bucket. | 196 // bucket. |
156 static bool enable_benchmarking_; | 197 static bool enable_benchmarking_; |
157 | 198 |
158 DISALLOW_COPY_AND_ASSIGN(FieldTrial); | 199 DISALLOW_COPY_AND_ASSIGN(FieldTrial); |
159 }; | 200 }; |
160 | 201 |
161 //------------------------------------------------------------------------------ | 202 //------------------------------------------------------------------------------ |
162 // Class with a list of all active field trials. A trial is active if it has | 203 // Class with a list of all active field trials. A trial is active if it has |
163 // been registered, which includes evaluating its state based on its probaility. | 204 // been registered, which includes evaluating its state based on its probaility. |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 Lock lock_; | 280 Lock lock_; |
240 RegistrationList registered_; | 281 RegistrationList registered_; |
241 | 282 |
242 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); | 283 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); |
243 }; | 284 }; |
244 | 285 |
245 } // namespace base | 286 } // namespace base |
246 | 287 |
247 #endif // BASE_METRICS_FIELD_TRIAL_H_ | 288 #endif // BASE_METRICS_FIELD_TRIAL_H_ |
248 | 289 |
OLD | NEW |