diff options
author | jar@chromium.org <jar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-03-02 19:02:56 +0000 |
---|---|---|
committer | jar@chromium.org <jar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-03-02 19:02:56 +0000 |
commit | 9660b97b7869116645a92d342c52a857d0550ec2 (patch) | |
tree | 06e907b12fbd3d019283fef414422dfd4205e592 /base/field_trial.cc | |
parent | d02e43a80f0fae032501e68d38bd562976b9d866 (diff) | |
download | chromium_src-9660b97b7869116645a92d342c52a857d0550ec2.zip chromium_src-9660b97b7869116645a92d342c52a857d0550ec2.tar.gz chromium_src-9660b97b7869116645a92d342c52a857d0550ec2.tar.bz2 |
Renovate FieldTrial class to better bit with histogram usage.
r=mbelshe
Review URL: http://codereview.chromium.org/28226
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@10714 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/field_trial.cc')
-rw-r--r-- | base/field_trial.cc | 66 |
1 files changed, 55 insertions, 11 deletions
diff --git a/base/field_trial.cc b/base/field_trial.cc index c88ab65..94121d7 100644 --- a/base/field_trial.cc +++ b/base/field_trial.cc @@ -6,10 +6,48 @@ #include "base/field_trial.h" #include "base/logging.h" #include "base/rand_util.h" +#include "base/string_util.h" using base::Time; //------------------------------------------------------------------------------ +// FieldTrial methods and members. + +FieldTrial::FieldTrial(const std::string& name, + const Probability total_probability) + : name_(name), + divisor_(total_probability), + random_(static_cast<Probability>(divisor_ * base::RandDouble())), + accumulated_group_probability_(0), + next_group_number_(0), + group_(kNotParticipating) { + FieldTrialList::Register(this); +} + +int FieldTrial::AppendGroup(const std::string& name, + Probability group_probability) { + DCHECK(group_probability <= divisor_); + accumulated_group_probability_ += group_probability; + DCHECK(accumulated_group_probability_ <= divisor_); + if (group_ == kNotParticipating && accumulated_group_probability_ > random_) { + // This is the group that crossed the random line, so we do teh assignment. + group_ = next_group_number_; + if (name.empty()) + StringAppendF(&group_name_, "_%d", group_); + else + group_name_ = name; + } + return next_group_number_++; +} + +// static +std::string FieldTrial::MakeName(const std::string& name_prefix, + const std::string& trial_name) { + std::string big_string(name_prefix); + return big_string.append(FieldTrialList::FindFullName(trial_name)); +} + +//------------------------------------------------------------------------------ // FieldTrialList methods and members. // static @@ -40,7 +78,23 @@ void FieldTrialList::Register(FieldTrial* trial) { } // static -FieldTrial* FieldTrialList::Find(const std::wstring& name) { +int FieldTrialList::FindValue(const std::string& name) { + FieldTrial* field_trial = Find(name); + if (field_trial) + return field_trial->group(); + return FieldTrial::kNotParticipating; +} + +// static +std::string FieldTrialList::FindFullName(const std::string& name) { + FieldTrial* field_trial = Find(name); + if (field_trial) + return field_trial->group_name(); + return ""; +} + + // static +FieldTrial* FieldTrialList::Find(const std::string& name) { DCHECK(global_->CalledOnValidThread()); RegistrationList::iterator it = global_->registered_.find(name); if (global_->registered_.end() == it) @@ -48,13 +102,3 @@ FieldTrial* FieldTrialList::Find(const std::wstring& name) { return it->second; } -//------------------------------------------------------------------------------ -// FieldTrial methods and members. - -FieldTrial::FieldTrial(const std::wstring& name, double probability) - : name_(name) { - double rand = base::RandDouble(); - DCHECK(rand >= 0.0 && rand < 1.0); - boolean_value_ = rand < probability; - FieldTrialList::Register(this); -} |