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

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: Unit test cancel support. Created 9 years, 8 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) 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/perftimer.h"
10 #include "base/rand_util.h"
9 #include "base/stringprintf.h" 11 #include "base/stringprintf.h"
12 #include "base/string_number_conversions.h"
10 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
11 14
15 #include <limits>
16
12 namespace base { 17 namespace base {
13 18
14 class FieldTrialTest : public testing::Test { 19 class FieldTrialTest : public testing::Test {
15 public: 20 public:
16 FieldTrialTest() : trial_list_() { 21 FieldTrialTest() : trial_list_() {
17 Time now = Time::NowFromSystemTime(); 22 Time now = Time::NowFromSystemTime();
18 TimeDelta oneYear = TimeDelta::FromDays(365); 23 TimeDelta oneYear = TimeDelta::FromDays(365);
19 Time::Exploded exploded; 24 Time::Exploded exploded;
20 25
21 Time next_year_time = now + oneYear; 26 Time next_year_time = now + oneYear;
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 } 293 }
289 294
290 TEST_F(FieldTrialTest, MakeName) { 295 TEST_F(FieldTrialTest, MakeName) {
291 FieldTrial* trial = 296 FieldTrial* trial =
292 new FieldTrial("Field Trial", 10, "Winner", next_year_, 12, 31); 297 new FieldTrial("Field Trial", 10, "Winner", next_year_, 12, 31);
293 trial->group(); 298 trial->group();
294 EXPECT_EQ("Histogram_Winner", 299 EXPECT_EQ("Histogram_Winner",
295 FieldTrial::MakeName("Histogram", "Field Trial")); 300 FieldTrial::MakeName("Histogram", "Field Trial"));
296 } 301 }
297 302
303 TEST_F(FieldTrialTest, HashClientId) {
304 double results[] = {
305 FieldTrial::HashClientId("hi", "1"),
306 FieldTrial::HashClientId("there", "1"),
307 };
308 ASSERT_NE(results[0], results[1]);
309 for (size_t i = 0; i < arraysize(results); ++i) {
310 ASSERT_LE(0.0, results[i]);
311 ASSERT_GT(1.0, results[i]);
312 }
313
314 ASSERT_EQ(FieldTrial::HashClientId("yo", "1"),
315 FieldTrial::HashClientId("yo", "1"));
316 ASSERT_NE(FieldTrial::HashClientId("yo", "something"),
317 FieldTrial::HashClientId("yo", "else"));
318 }
319
320 TEST_F(FieldTrialTest, HashClientIdIsUniform) {
321 PerfTimer timer;
322 bool success = false;
323 while (!success && timer.Elapsed() < TimeDelta::FromSeconds(20)) {
jar (doing other things) 2011/04/21 22:10:02 You probably don't need to (and shouldn't) stop in
Paweł Hajdan Jr. 2011/04/26 10:23:10 Just in case you decide to keep a timeout here, pl
Jói 2011/04/28 01:03:50 Thanks, I'm removing the timeout altogether.
Jói 2011/04/28 01:03:50 The way the test was written (with or without the
324 // A uniform distribution should result in an expected average value
325 // of 0.5. The actual average for a large number of samples should,
326 // with extremely high probability, be very close to this.
327 const int kNumSamples = 1000;
328 double total_value = 0.0;
329
330 // Choose a random start number but go sequentially from there, so
331 // that each test tries a different range but we never provide uniformly
332 // distributed data.
333 int start_number = RandInt(0, std::numeric_limits<int>::max());
334 for (int i = 0; i < kNumSamples; ++i) {
335 total_value += FieldTrial::HashClientId(
336 IntToString(start_number + i), "salt");
337 }
338
339 double average = total_value / kNumSamples;
340 double kExpectedMin = 0.45;
341 double kExpectedMax = 0.55;
342
343 if (average < kExpectedMin || average > kExpectedMax) {
344 printf("Average was %f, outside the expected range (%f, %f).\n"
345 "Values far outside the range may indicate a real problem,\n"
346 "whereas values just outside the range are likely just flukes.\n"
347 "(Will probably retry and print PASSED.)",
348 average, kExpectedMin, kExpectedMax);
349 } else {
350 success = true;
351 }
352 }
353
354 ASSERT_TRUE(success);
355 }
356
357 TEST_F(FieldTrialTest, UseOneTimeRandomization) {
358 // Simply asserts that two trials using one-time randomization
359 // that have different names, normally generate different results.
360 //
361 // Note that depending on the one-time random initialization, they
362 // _might_ actually give the same result, but we know that given
363 // this particular initialization they won't.
364 FieldTrialList::EnableOneTimeRandomization("this is cheating");
365
366 scoped_refptr<FieldTrial> trials[] = {
367 new FieldTrial("one", 100, "default", next_year_, 1, 1),
368 new FieldTrial("two", 100, "default", next_year_, 1, 1),
369 };
370
371 for (size_t i = 0; i < arraysize(trials); ++i) {
372 trials[i]->UseOneTimeRandomization();
373
374 for (int j = 0; j < 100; ++j) {
375 trials[i]->AppendGroup("", 1);
376 }
377 }
378
379 // The trials are most likely to give different results since they have
380 // different names.
381 ASSERT_NE(trials[0]->group(), trials[1]->group());
382 ASSERT_NE(trials[0]->group_name(), trials[1]->group_name());
383 }
384
385 TEST_F(FieldTrialTest, DisableImmediately) {
386 FieldTrial* trial =
387 new FieldTrial("trial", 100, "default", next_year_, 12, 31);
388 trial->Disable();
389 ASSERT_EQ("default", trial->group_name());
390 ASSERT_EQ(FieldTrial::kDefaultGroupNumber, trial->group());
391 }
392
393 TEST_F(FieldTrialTest, DisableAfterInitialization) {
394 FieldTrial* trial =
395 new FieldTrial("trial", 100, "default", next_year_, 12, 31);
396 trial->AppendGroup("non_default", 100);
397 ASSERT_EQ("non_default", trial->group_name());
398 trial->Disable();
399 ASSERT_EQ("default", trial->group_name());
jar (doing other things) 2011/04/21 22:10:02 Good tests. ;-)
Jói 2011/04/28 01:03:50 Thanks :)
400 }
401
298 } // namespace base 402 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698