Home | History | Annotate | Download | only in metrics
      1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // Test of FieldTrial class
      6 
      7 #include "base/metrics/field_trial.h"
      8 
      9 #include "base/stringprintf.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 
     12 namespace base {
     13 
     14 class FieldTrialTest : public testing::Test {
     15  public:
     16   FieldTrialTest() : trial_list_() {
     17     Time now = Time::NowFromSystemTime();
     18     TimeDelta oneYear = TimeDelta::FromDays(365);
     19     Time::Exploded exploded;
     20 
     21     Time next_year_time = now + oneYear;
     22     next_year_time.LocalExplode(&exploded);
     23     next_year_ = exploded.year;
     24 
     25     Time last_year_time = now - oneYear;
     26     last_year_time.LocalExplode(&exploded);
     27     last_year_ = exploded.year;
     28   }
     29 
     30  protected:
     31   int next_year_;
     32   int last_year_;
     33 
     34  private:
     35   FieldTrialList trial_list_;
     36 };
     37 
     38 // Test registration, and also check that destructors are called for trials
     39 // (and that Purify doesn't catch us leaking).
     40 TEST_F(FieldTrialTest, Registration) {
     41   const char* name1 = "name 1 test";
     42   const char* name2 = "name 2 test";
     43   EXPECT_FALSE(FieldTrialList::Find(name1));
     44   EXPECT_FALSE(FieldTrialList::Find(name2));
     45 
     46   FieldTrial* trial1 =
     47       new FieldTrial(name1, 10, "default name 1 test", next_year_, 12, 31);
     48   EXPECT_EQ(FieldTrial::kNotFinalized, trial1->group_);
     49   EXPECT_EQ(name1, trial1->name());
     50   EXPECT_EQ("", trial1->group_name_internal());
     51 
     52   trial1->AppendGroup("", 7);
     53 
     54   EXPECT_EQ(trial1, FieldTrialList::Find(name1));
     55   EXPECT_FALSE(FieldTrialList::Find(name2));
     56 
     57   FieldTrial* trial2 =
     58       new FieldTrial(name2, 10, "default name 2 test", next_year_, 12, 31);
     59   EXPECT_EQ(FieldTrial::kNotFinalized, trial2->group_);
     60   EXPECT_EQ(name2, trial2->name());
     61   EXPECT_EQ("", trial2->group_name_internal());
     62 
     63   trial2->AppendGroup("a first group", 7);
     64 
     65   EXPECT_EQ(trial1, FieldTrialList::Find(name1));
     66   EXPECT_EQ(trial2, FieldTrialList::Find(name2));
     67   // Note: FieldTrialList should delete the objects at shutdown.
     68 }
     69 
     70 TEST_F(FieldTrialTest, AbsoluteProbabilities) {
     71   char always_true[] = " always true";
     72   char default_always_true[] = " default always true";
     73   char always_false[] = " always false";
     74   char default_always_false[] = " default always false";
     75   for (int i = 1; i < 250; ++i) {
     76     // Try lots of names, by changing the first character of the name.
     77     always_true[0] = i;
     78     default_always_true[0] = i;
     79     always_false[0] = i;
     80     default_always_false[0] = i;
     81 
     82     FieldTrial* trial_true =
     83         new FieldTrial(
     84             always_true, 10, default_always_true, next_year_, 12, 31);
     85     const std::string winner = "TheWinner";
     86     int winner_group = trial_true->AppendGroup(winner, 10);
     87 
     88     EXPECT_EQ(winner_group, trial_true->group());
     89     EXPECT_EQ(winner, trial_true->group_name());
     90 
     91     FieldTrial* trial_false =
     92         new FieldTrial(
     93             always_false, 10, default_always_false, next_year_, 12, 31);
     94     int loser_group = trial_false->AppendGroup("ALoser", 0);
     95 
     96     EXPECT_NE(loser_group, trial_false->group());
     97   }
     98 }
     99 
    100 TEST_F(FieldTrialTest, RemainingProbability) {
    101   // First create a test that hasn't had a winner yet.
    102   const std::string winner = "Winner";
    103   const std::string loser = "Loser";
    104   scoped_refptr<FieldTrial> trial;
    105   int counter = 0;
    106   do {
    107     std::string name = StringPrintf("trial%d", ++counter);
    108     trial = new FieldTrial(name, 10, winner, next_year_, 12, 31);
    109     trial->AppendGroup(loser, 5);  // 50% chance of not being chosen.
    110     // If a group is not assigned, group_ will be kNotFinalized.
    111   } while (trial->group_ != FieldTrial::kNotFinalized);
    112 
    113   // And that 'default' group (winner) should always win.
    114   EXPECT_EQ(FieldTrial::kDefaultGroupNumber, trial->group());
    115 
    116   // And that winner should ALWAYS win.
    117   EXPECT_EQ(winner, trial->group_name());
    118 }
    119 
    120 TEST_F(FieldTrialTest, FiftyFiftyProbability) {
    121   // Check that even with small divisors, we have the proper probabilities, and
    122   // all outcomes are possible.  Since this is a 50-50 test, it should get both
    123   // outcomes in a few tries, but we'll try no more than 100 times (and be flaky
    124   // with probability around 1 in 2^99).
    125   bool first_winner = false;
    126   bool second_winner = false;
    127   int counter = 0;
    128   do {
    129     std::string name = base::StringPrintf("FiftyFifty%d", ++counter);
    130     std::string default_group_name = base::StringPrintf("Default FiftyFifty%d",
    131                                                         ++counter);
    132     scoped_refptr<FieldTrial> trial(
    133         new FieldTrial(name, 2, default_group_name, next_year_, 12, 31));
    134     trial->AppendGroup("first", 1);  // 50% chance of being chosen.
    135     // If group_ is kNotFinalized, then a group assignement hasn't been done.
    136     if (trial->group_ != FieldTrial::kNotFinalized) {
    137       first_winner = true;
    138       continue;
    139     }
    140     trial->AppendGroup("second", 1);  // Always chosen at this point.
    141     EXPECT_NE(FieldTrial::kNotFinalized, trial->group());
    142     second_winner = true;
    143   } while ((!second_winner || !first_winner) && counter < 100);
    144   EXPECT_TRUE(second_winner);
    145   EXPECT_TRUE(first_winner);
    146 }
    147 
    148 TEST_F(FieldTrialTest, MiddleProbabilities) {
    149   char name[] = " same name";
    150   char default_group_name[] = " default same name";
    151   bool false_event_seen = false;
    152   bool true_event_seen = false;
    153   for (int i = 1; i < 250; ++i) {
    154     name[0] = i;
    155     default_group_name[0] = i;
    156     FieldTrial* trial =
    157         new FieldTrial(name, 10, default_group_name, next_year_, 12, 31);
    158     int might_win = trial->AppendGroup("MightWin", 5);
    159 
    160     if (trial->group() == might_win) {
    161       true_event_seen = true;
    162     } else {
    163       false_event_seen = true;
    164     }
    165     if (false_event_seen && true_event_seen)
    166       return;  // Successful test!!!
    167   }
    168   // Very surprising to get here. Probability should be around 1 in 2 ** 250.
    169   // One of the following will fail.
    170   EXPECT_TRUE(false_event_seen);
    171   EXPECT_TRUE(true_event_seen);
    172 }
    173 
    174 TEST_F(FieldTrialTest, OneWinner) {
    175   char name[] = "Some name";
    176   char default_group_name[] = "Default some name";
    177   int group_count(10);
    178 
    179   FieldTrial* trial =
    180       new FieldTrial(
    181           name, group_count, default_group_name, next_year_, 12, 31);
    182   int winner_index(-2);
    183   std::string winner_name;
    184 
    185   for (int i = 1; i <= group_count; ++i) {
    186     int might_win = trial->AppendGroup("", 1);
    187 
    188     // Because we keep appending groups, we want to see if the last group that
    189     // was added has been assigned or not.
    190     if (trial->group_ == might_win) {
    191       EXPECT_EQ(-2, winner_index);
    192       winner_index = might_win;
    193       StringAppendF(&winner_name, "%d", might_win);
    194       EXPECT_EQ(winner_name, trial->group_name());
    195     }
    196   }
    197   EXPECT_GE(winner_index, 0);
    198   EXPECT_EQ(trial->group(), winner_index);
    199   EXPECT_EQ(trial->group_name(), winner_name);
    200 }
    201 
    202 TEST_F(FieldTrialTest, DisableProbability) {
    203   const std::string default_group_name = "Default group";
    204   const std::string loser = "Loser";
    205   const std::string name = "Trial";
    206 
    207   // Create a field trail that has expired.
    208   scoped_refptr<FieldTrial> trial;
    209   trial = new FieldTrial(
    210       name, 1000000000, default_group_name, last_year_, 1, 1);
    211   trial->AppendGroup(loser, 999999999);  // 99.9999999% chance of being chosen.
    212 
    213   // Because trial has expired, we should always be in the default group.
    214   EXPECT_EQ(FieldTrial::kDefaultGroupNumber, trial->group());
    215 
    216   // And that default_group_name should ALWAYS win.
    217   EXPECT_EQ(default_group_name, trial->group_name());
    218 }
    219 
    220 TEST_F(FieldTrialTest, Save) {
    221   std::string save_string;
    222 
    223   FieldTrial* trial =
    224       new FieldTrial(
    225           "Some name", 10, "Default some name", next_year_, 12, 31);
    226   // There is no winner yet, so no textual group name is associated with trial.
    227   EXPECT_EQ("", trial->group_name_internal());
    228   FieldTrialList::StatesToString(&save_string);
    229   EXPECT_EQ("Some name/Default some name/", save_string);
    230   save_string.clear();
    231 
    232   // Create a winning group.
    233   trial->AppendGroup("Winner", 10);
    234   FieldTrialList::StatesToString(&save_string);
    235   EXPECT_EQ("Some name/Winner/", save_string);
    236   save_string.clear();
    237 
    238   // Create a second trial and winning group.
    239   FieldTrial* trial2 =
    240       new FieldTrial("xxx", 10, "Default xxx", next_year_, 12, 31);
    241   trial2->AppendGroup("yyyy", 10);
    242 
    243   FieldTrialList::StatesToString(&save_string);
    244   // We assume names are alphabetized... though this is not critical.
    245   EXPECT_EQ("Some name/Winner/xxx/yyyy/", save_string);
    246 }
    247 
    248 TEST_F(FieldTrialTest, Restore) {
    249   EXPECT_TRUE(FieldTrialList::Find("Some_name") == NULL);
    250   EXPECT_TRUE(FieldTrialList::Find("xxx") == NULL);
    251 
    252   FieldTrialList::CreateTrialsInChildProcess("Some_name/Winner/xxx/yyyy/");
    253 
    254   FieldTrial* trial = FieldTrialList::Find("Some_name");
    255   ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
    256   EXPECT_EQ("Winner", trial->group_name());
    257   EXPECT_EQ("Some_name", trial->name());
    258 
    259   trial = FieldTrialList::Find("xxx");
    260   ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
    261   EXPECT_EQ("yyyy", trial->group_name());
    262   EXPECT_EQ("xxx", trial->name());
    263 }
    264 
    265 TEST_F(FieldTrialTest, BogusRestore) {
    266   EXPECT_FALSE(FieldTrialList::CreateTrialsInChildProcess("MissingSlash"));
    267   EXPECT_FALSE(FieldTrialList::CreateTrialsInChildProcess("MissingGroupName/"));
    268   EXPECT_FALSE(
    269       FieldTrialList::CreateTrialsInChildProcess("MissingFinalSlash/gname"));
    270   EXPECT_FALSE(
    271       FieldTrialList::CreateTrialsInChildProcess("/noname, only group/"));
    272 }
    273 
    274 TEST_F(FieldTrialTest, DuplicateRestore) {
    275   FieldTrial* trial =
    276       new FieldTrial(
    277           "Some name", 10, "Default some name", next_year_, 12, 31);
    278   trial->AppendGroup("Winner", 10);
    279   std::string save_string;
    280   FieldTrialList::StatesToString(&save_string);
    281   EXPECT_EQ("Some name/Winner/", save_string);
    282 
    283   // It is OK if we redundantly specify a winner.
    284   EXPECT_TRUE(FieldTrialList::CreateTrialsInChildProcess(save_string));
    285 
    286   // But it is an error to try to change to a different winner.
    287   EXPECT_FALSE(FieldTrialList::CreateTrialsInChildProcess("Some name/Loser/"));
    288 }
    289 
    290 TEST_F(FieldTrialTest, MakeName) {
    291   FieldTrial* trial =
    292       new FieldTrial("Field Trial", 10, "Winner", next_year_, 12, 31);
    293   trial->group();
    294   EXPECT_EQ("Histogram_Winner",
    295             FieldTrial::MakeName("Histogram", "Field Trial"));
    296 }
    297 
    298 }  // namespace base
    299