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

Side by Side Diff: base/field_trial.h

Issue 28226: Renovate FieldTrial class to better fit with histogram usage. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | base/field_trial.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 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 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 a test to see whether one of two options produces 9 // The simplest example is an experiment to see whether one of two options
10 // "better" results across our user population. In that scenario, UMA data 10 // produces "better" results across our user population. In that scenario, UMA
11 // is uploaded to show the test results, and this class manages the state of 11 // data is uploaded to aggregate the test results, and this FieldTrial class
12 // each such test (state == which option was pseudo-randomly selected). 12 // manages the state of each such experiment (state == which option was
13 // pseudo-randomly selected).
14 //
13 // States are typically generated randomly, either based on a one time 15 // States are typically generated randomly, either based on a one time
14 // randomization (reused during each run of the program), or by a startup 16 // randomization (generated randomly once, and then persitently reused in the
15 // randomization (keeping that tests state constant across a run), or by 17 // client during each future run of the program), or by a startup randomization
16 // continuous randomization across a run. 18 // (generated each time the application starts up, but held constant during the
17 // Only startup randomization is implemented (thus far). 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.
36 // trial = new FieldTrial("MemoryExperiment", 1000);
37 // group1 = trial->AppendGroup("_high_mem", 20); // 2% this _high_mem group.
38 // group2 = trial->AppendGroup("_low_mem", 20); // 2% this _low_mem group.
39 // // Take action depending of which group we randomly land in.
40 // switch (trial->group()) {
41 // case group1:
42 // ... do something
43 // break;
44 // case group2:
45 // ....
46 // break;
47 // default:
48 // ...
49 // }
50
51 // We then modify any histograms we wish to correlate with our experiment to
52 // have slighly different names, depending on what group the trial instance
53 // happened (randomly) to be assigned to:
54
55 // HISTOGRAM_COUNTS(FieldTrial::MakeName("Memory.RendererTotal",
56 // "MemoryExperiment"), count);
57
58 // The above code will create 3 distinct histograms, with each run of the
59 // application being assigned to of of teh three groups, and for each group, the
60 // correspondingly named histogram will be populated:
61
62 // Memory.RendererTotal // 96% of users still fill this histogram.
63 // Memory.RendererTotal_high_mem // 2% of users will fill this histogram.
64 // Memory.RendererTotal_low_mem // 2% of users will fill this histogram.
65
66 //------------------------------------------------------------------------------
18 67
19 #ifndef BASE_FIELD_TRIAL_H_ 68 #ifndef BASE_FIELD_TRIAL_H_
20 #define BASE_FIELD_TRIAL_H_ 69 #define BASE_FIELD_TRIAL_H_
21 70
22 #include <map> 71 #include <map>
23 #include <string> 72 #include <string>
24 73
25 #include "base/non_thread_safe.h" 74 #include "base/non_thread_safe.h"
26 #include "base/ref_counted.h" 75 #include "base/ref_counted.h"
27 #include "base/time.h" 76 #include "base/time.h"
28 77
78
29 class FieldTrial : public base::RefCounted<FieldTrial> { 79 class FieldTrial : public base::RefCounted<FieldTrial> {
30 public: 80 public:
31 // Constructor for a 2-state (boolean) trial. 81 static const int kNotParticipating = -1;
82
83 typedef int Probability; // Use scaled up probability.
84
32 // The name is used to register the instance with the FieldTrialList class, 85 // 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 86 // and can be used to find the trial (only one trial can be present for each
34 // name) using the Find() method. 87 // name).
35 // The probability is a number in the range [0, 1], and is the likliehood that 88 // Group probabilities that are later supplied must sum to less than or equal
36 // the assigned boolean value will be true. 89 // to the total_probability.
37 FieldTrial(const std::wstring& name, double probability); 90 FieldTrial(const std::string& name, Probability total_probability);
38 91
39 // Return the selected boolean value. 92 // Establish the name and probability of the next group in this trial.
40 bool boolean_value() const { return boolean_value_; } 93 // Sometimes, based on construction randomization, this call may causes the
41 std::wstring name() const { return name_; } 94 // provided group to be *THE* group selected for use in this instance.
95 int AppendGroup(const std::string& name, Probability group_probability);
96
97 // Return the name of the FieldTrial (excluding the group name).
98 std::string name() const { return name_; }
99
100 // Return the randomly selected group number that was assigned.
101 // Return kNotParticipating if the instance is not participating in the
102 // experiment.
103 int group() const { return group_; }
104
105 // If the field trial is not in an experiment, this returns the empty string.
106 // if the group's name is empty, a name of "_" concatenated with the group
107 // number is used as the group name.
108 std::string group_name() const { return group_name_; }
109
110 // Helper function for the most common use: as an argument to specifiy the
111 // name of a HISTOGRAM. Use the original histogram name as the name_prefix.
112 static std::string MakeName(const std::string& name_prefix,
113 const std::string& trial_name);
42 114
43 private: 115 private:
44 const std::wstring name_; 116 // The name of the field trial, as can be found via the FieldTrialList.
45 bool boolean_value_; 117 // This is empty of the trial is not in the experiment.
118 const std::string name_;
119
120 // The maximu sum of all probabilities supplied, which corresponds to 100%.
121 // This is the scaling factor used to adjust supplied probabilities.
122 Probability divisor_;
123
124 // The randomly selected probability that is used to select a group (or have
125 // the instance not participate). It is the product of divisor_ and a random
126 // number between [0, 1).
127 Probability random_;
128
129 // Sum of the probabilities of all appended groups.
130 Probability accumulated_group_probability_;
131
132 int next_group_number_;
133
134 // The pseudo-randomly assigned group number.
135 // This is kNotParticipating if no group has been assigned.
136 int group_;
137
138 // A textual name for the randomly selected group, including the Trial name.
139 // If this Trial is not a member of an group, this string is empty.
140 std::string group_name_;
46 141
47 DISALLOW_COPY_AND_ASSIGN(FieldTrial); 142 DISALLOW_COPY_AND_ASSIGN(FieldTrial);
48 }; 143 };
49 144
145 //------------------------------------------------------------------------------
50 // Class with a list of all active field trials. A trial is active if it has 146 // 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. 147 // been registered, which includes evaluating its state based on its probaility.
52 // Only one instance of this class exists. 148 // Only one instance of this class exists.
53 class FieldTrialList : NonThreadSafe { 149 class FieldTrialList : NonThreadSafe {
54 public: 150 public:
55 // This singleton holds the global list of registered FieldTrials. 151 // This singleton holds the global list of registered FieldTrials.
56 FieldTrialList(); 152 FieldTrialList();
57 // Destructor Release()'s references to all registered FieldTrial instances. 153 // Destructor Release()'s references to all registered FieldTrial instances.
58 ~FieldTrialList(); 154 ~FieldTrialList();
59 155
60 // Register() stores a pointer to the given trial in a global map. 156 // Register() stores a pointer to the given trial in a global map.
61 // This method also AddRef's the indicated trial. 157 // This method also AddRef's the indicated trial.
62 static void Register(FieldTrial* trial); 158 static void Register(FieldTrial* trial);
63 159
64 // The Find() method can be used to test to see if a named Trial was already 160 // 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. 161 // registered, or to retrieve a pointer to it from the global map.
66 static FieldTrial* Find(const std::wstring& name); 162 static FieldTrial* Find(const std::string& name);
163
164 static int FindValue(const std::string& name);
165
166 static std::string FindFullName(const std::string& name);
67 167
68 // The time of construction of the global map is recorded in a static variable 168 // 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 169 // 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 170 // of the application. In some experiments it may be useful to discount
71 // data that is gathered before the application has reach sufficient 171 // data that is gathered before the application has reached sufficient
72 // stability (example: most DLL have loaded, etc.) 172 // stability (example: most DLL have loaded, etc.)
73 static base::Time application_start_time() { 173 static base::Time application_start_time() {
74 if (global_) 174 if (global_)
75 return global_->application_start_time_; 175 return global_->application_start_time_;
76 // For testing purposes only, or when we don't yet have a start time. 176 // For testing purposes only, or when we don't yet have a start time.
77 return base::Time::Now(); 177 return base::Time::Now();
78 } 178 }
79 179
80 private: 180 private:
81 typedef std::map<std::wstring, FieldTrial*> RegistrationList; 181 typedef std::map<std::string, FieldTrial*> RegistrationList;
82 182
83 static FieldTrialList* global_; // The singleton of this class. 183 static FieldTrialList* global_; // The singleton of this class.
84 184
85 base::Time application_start_time_; 185 base::Time application_start_time_;
86 RegistrationList registered_; 186 RegistrationList registered_;
87 187
88 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); 188 DISALLOW_COPY_AND_ASSIGN(FieldTrialList);
89 }; 189 };
90 190
91 #endif // BASE_FIELD_TRIAL_H_ 191 #endif // BASE_FIELD_TRIAL_H_
OLDNEW
« no previous file with comments | « no previous file | base/field_trial.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698