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

Side by Side Diff: base/metrics/field_trial_unittest.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: Merge to head. Created 9 years, 7 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
« no previous file with comments | « base/metrics/field_trial.cc ('k') | base/rand_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 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 // 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/rand_util.h"
9 #include "base/stringprintf.h" 10 #include "base/stringprintf.h"
11 #include "base/string_number_conversions.h"
10 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
11 13
14 #include <limits>
15
12 namespace base { 16 namespace base {
13 17
14 class FieldTrialTest : public testing::Test { 18 class FieldTrialTest : public testing::Test {
15 public: 19 public:
16 FieldTrialTest() : trial_list_() { 20 FieldTrialTest() : trial_list_("client_id") {
17 Time now = Time::NowFromSystemTime(); 21 Time now = Time::NowFromSystemTime();
18 TimeDelta oneYear = TimeDelta::FromDays(365); 22 TimeDelta oneYear = TimeDelta::FromDays(365);
19 Time::Exploded exploded; 23 Time::Exploded exploded;
20 24
21 Time next_year_time = now + oneYear; 25 Time next_year_time = now + oneYear;
22 next_year_time.LocalExplode(&exploded); 26 next_year_time.LocalExplode(&exploded);
23 next_year_ = exploded.year; 27 next_year_ = exploded.year;
24 28
25 Time last_year_time = now - oneYear; 29 Time last_year_time = now - oneYear;
26 last_year_time.LocalExplode(&exploded); 30 last_year_time.LocalExplode(&exploded);
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 EXPECT_EQ(default_group_name, trial->group_name()); 221 EXPECT_EQ(default_group_name, trial->group_name());
218 } 222 }
219 223
220 TEST_F(FieldTrialTest, Save) { 224 TEST_F(FieldTrialTest, Save) {
221 std::string save_string; 225 std::string save_string;
222 226
223 FieldTrial* trial = 227 FieldTrial* trial =
224 new FieldTrial( 228 new FieldTrial(
225 "Some name", 10, "Default some name", next_year_, 12, 31); 229 "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. 230 // There is no winner yet, so no textual group name is associated with trial.
231 // In this case, the trial should not be included.
227 EXPECT_EQ("", trial->group_name_internal()); 232 EXPECT_EQ("", trial->group_name_internal());
228 FieldTrialList::StatesToString(&save_string); 233 FieldTrialList::StatesToString(&save_string);
229 EXPECT_EQ("Some name/Default some name/", save_string); 234 EXPECT_EQ("", save_string);
230 save_string.clear(); 235 save_string.clear();
231 236
232 // Create a winning group. 237 // Create a winning group.
233 trial->AppendGroup("Winner", 10); 238 trial->AppendGroup("Winner", 10);
234 FieldTrialList::StatesToString(&save_string); 239 FieldTrialList::StatesToString(&save_string);
235 EXPECT_EQ("Some name/Winner/", save_string); 240 EXPECT_EQ("Some name/Winner/", save_string);
236 save_string.clear(); 241 save_string.clear();
237 242
238 // Create a second trial and winning group. 243 // Create a second trial and winning group.
239 FieldTrial* trial2 = 244 FieldTrial* trial2 =
(...skipping 18 matching lines...) Expand all
258 263
259 trial = FieldTrialList::Find("xxx"); 264 trial = FieldTrialList::Find("xxx");
260 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial); 265 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
261 EXPECT_EQ("yyyy", trial->group_name()); 266 EXPECT_EQ("yyyy", trial->group_name());
262 EXPECT_EQ("xxx", trial->name()); 267 EXPECT_EQ("xxx", trial->name());
263 } 268 }
264 269
265 TEST_F(FieldTrialTest, BogusRestore) { 270 TEST_F(FieldTrialTest, BogusRestore) {
266 EXPECT_FALSE(FieldTrialList::CreateTrialsInChildProcess("MissingSlash")); 271 EXPECT_FALSE(FieldTrialList::CreateTrialsInChildProcess("MissingSlash"));
267 EXPECT_FALSE(FieldTrialList::CreateTrialsInChildProcess("MissingGroupName/")); 272 EXPECT_FALSE(FieldTrialList::CreateTrialsInChildProcess("MissingGroupName/"));
268 EXPECT_FALSE( 273 EXPECT_FALSE(FieldTrialList::CreateTrialsInChildProcess(
269 FieldTrialList::CreateTrialsInChildProcess("MissingFinalSlash/gname")); 274 "MissingFinalSlash/gname"));
270 EXPECT_FALSE( 275 EXPECT_FALSE(FieldTrialList::CreateTrialsInChildProcess(
271 FieldTrialList::CreateTrialsInChildProcess("/noname, only group/")); 276 "noname, only group/"));
272 } 277 }
273 278
274 TEST_F(FieldTrialTest, DuplicateRestore) { 279 TEST_F(FieldTrialTest, DuplicateRestore) {
275 FieldTrial* trial = 280 FieldTrial* trial =
276 new FieldTrial( 281 new FieldTrial(
277 "Some name", 10, "Default some name", next_year_, 12, 31); 282 "Some name", 10, "Default some name", next_year_, 12, 31);
278 trial->AppendGroup("Winner", 10); 283 trial->AppendGroup("Winner", 10);
279 std::string save_string; 284 std::string save_string;
280 FieldTrialList::StatesToString(&save_string); 285 FieldTrialList::StatesToString(&save_string);
281 EXPECT_EQ("Some name/Winner/", save_string); 286 EXPECT_EQ("Some name/Winner/", save_string);
282 287
283 // It is OK if we redundantly specify a winner. 288 // It is OK if we redundantly specify a winner.
284 EXPECT_TRUE(FieldTrialList::CreateTrialsInChildProcess(save_string)); 289 EXPECT_TRUE(FieldTrialList::CreateTrialsInChildProcess(save_string));
285 290
286 // But it is an error to try to change to a different winner. 291 // But it is an error to try to change to a different winner.
287 EXPECT_FALSE(FieldTrialList::CreateTrialsInChildProcess("Some name/Loser/")); 292 EXPECT_FALSE(FieldTrialList::CreateTrialsInChildProcess(
293 "Some name/Loser/"));
288 } 294 }
289 295
290 TEST_F(FieldTrialTest, CreateFieldTrial) { 296 TEST_F(FieldTrialTest, CreateFieldTrial) {
291 EXPECT_TRUE(FieldTrialList::Find("Some_name") == NULL); 297 EXPECT_TRUE(FieldTrialList::Find("Some_name") == NULL);
292 298
293 FieldTrialList::CreateFieldTrial("Some_name", "Winner"); 299 FieldTrialList::CreateFieldTrial("Some_name", "Winner");
294 300
295 FieldTrial* trial = FieldTrialList::Find("Some_name"); 301 FieldTrial* trial = FieldTrialList::Find("Some_name");
296 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial); 302 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
297 EXPECT_EQ("Winner", trial->group_name()); 303 EXPECT_EQ("Winner", trial->group_name());
(...skipping 16 matching lines...) Expand all
314 } 320 }
315 321
316 TEST_F(FieldTrialTest, MakeName) { 322 TEST_F(FieldTrialTest, MakeName) {
317 FieldTrial* trial = 323 FieldTrial* trial =
318 new FieldTrial("Field Trial", 10, "Winner", next_year_, 12, 31); 324 new FieldTrial("Field Trial", 10, "Winner", next_year_, 12, 31);
319 trial->group(); 325 trial->group();
320 EXPECT_EQ("Histogram_Winner", 326 EXPECT_EQ("Histogram_Winner",
321 FieldTrial::MakeName("Histogram", "Field Trial")); 327 FieldTrial::MakeName("Histogram", "Field Trial"));
322 } 328 }
323 329
330 TEST_F(FieldTrialTest, HashClientId) {
331 double results[] = {
332 FieldTrial::HashClientId("hi", "1"),
333 FieldTrial::HashClientId("there", "1"),
334 };
335 ASSERT_NE(results[0], results[1]);
336 for (size_t i = 0; i < arraysize(results); ++i) {
337 ASSERT_LE(0.0, results[i]);
338 ASSERT_GT(1.0, results[i]);
339 }
340
341 ASSERT_EQ(FieldTrial::HashClientId("yo", "1"),
342 FieldTrial::HashClientId("yo", "1"));
343 ASSERT_NE(FieldTrial::HashClientId("yo", "something"),
344 FieldTrial::HashClientId("yo", "else"));
345 }
346
347 TEST_F(FieldTrialTest, HashClientIdIsUniform) {
348 // Choose a random start number but go sequentially from there, so
349 // that each test tries a different range but we never provide uniformly
350 // distributed input data.
351 int current_number = RandInt(0, std::numeric_limits<int>::max());
352
353 // The expected value of a random distribution is the average over all
354 // samples as the number of samples approaches infinity. For a uniform
355 // distribution from [0.0, 1.0) this would be 0.5.
356 //
357 // We do kSamplesBetweenChecks at a time and check if the value has converged
358 // to a narrow interval around 0.5. A non-uniform distribution would likely
359 // converge at something different, or not converge consistently within this
360 // range (i.e. the test would start timing out occasionally).
361 int kSamplesBetweenChecks = 300;
362 int num_samples = 0;
363 double total_value = 0.0;
364 while (true) {
365 for (int i = 0; i < kSamplesBetweenChecks; ++i) {
366 total_value += FieldTrial::HashClientId(
367 IntToString(current_number++), "salt");
368 num_samples++;
369 }
370
371 double average = total_value / num_samples;
372 double kExpectedMin = 0.48;
373 double kExpectedMax = 0.52;
374
375 if (num_samples > 1000 &&
376 (average < kExpectedMin || average > kExpectedMax)) {
377 // Only printed once we have enough samples that it's very unlikely
378 // things haven't converged.
379 printf("After %d samples, the average was %f, outside the expected\n"
380 "range (%f, %f). We will add more samples and check after every\n"
381 "%d samples. If the average does not converge, something\n"
382 "is broken. If it does converge, the test will pass.\n",
383 num_samples, average,
384 kExpectedMin, kExpectedMax, kSamplesBetweenChecks);
385 } else {
386 // Success.
387 break;
388 }
389 }
390 }
391
392 TEST_F(FieldTrialTest, UseOneTimeRandomization) {
393 // Simply asserts that two trials using one-time randomization
394 // that have different names, normally generate different results.
395 //
396 // Note that depending on the one-time random initialization, they
397 // _might_ actually give the same result, but we know that given
398 // the particular client_id we use for unit tests they won't.
399 scoped_refptr<FieldTrial> trials[] = {
400 new FieldTrial("one", 100, "default", next_year_, 1, 1),
401 new FieldTrial("two", 100, "default", next_year_, 1, 1),
402 };
403
404 for (size_t i = 0; i < arraysize(trials); ++i) {
405 trials[i]->UseOneTimeRandomization();
406
407 for (int j = 0; j < 100; ++j) {
408 trials[i]->AppendGroup("", 1);
409 }
410 }
411
412 // The trials are most likely to give different results since they have
413 // different names.
414 ASSERT_NE(trials[0]->group(), trials[1]->group());
415 ASSERT_NE(trials[0]->group_name(), trials[1]->group_name());
416 }
417
418 TEST_F(FieldTrialTest, DisableImmediately) {
419 FieldTrial* trial =
420 new FieldTrial("trial", 100, "default", next_year_, 12, 31);
421 trial->Disable();
422 ASSERT_EQ("default", trial->group_name());
423 ASSERT_EQ(FieldTrial::kDefaultGroupNumber, trial->group());
424 }
425
426 TEST_F(FieldTrialTest, DisableAfterInitialization) {
427 FieldTrial* trial =
428 new FieldTrial("trial", 100, "default", next_year_, 12, 31);
429 trial->AppendGroup("non_default", 100);
430 ASSERT_EQ("non_default", trial->group_name());
431 trial->Disable();
432 ASSERT_EQ("default", trial->group_name());
433 }
434
324 } // namespace base 435 } // namespace base
OLDNEW
« no previous file with comments | « base/metrics/field_trial.cc ('k') | base/rand_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698