OLD | NEW |
1 // Copyright (c) 2010 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_() { |
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; |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 } | 292 } |
289 | 293 |
290 TEST_F(FieldTrialTest, MakeName) { | 294 TEST_F(FieldTrialTest, MakeName) { |
291 FieldTrial* trial = | 295 FieldTrial* trial = |
292 new FieldTrial("Field Trial", 10, "Winner", next_year_, 12, 31); | 296 new FieldTrial("Field Trial", 10, "Winner", next_year_, 12, 31); |
293 trial->group(); | 297 trial->group(); |
294 EXPECT_EQ("Histogram_Winner", | 298 EXPECT_EQ("Histogram_Winner", |
295 FieldTrial::MakeName("Histogram", "Field Trial")); | 299 FieldTrial::MakeName("Histogram", "Field Trial")); |
296 } | 300 } |
297 | 301 |
| 302 TEST_F(FieldTrialTest, HashClientId) { |
| 303 double results[] = { |
| 304 FieldTrial::HashClientId("hi", "1"), |
| 305 FieldTrial::HashClientId("there", "1"), |
| 306 }; |
| 307 ASSERT_NE(results[0], results[1]); |
| 308 for (size_t i = 0; i < arraysize(results); ++i) { |
| 309 ASSERT_LE(0.0, results[i]); |
| 310 ASSERT_GT(1.0, results[i]); |
| 311 } |
| 312 |
| 313 ASSERT_EQ(FieldTrial::HashClientId("yo", "1"), |
| 314 FieldTrial::HashClientId("yo", "1")); |
| 315 ASSERT_NE(FieldTrial::HashClientId("yo", "something"), |
| 316 FieldTrial::HashClientId("yo", "else")); |
| 317 } |
| 318 |
| 319 TEST_F(FieldTrialTest, HashClientIdIsUniform) { |
| 320 // Choose a random start number but go sequentially from there, so |
| 321 // that each test tries a different range but we never provide uniformly |
| 322 // distributed input data. |
| 323 int current_number = RandInt(0, std::numeric_limits<int>::max()); |
| 324 |
| 325 // The expected value of a random distribution is the average over all |
| 326 // samples as the number of samples approaches infinity. For a uniform |
| 327 // distribution from [0.0, 1.0) this would be 0.5. |
| 328 // |
| 329 // We do kSamplesBetweenChecks at a time and check if the value has converged |
| 330 // to a narrow interval around 0.5. A non-uniform distribution would likely |
| 331 // converge at something different, or not converge consistently within this |
| 332 // range (i.e. the test would start timing out occasionally). |
| 333 int kSamplesBetweenChecks = 300; |
| 334 int num_samples = 0; |
| 335 double total_value = 0.0; |
| 336 while (true) { |
| 337 for (int i = 0; i < kSamplesBetweenChecks; ++i) { |
| 338 total_value += FieldTrial::HashClientId( |
| 339 IntToString(current_number++), "salt"); |
| 340 num_samples++; |
| 341 } |
| 342 |
| 343 double average = total_value / num_samples; |
| 344 double kExpectedMin = 0.48; |
| 345 double kExpectedMax = 0.52; |
| 346 |
| 347 if (average < kExpectedMin || average > kExpectedMax) { |
| 348 printf("After %d samples, the average was %f, outside the expected\n" |
| 349 "range (%f, %f). We will add more samples and check after every\n" |
| 350 "%d samples. If the average does not converge, something\n" |
| 351 "is broken. If it does converge, the test will pass.\n", |
| 352 num_samples, average, |
| 353 kExpectedMin, kExpectedMax, kSamplesBetweenChecks); |
| 354 } else { |
| 355 // Success. |
| 356 break; |
| 357 } |
| 358 } |
| 359 } |
| 360 |
| 361 TEST_F(FieldTrialTest, UseOneTimeRandomization) { |
| 362 // Simply asserts that two trials using one-time randomization |
| 363 // that have different names, normally generate different results. |
| 364 // |
| 365 // Note that depending on the one-time random initialization, they |
| 366 // _might_ actually give the same result, but we know that given |
| 367 // this particular initialization they won't. |
| 368 FieldTrialList::EnableOneTimeRandomization("this is cheating"); |
| 369 |
| 370 scoped_refptr<FieldTrial> trials[] = { |
| 371 new FieldTrial("one", 100, "default", next_year_, 1, 1), |
| 372 new FieldTrial("two", 100, "default", next_year_, 1, 1), |
| 373 }; |
| 374 |
| 375 for (size_t i = 0; i < arraysize(trials); ++i) { |
| 376 trials[i]->UseOneTimeRandomization(); |
| 377 |
| 378 for (int j = 0; j < 100; ++j) { |
| 379 trials[i]->AppendGroup("", 1); |
| 380 } |
| 381 } |
| 382 |
| 383 // The trials are most likely to give different results since they have |
| 384 // different names. |
| 385 ASSERT_NE(trials[0]->group(), trials[1]->group()); |
| 386 ASSERT_NE(trials[0]->group_name(), trials[1]->group_name()); |
| 387 } |
| 388 |
| 389 TEST_F(FieldTrialTest, DisableImmediately) { |
| 390 FieldTrial* trial = |
| 391 new FieldTrial("trial", 100, "default", next_year_, 12, 31); |
| 392 trial->Disable(); |
| 393 ASSERT_EQ("default", trial->group_name()); |
| 394 ASSERT_EQ(FieldTrial::kDefaultGroupNumber, trial->group()); |
| 395 } |
| 396 |
| 397 TEST_F(FieldTrialTest, DisableAfterInitialization) { |
| 398 FieldTrial* trial = |
| 399 new FieldTrial("trial", 100, "default", next_year_, 12, 31); |
| 400 trial->AppendGroup("non_default", 100); |
| 401 ASSERT_EQ("non_default", trial->group_name()); |
| 402 trial->Disable(); |
| 403 ASSERT_EQ("default", trial->group_name()); |
| 404 } |
| 405 |
298 } // namespace base | 406 } // namespace base |
OLD | NEW |