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

Side by Side Diff: base/metrics/field_trial.cc

Issue 6883102: Add one-time randomization support for FieldTrial, and the ability to (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Unit test cancel support. Created 9 years, 8 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
OLDNEW
1 // Copyright (c) 2010 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 #include "base/metrics/field_trial.h" 5 #include "base/metrics/field_trial.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/rand_util.h" 8 #include "base/rand_util.h"
9 #include "base/sha1.h"
10 #include "base/string_util.h"
9 #include "base/stringprintf.h" 11 #include "base/stringprintf.h"
10 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
11 13
12 namespace base { 14 namespace base {
13 15
14 // static 16 // static
15 const int FieldTrial::kNotFinalized = -1; 17 const int FieldTrial::kNotFinalized = -1;
16 18
17 // static 19 // static
18 const int FieldTrial::kDefaultGroupNumber = 0; 20 const int FieldTrial::kDefaultGroupNumber = 0;
(...skipping 11 matching lines...) Expand all
30 32
31 FieldTrial::FieldTrial(const std::string& name, 33 FieldTrial::FieldTrial(const std::string& name,
32 const Probability total_probability, 34 const Probability total_probability,
33 const std::string& default_group_name, 35 const std::string& default_group_name,
34 const int year, 36 const int year,
35 const int month, 37 const int month,
36 const int day_of_month) 38 const int day_of_month)
37 : name_(name), 39 : name_(name),
38 divisor_(total_probability), 40 divisor_(total_probability),
39 default_group_name_(default_group_name), 41 default_group_name_(default_group_name),
40 random_(static_cast<Probability>(divisor_ * base::RandDouble())), 42 random_(static_cast<Probability>(divisor_ * RandDouble())),
41 accumulated_group_probability_(0), 43 accumulated_group_probability_(0),
42 next_group_number_(kDefaultGroupNumber+1), 44 next_group_number_(kDefaultGroupNumber+1),
43 group_(kNotFinalized) { 45 group_(kNotFinalized),
46 enable_field_trial_(true) {
44 DCHECK_GT(total_probability, 0); 47 DCHECK_GT(total_probability, 0);
45 DCHECK(!default_group_name_.empty()); 48 DCHECK(!default_group_name_.empty());
46 FieldTrialList::Register(this); 49 FieldTrialList::Register(this);
47 50
48 DCHECK_GT(year, 1970); 51 DCHECK_GT(year, 1970);
49 DCHECK_GT(month, 0); 52 DCHECK_GT(month, 0);
50 DCHECK_LT(month, 13); 53 DCHECK_LT(month, 13);
51 DCHECK_GT(day_of_month, 0); 54 DCHECK_GT(day_of_month, 0);
52 DCHECK_LT(day_of_month, 32); 55 DCHECK_LT(day_of_month, 32);
53 56
54 base::Time::Exploded exploded; 57 Time::Exploded exploded;
55 exploded.year = year; 58 exploded.year = year;
56 exploded.month = month; 59 exploded.month = month;
57 exploded.day_of_week = 0; // Should be unused. 60 exploded.day_of_week = 0; // Should be unused.
58 exploded.day_of_month = day_of_month; 61 exploded.day_of_month = day_of_month;
59 exploded.hour = 0; 62 exploded.hour = 0;
60 exploded.minute = 0; 63 exploded.minute = 0;
61 exploded.second = 0; 64 exploded.second = 0;
62 exploded.millisecond = 0; 65 exploded.millisecond = 0;
63 66
64 base::Time expiration_time = Time::FromLocalExploded(exploded); 67 Time expiration_time = Time::FromLocalExploded(exploded);
65 disable_field_trial_ = (GetBuildTime() > expiration_time) ? true : false; 68 if (GetBuildTime() > expiration_time)
69 Disable();
70 }
71
72 void FieldTrial::UseOneTimeRandomization() {
73 DCHECK_EQ(group_, kNotFinalized);
74 if (!FieldTrialList::IsOneTimeRandomizationEnabled()) {
75 NOTREACHED();
76 Disable();
77 return;
78 }
79
80 random_ = static_cast<Probability>(
81 divisor_ * HashClientId(FieldTrialList::client_id(), name_));
82 }
83
84 void FieldTrial::Disable() {
85 enable_field_trial_ = false;
86
87 // In case we are disabled after initialization, we need to switch
88 // the trial to the default group.
89 if (group_ != kNotFinalized) {
90 group_ = kDefaultGroupNumber;
91 group_name_ = default_group_name_;
92 }
66 } 93 }
67 94
68 int FieldTrial::AppendGroup(const std::string& name, 95 int FieldTrial::AppendGroup(const std::string& name,
69 Probability group_probability) { 96 Probability group_probability) {
70 DCHECK(group_probability <= divisor_); 97 DCHECK(group_probability <= divisor_);
71 DCHECK_GE(group_probability, 0); 98 DCHECK_GE(group_probability, 0);
72 99
73 if (enable_benchmarking_ || disable_field_trial_) 100 if (enable_benchmarking_ || !enable_field_trial_)
74 group_probability = 0; 101 group_probability = 0;
75 102
76 accumulated_group_probability_ += group_probability; 103 accumulated_group_probability_ += group_probability;
77 104
78 DCHECK(accumulated_group_probability_ <= divisor_); 105 DCHECK(accumulated_group_probability_ <= divisor_);
79 if (group_ == kNotFinalized && accumulated_group_probability_ > random_) { 106 if (group_ == kNotFinalized && accumulated_group_probability_ > random_) {
80 // This is the group that crossed the random line, so we do the assignment. 107 // This is the group that crossed the random line, so we do the assignment.
81 group_ = next_group_number_; 108 group_ = next_group_number_;
82 if (name.empty()) 109 if (name.empty())
83 base::StringAppendF(&group_name_, "%d", group_); 110 StringAppendF(&group_name_, "%d", group_);
84 else 111 else
85 group_name_ = name; 112 group_name_ = name;
86 } 113 }
87 return next_group_number_++; 114 return next_group_number_++;
88 } 115 }
89 116
90 int FieldTrial::group() { 117 int FieldTrial::group() {
91 if (group_ == kNotFinalized) { 118 if (group_ == kNotFinalized) {
92 accumulated_group_probability_ = divisor_; 119 accumulated_group_probability_ = divisor_;
93 group_ = kDefaultGroupNumber; 120 group_ = kDefaultGroupNumber;
94 group_name_ = default_group_name_; 121 group_name_ = default_group_name_;
95 } 122 }
96 return group_; 123 return group_;
97 } 124 }
98 125
99 std::string FieldTrial::group_name() { 126 std::string FieldTrial::group_name() {
100 group(); // call group() to make group assignment was done. 127 group(); // call group() to make group assignment was done.
128 DCHECK(!group_name_.empty());
101 return group_name_; 129 return group_name_;
102 } 130 }
103 131
104 // static 132 // static
105 std::string FieldTrial::MakeName(const std::string& name_prefix, 133 std::string FieldTrial::MakeName(const std::string& name_prefix,
106 const std::string& trial_name) { 134 const std::string& trial_name) {
107 std::string big_string(name_prefix); 135 std::string big_string(name_prefix);
108 big_string.append(1, kHistogramFieldTrialSeparator); 136 big_string.append(1, kHistogramFieldTrialSeparator);
109 return big_string.append(FieldTrialList::FindFullName(trial_name)); 137 return big_string.append(FieldTrialList::FindFullName(trial_name));
110 } 138 }
111 139
112 // static 140 // static
113 void FieldTrial::EnableBenchmarking() { 141 void FieldTrial::EnableBenchmarking() {
114 DCHECK_EQ(0u, FieldTrialList::GetFieldTrialCount()); 142 DCHECK_EQ(0u, FieldTrialList::GetFieldTrialCount());
115 enable_benchmarking_ = true; 143 enable_benchmarking_ = true;
116 } 144 }
117 145
118 FieldTrial::~FieldTrial() {} 146 FieldTrial::~FieldTrial() {}
119 147
120 // static 148 // static
121 Time FieldTrial::GetBuildTime() { 149 Time FieldTrial::GetBuildTime() {
122 Time integral_build_time; 150 Time integral_build_time;
123 const char* kDateTime = __DATE__ " " __TIME__; 151 const char* kDateTime = __DATE__ " " __TIME__;
124 bool result = Time::FromString(ASCIIToWide(kDateTime).c_str(), 152 bool result = Time::FromString(ASCIIToWide(kDateTime).c_str(),
125 &integral_build_time); 153 &integral_build_time);
126 DCHECK(result); 154 DCHECK(result);
127 return integral_build_time; 155 return integral_build_time;
128 } 156 }
129 157
158 // static
159 double FieldTrial::HashClientId(const std::string& client_id,
160 const std::string& trial_name) {
161 // SHA-1 is designed to produce a uniformly random spread in its output space,
162 // even for nearly-identical inputs, so it helps massage whatever client_id
163 // and trial_name we get into something with a uniform distribution, which
164 // is desirable so that we don't skew any part of the 0-100% spectrum.
165 std::string input(client_id + trial_name);
166 unsigned char sha1_hash[SHA1_LENGTH];
167 SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()),
168 input.size(),
169 sha1_hash);
170
171 COMPILE_ASSERT(sizeof(uint64) < sizeof(sha1_hash), need_more_data);
172 uint64* bits = reinterpret_cast<uint64*>(&sha1_hash[0]);
173
174 return BitsToRandLikeDouble(*bits);
175 }
176
130 //------------------------------------------------------------------------------ 177 //------------------------------------------------------------------------------
131 // FieldTrialList methods and members. 178 // FieldTrialList methods and members.
132 179
133 // static 180 // static
134 FieldTrialList* FieldTrialList::global_ = NULL; 181 FieldTrialList* FieldTrialList::global_ = NULL;
135 182
136 // static 183 // static
137 bool FieldTrialList::register_without_global_ = false; 184 bool FieldTrialList::register_without_global_ = false;
138 185
139 FieldTrialList::FieldTrialList() : application_start_time_(TimeTicks::Now()) { 186 FieldTrialList::FieldTrialList() : application_start_time_(TimeTicks::Now()) {
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 } 303 }
257 304
258 // static 305 // static
259 size_t FieldTrialList::GetFieldTrialCount() { 306 size_t FieldTrialList::GetFieldTrialCount() {
260 if (!global_) 307 if (!global_)
261 return 0; 308 return 0;
262 AutoLock auto_lock(global_->lock_); 309 AutoLock auto_lock(global_->lock_);
263 return global_->registered_.size(); 310 return global_->registered_.size();
264 } 311 }
265 312
313 // static
314 void FieldTrialList::EnableOneTimeRandomization(const std::string& client_id) {
315 DCHECK(global_);
316 if (!global_)
317 return;
318
319 DCHECK(!global_->IsOneTimeRandomizationEnabled());
320 DCHECK(!client_id.empty());
321
322 global_->client_id_ = client_id;
323 }
324
325 // static
326 bool FieldTrialList::IsOneTimeRandomizationEnabled() {
327 DCHECK(global_);
328 if (!global_)
329 return false;
330
331 return !global_->client_id_.empty();
332 }
333
334 // static
335 const std::string& FieldTrialList::client_id() {
336 DCHECK(global_);
337 if (!global_)
338 return EmptyString();
jar (doing other things) 2011/04/21 22:10:02 If you switch over to using a detectable uninitial
jar (doing other things) 2011/04/29 00:57:11 In fact.... I'd argument that the EmptyString() wo
339
340 return global_->client_id_;
341 }
342
266 FieldTrial* FieldTrialList::PreLockedFind(const std::string& name) { 343 FieldTrial* FieldTrialList::PreLockedFind(const std::string& name) {
267 RegistrationList::iterator it = registered_.find(name); 344 RegistrationList::iterator it = registered_.find(name);
268 if (registered_.end() == it) 345 if (registered_.end() == it)
269 return NULL; 346 return NULL;
270 return it->second; 347 return it->second;
271 } 348 }
272 349
273 } // namespace base 350 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698