Chromium Code Reviews| 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 |
| 12 namespace base { | 14 namespace base { |
| 13 | 15 |
| 14 class FieldTrialTest : public testing::Test { | 16 class FieldTrialTest : public testing::Test { |
| 15 public: | 17 public: |
| 16 FieldTrialTest() : trial_list_() { | 18 FieldTrialTest() : trial_list_() { |
| 17 Time now = Time::NowFromSystemTime(); | 19 Time now = Time::NowFromSystemTime(); |
| 18 TimeDelta oneYear = TimeDelta::FromDays(365); | 20 TimeDelta oneYear = TimeDelta::FromDays(365); |
| 19 Time::Exploded exploded; | 21 Time::Exploded exploded; |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 288 } | 290 } |
| 289 | 291 |
| 290 TEST_F(FieldTrialTest, MakeName) { | 292 TEST_F(FieldTrialTest, MakeName) { |
| 291 FieldTrial* trial = | 293 FieldTrial* trial = |
| 292 new FieldTrial("Field Trial", 10, "Winner", next_year_, 12, 31); | 294 new FieldTrial("Field Trial", 10, "Winner", next_year_, 12, 31); |
| 293 trial->group(); | 295 trial->group(); |
| 294 EXPECT_EQ("Histogram_Winner", | 296 EXPECT_EQ("Histogram_Winner", |
| 295 FieldTrial::MakeName("Histogram", "Field Trial")); | 297 FieldTrial::MakeName("Histogram", "Field Trial")); |
| 296 } | 298 } |
| 297 | 299 |
| 300 TEST_F(FieldTrialTest, MachineIdToUniformDouble) { | |
| 301 double results[] = { | |
| 302 FieldTrial::MachineIdToUniformDouble("hi"), | |
| 303 FieldTrial::MachineIdToUniformDouble("there"), | |
| 304 }; | |
| 305 EXPECT_NE(results[0], results[1]); | |
| 306 for (int i = 0; i < arraysize(results); ++i) { | |
| 307 EXPECT_LE(0.0, results[i]); | |
| 308 EXPECT_GT(1.0, results[i]); | |
| 309 } | |
| 310 | |
| 311 EXPECT_EQ(FieldTrial::MachineIdToUniformDouble("yo"), | |
| 312 FieldTrial::MachineIdToUniformDouble("yo")); | |
| 313 } | |
| 314 | |
| 315 // Not marking flaky as it should be incredibly rare for it to | |
| 316 // not pass, and the diagnostics we print in this case would help | |
| 317 // determine whether it is a true failure. | |
| 318 TEST_F(FieldTrialTest, ProbabilisticMachineIdToUniformDoubleIsUniform) { | |
| 319 // A uniform distribution should result in an expected average value | |
| 320 // of 0.5. The actual average for a large number of samples should, | |
| 321 // with extremely high probability, be very close to this. | |
| 322 const int kNumSamples = 5000; | |
| 323 double total_value = 0.0; | |
| 324 for (int i = 0; i < kNumSamples; ++i) { | |
| 325 total_value += FieldTrial::MachineIdToUniformDouble( | |
| 326 IntToString(RandInt(0, MAXINT))); | |
| 327 } | |
| 328 | |
| 329 double average = total_value / kNumSamples; | |
| 330 double kExpectedMin = 0.45; | |
| 331 double kExpectedMax = 0.55; | |
| 332 EXPECT_LT(kExpectedMin, average); | |
| 333 EXPECT_GT(kExpectedMax, average); | |
|
jar (doing other things)
2011/04/21 01:03:50
Flaky tests are not acceptable, even with an expla
Jói
2011/04/21 19:50:33
Fixed.
| |
| 334 | |
| 335 if (average < kExpectedMin || average > kExpectedMax) { | |
| 336 printf("Average was %f, outside the expected range (%f, %f).\n" | |
| 337 "Values far outside the range may indicate a real problem,\n" | |
| 338 "whereas values just outside the range are likely just flukes.\n", | |
| 339 average, kExpectedMin, kExpectedMax); | |
| 340 } | |
| 341 } | |
| 342 | |
| 343 TEST_F(FieldTrialTest, UseOneTimeRandomization) { | |
| 344 // Simply asserts that two trials with the same machine_id and the | |
| 345 // same set of groups should return the same results. | |
| 346 | |
| 347 scoped_refptr<FieldTrial> trials[] = { | |
| 348 new FieldTrial("first", 100, "default", 2050, 1, 1), | |
|
jar (doing other things)
2011/04/21 01:03:50
use (... next_year_, 12, 31) for the date. See li
Jói
2011/04/21 19:50:33
Done.
| |
| 349 new FieldTrial("second", 100, "default", 2050, 1, 1), | |
| 350 }; | |
| 351 | |
| 352 std::string machine_id = IntToString(RandInt(0, MAXINT)); | |
| 353 for (int i = 0; i < arraysize(trials); ++i) { | |
| 354 // Same machine_id for both trials. | |
| 355 trials[i]->UseOneTimeRandomization(machine_id); | |
| 356 | |
| 357 for (int j = 0; j < 10; ++j) { | |
| 358 trials[i]->AppendGroup("", 10); | |
| 359 } | |
| 360 } | |
| 361 | |
| 362 ASSERT_EQ(trials[0]->group(), trials[1]->group()); | |
| 363 ASSERT_EQ(trials[0]->group_name(), trials[1]->group_name()); | |
| 364 } | |
| 365 | |
| 298 } // namespace base | 366 } // namespace base |
| OLD | NEW |