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

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

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

Powered by Google App Engine
This is Rietveld 408576698