OLD | NEW |
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 | 5 |
6 #include "base/field_trial.h" | 6 #include "base/field_trial.h" |
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/string_util.h" |
9 | 10 |
10 using base::Time; | 11 using base::Time; |
11 | 12 |
12 //------------------------------------------------------------------------------ | 13 //------------------------------------------------------------------------------ |
| 14 // FieldTrial methods and members. |
| 15 |
| 16 FieldTrial::FieldTrial(const std::string& name, |
| 17 const Probability total_probability) |
| 18 : name_(name), |
| 19 divisor_(total_probability), |
| 20 random_(static_cast<Probability>(divisor_ * base::RandDouble())), |
| 21 accumulated_group_probability_(0), |
| 22 next_group_number_(0), |
| 23 group_(kNotParticipating) { |
| 24 FieldTrialList::Register(this); |
| 25 } |
| 26 |
| 27 int FieldTrial::AppendGroup(const std::string& name, |
| 28 Probability group_probability) { |
| 29 DCHECK(group_probability <= divisor_); |
| 30 accumulated_group_probability_ += group_probability; |
| 31 DCHECK(accumulated_group_probability_ <= divisor_); |
| 32 if (group_ == kNotParticipating && accumulated_group_probability_ > random_) { |
| 33 // This is the group that crossed the random line, so we do teh assignment. |
| 34 group_ = next_group_number_; |
| 35 if (name.empty()) |
| 36 StringAppendF(&group_name_, "_%d", group_); |
| 37 else |
| 38 group_name_ = name; |
| 39 } |
| 40 return next_group_number_++; |
| 41 } |
| 42 |
| 43 // static |
| 44 std::string FieldTrial::MakeName(const std::string& name_prefix, |
| 45 const std::string& trial_name) { |
| 46 std::string big_string(name_prefix); |
| 47 return big_string.append(FieldTrialList::FindFullName(trial_name)); |
| 48 } |
| 49 |
| 50 //------------------------------------------------------------------------------ |
13 // FieldTrialList methods and members. | 51 // FieldTrialList methods and members. |
14 | 52 |
15 // static | 53 // static |
16 FieldTrialList* FieldTrialList::global_ = NULL; | 54 FieldTrialList* FieldTrialList::global_ = NULL; |
17 | 55 |
18 FieldTrialList::FieldTrialList() | 56 FieldTrialList::FieldTrialList() |
19 : application_start_time_(Time::Now()) { | 57 : application_start_time_(Time::Now()) { |
20 DCHECK(!global_); | 58 DCHECK(!global_); |
21 global_ = this; | 59 global_ = this; |
22 } | 60 } |
(...skipping 10 matching lines...) Expand all Loading... |
33 | 71 |
34 // static | 72 // static |
35 void FieldTrialList::Register(FieldTrial* trial) { | 73 void FieldTrialList::Register(FieldTrial* trial) { |
36 DCHECK(global_->CalledOnValidThread()); | 74 DCHECK(global_->CalledOnValidThread()); |
37 DCHECK(!Find(trial->name())); | 75 DCHECK(!Find(trial->name())); |
38 trial->AddRef(); | 76 trial->AddRef(); |
39 global_->registered_[trial->name()] = trial; | 77 global_->registered_[trial->name()] = trial; |
40 } | 78 } |
41 | 79 |
42 // static | 80 // static |
43 FieldTrial* FieldTrialList::Find(const std::wstring& name) { | 81 int FieldTrialList::FindValue(const std::string& name) { |
| 82 FieldTrial* field_trial = Find(name); |
| 83 if (field_trial) |
| 84 return field_trial->group(); |
| 85 return FieldTrial::kNotParticipating; |
| 86 } |
| 87 |
| 88 // static |
| 89 std::string FieldTrialList::FindFullName(const std::string& name) { |
| 90 FieldTrial* field_trial = Find(name); |
| 91 if (field_trial) |
| 92 return field_trial->group_name(); |
| 93 return ""; |
| 94 } |
| 95 |
| 96 // static |
| 97 FieldTrial* FieldTrialList::Find(const std::string& name) { |
44 DCHECK(global_->CalledOnValidThread()); | 98 DCHECK(global_->CalledOnValidThread()); |
45 RegistrationList::iterator it = global_->registered_.find(name); | 99 RegistrationList::iterator it = global_->registered_.find(name); |
46 if (global_->registered_.end() == it) | 100 if (global_->registered_.end() == it) |
47 return NULL; | 101 return NULL; |
48 return it->second; | 102 return it->second; |
49 } | 103 } |
50 | 104 |
51 //------------------------------------------------------------------------------ | |
52 // FieldTrial methods and members. | |
53 | |
54 FieldTrial::FieldTrial(const std::wstring& name, double probability) | |
55 : name_(name) { | |
56 double rand = base::RandDouble(); | |
57 DCHECK(rand >= 0.0 && rand < 1.0); | |
58 boolean_value_ = rand < probability; | |
59 FieldTrialList::Register(this); | |
60 } | |
OLD | NEW |