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

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

Issue 10830318: Use a different algorithm with the low entropy source for field trials. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 4 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') | chrome/browser/chrome_browser_main.cc » ('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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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
6
7 #include "base/metrics/field_trial.h" 5 #include "base/metrics/field_trial.h"
8
9 #include "base/rand_util.h" 6 #include "base/rand_util.h"
10 #include "base/stringprintf.h" 7 #include "base/stringprintf.h"
11 #include "base/string_number_conversions.h" 8 #include "base/string_number_conversions.h"
12 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
13 10
14 #include <limits>
15
16 namespace base { 11 namespace base {
17 12
18 class FieldTrialTest : public testing::Test { 13 class FieldTrialTest : public testing::Test {
19 public: 14 public:
20 FieldTrialTest() : trial_list_("client_id") { 15 FieldTrialTest() : trial_list_(NULL) {
21 Time now = Time::NowFromSystemTime(); 16 Time now = Time::NowFromSystemTime();
22 TimeDelta oneYear = TimeDelta::FromDays(365); 17 TimeDelta oneYear = TimeDelta::FromDays(365);
23 Time::Exploded exploded; 18 Time::Exploded exploded;
24 19
25 Time next_year_time = now + oneYear; 20 Time next_year_time = now + oneYear;
26 next_year_time.LocalExplode(&exploded); 21 next_year_time.LocalExplode(&exploded);
27 next_year_ = exploded.year; 22 next_year_ = exploded.year;
28 23
29 Time last_year_time = now - oneYear; 24 Time last_year_time = now - oneYear;
30 last_year_time.LocalExplode(&exploded); 25 last_year_time.LocalExplode(&exploded);
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 } 362 }
368 363
369 TEST_F(FieldTrialTest, MakeName) { 364 TEST_F(FieldTrialTest, MakeName) {
370 FieldTrial* trial = FieldTrialList::FactoryGetFieldTrial( 365 FieldTrial* trial = FieldTrialList::FactoryGetFieldTrial(
371 "Field Trial", 10, "Winner", next_year_, 12, 31, NULL); 366 "Field Trial", 10, "Winner", next_year_, 12, 31, NULL);
372 trial->group(); 367 trial->group();
373 EXPECT_EQ("Histogram_Winner", 368 EXPECT_EQ("Histogram_Winner",
374 FieldTrial::MakeName("Histogram", "Field Trial")); 369 FieldTrial::MakeName("Histogram", "Field Trial"));
375 } 370 }
376 371
377 TEST_F(FieldTrialTest, HashClientId) {
378 double results[] = {
379 FieldTrial::HashClientId("hi", "1"),
380 FieldTrial::HashClientId("there", "1"),
381 };
382 ASSERT_NE(results[0], results[1]);
383 for (size_t i = 0; i < arraysize(results); ++i) {
384 ASSERT_LE(0.0, results[i]);
385 ASSERT_GT(1.0, results[i]);
386 }
387
388 ASSERT_EQ(FieldTrial::HashClientId("yo", "1"),
389 FieldTrial::HashClientId("yo", "1"));
390 ASSERT_NE(FieldTrial::HashClientId("yo", "something"),
391 FieldTrial::HashClientId("yo", "else"));
392 }
393
394 TEST_F(FieldTrialTest, HashClientIdIsUniform) {
395 // Choose a random start number but go sequentially from there, so
396 // that each test tries a different range but we never provide uniformly
397 // distributed input data.
398 int current_number = RandInt(0, std::numeric_limits<int>::max());
399
400 // The expected value of a random distribution is the average over all
401 // samples as the number of samples approaches infinity. For a uniform
402 // distribution from [0.0, 1.0) this would be 0.5.
403 //
404 // We do kSamplesBetweenChecks at a time and check if the value has converged
405 // to a narrow interval around 0.5. A non-uniform distribution would likely
406 // converge at something different, or not converge consistently within this
407 // range (i.e. the test would start timing out occasionally).
408 int kSamplesBetweenChecks = 300;
409 int num_samples = 0;
410 double total_value = 0.0;
411 while (true) {
412 for (int i = 0; i < kSamplesBetweenChecks; ++i) {
413 total_value += FieldTrial::HashClientId(
414 IntToString(current_number++), "salt");
415 num_samples++;
416 }
417
418 double average = total_value / num_samples;
419 double kExpectedMin = 0.48;
420 double kExpectedMax = 0.52;
421
422 if (num_samples > 1000 &&
423 (average < kExpectedMin || average > kExpectedMax)) {
424 // Only printed once we have enough samples that it's very unlikely
425 // things haven't converged.
426 printf("After %d samples, the average was %f, outside the expected\n"
427 "range (%f, %f). We will add more samples and check after every\n"
428 "%d samples. If the average does not converge, something\n"
429 "is broken. If it does converge, the test will pass.\n",
430 num_samples, average,
431 kExpectedMin, kExpectedMax, kSamplesBetweenChecks);
432 } else {
433 // Success.
434 break;
435 }
436 }
437 }
438
439 TEST_F(FieldTrialTest, UseOneTimeRandomization) {
440 // Simply asserts that two trials using one-time randomization
441 // that have different names, normally generate different results.
442 //
443 // Note that depending on the one-time random initialization, they
444 // _might_ actually give the same result, but we know that given
445 // the particular client_id we use for unit tests they won't.
446 scoped_refptr<FieldTrial> trials[] = {
447 FieldTrialList::FactoryGetFieldTrial("one", 100, "default",
448 next_year_, 1, 1, NULL),
449 FieldTrialList::FactoryGetFieldTrial("two", 100, "default",
450 next_year_, 1, 1, NULL),
451 };
452
453 for (size_t i = 0; i < arraysize(trials); ++i) {
454 trials[i]->UseOneTimeRandomization();
455
456 for (int j = 0; j < 100; ++j) {
457 trials[i]->AppendGroup("", 1);
458 }
459 }
460
461 // The trials are most likely to give different results since they have
462 // different names.
463 ASSERT_NE(trials[0]->group(), trials[1]->group());
464 ASSERT_NE(trials[0]->group_name(), trials[1]->group_name());
465 }
466
467 TEST_F(FieldTrialTest, DisableImmediately) { 372 TEST_F(FieldTrialTest, DisableImmediately) {
468 int default_group_number = -1; 373 int default_group_number = -1;
469 FieldTrial* trial = FieldTrialList::FactoryGetFieldTrial( 374 FieldTrial* trial = FieldTrialList::FactoryGetFieldTrial(
470 "trial", 100, "default", next_year_, 12, 31, &default_group_number); 375 "trial", 100, "default", next_year_, 12, 31, &default_group_number);
471 trial->Disable(); 376 trial->Disable();
472 ASSERT_EQ("default", trial->group_name()); 377 ASSERT_EQ("default", trial->group_name());
473 ASSERT_EQ(default_group_number, trial->group()); 378 ASSERT_EQ(default_group_number, trial->group());
474 } 379 }
475 380
476 TEST_F(FieldTrialTest, DisableAfterInitialization) { 381 TEST_F(FieldTrialTest, DisableAfterInitialization) {
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
548 FieldTrial* other_hard_coded_trial = FieldTrialList::FactoryGetFieldTrial( 453 FieldTrial* other_hard_coded_trial = FieldTrialList::FactoryGetFieldTrial(
549 "Use the", 1, "default", next_year_, 12, 31, &default_group_number); 454 "Use the", 1, "default", next_year_, 12, 31, &default_group_number);
550 EXPECT_EQ(other_hard_coded_trial, forced_trial); 455 EXPECT_EQ(other_hard_coded_trial, forced_trial);
551 456
552 int would_win_group = other_hard_coded_trial->AppendGroup("Force", 1); 457 int would_win_group = other_hard_coded_trial->AppendGroup("Force", 1);
553 EXPECT_EQ(forced_group, other_hard_coded_trial->group()); 458 EXPECT_EQ(forced_group, other_hard_coded_trial->group());
554 EXPECT_EQ(forced_group, would_win_group); 459 EXPECT_EQ(forced_group, would_win_group);
555 } 460 }
556 461
557 } // namespace base 462 } // namespace base
OLDNEW
« no previous file with comments | « base/metrics/field_trial.cc ('k') | chrome/browser/chrome_browser_main.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698