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

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

Issue 6931048: Revert 84197 - Add one-time randomization support for FieldTrial, and the ability to (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: 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"
10 #include "base/stringprintf.h" 9 #include "base/stringprintf.h"
11 #include "base/string_number_conversions.h"
12 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
13 11
14 #include <limits>
15
16 namespace base { 12 namespace base {
17 13
18 class FieldTrialTest : public testing::Test { 14 class FieldTrialTest : public testing::Test {
19 public: 15 public:
20 FieldTrialTest() : trial_list_("client_id") { 16 FieldTrialTest() : trial_list_() {
21 Time now = Time::NowFromSystemTime(); 17 Time now = Time::NowFromSystemTime();
22 TimeDelta oneYear = TimeDelta::FromDays(365); 18 TimeDelta oneYear = TimeDelta::FromDays(365);
23 Time::Exploded exploded; 19 Time::Exploded exploded;
24 20
25 Time next_year_time = now + oneYear; 21 Time next_year_time = now + oneYear;
26 next_year_time.LocalExplode(&exploded); 22 next_year_time.LocalExplode(&exploded);
27 next_year_ = exploded.year; 23 next_year_ = exploded.year;
28 24
29 Time last_year_time = now - oneYear; 25 Time last_year_time = now - oneYear;
30 last_year_time.LocalExplode(&exploded); 26 last_year_time.LocalExplode(&exploded);
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 EXPECT_EQ(default_group_name, trial->group_name()); 217 EXPECT_EQ(default_group_name, trial->group_name());
222 } 218 }
223 219
224 TEST_F(FieldTrialTest, Save) { 220 TEST_F(FieldTrialTest, Save) {
225 std::string save_string; 221 std::string save_string;
226 222
227 FieldTrial* trial = 223 FieldTrial* trial =
228 new FieldTrial( 224 new FieldTrial(
229 "Some name", 10, "Default some name", next_year_, 12, 31); 225 "Some name", 10, "Default some name", next_year_, 12, 31);
230 // There is no winner yet, so no textual group name is associated with trial. 226 // There is no winner yet, so no textual group name is associated with trial.
231 // In this case, the trial should not be included.
232 EXPECT_EQ("", trial->group_name_internal()); 227 EXPECT_EQ("", trial->group_name_internal());
233 FieldTrialList::StatesToString(&save_string); 228 FieldTrialList::StatesToString(&save_string);
234 EXPECT_EQ("", save_string); 229 EXPECT_EQ("Some name/Default some name/", save_string);
235 save_string.clear(); 230 save_string.clear();
236 231
237 // Create a winning group. 232 // Create a winning group.
238 trial->AppendGroup("Winner", 10); 233 trial->AppendGroup("Winner", 10);
239 FieldTrialList::StatesToString(&save_string); 234 FieldTrialList::StatesToString(&save_string);
240 EXPECT_EQ("Some name/Winner/", save_string); 235 EXPECT_EQ("Some name/Winner/", save_string);
241 save_string.clear(); 236 save_string.clear();
242 237
243 // Create a second trial and winning group. 238 // Create a second trial and winning group.
244 FieldTrial* trial2 = 239 FieldTrial* trial2 =
(...skipping 18 matching lines...) Expand all
263 258
264 trial = FieldTrialList::Find("xxx"); 259 trial = FieldTrialList::Find("xxx");
265 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial); 260 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
266 EXPECT_EQ("yyyy", trial->group_name()); 261 EXPECT_EQ("yyyy", trial->group_name());
267 EXPECT_EQ("xxx", trial->name()); 262 EXPECT_EQ("xxx", trial->name());
268 } 263 }
269 264
270 TEST_F(FieldTrialTest, BogusRestore) { 265 TEST_F(FieldTrialTest, BogusRestore) {
271 EXPECT_FALSE(FieldTrialList::CreateTrialsInChildProcess("MissingSlash")); 266 EXPECT_FALSE(FieldTrialList::CreateTrialsInChildProcess("MissingSlash"));
272 EXPECT_FALSE(FieldTrialList::CreateTrialsInChildProcess("MissingGroupName/")); 267 EXPECT_FALSE(FieldTrialList::CreateTrialsInChildProcess("MissingGroupName/"));
273 EXPECT_FALSE(FieldTrialList::CreateTrialsInChildProcess( 268 EXPECT_FALSE(
274 "MissingFinalSlash/gname")); 269 FieldTrialList::CreateTrialsInChildProcess("MissingFinalSlash/gname"));
275 EXPECT_FALSE(FieldTrialList::CreateTrialsInChildProcess( 270 EXPECT_FALSE(
276 "noname, only group/")); 271 FieldTrialList::CreateTrialsInChildProcess("/noname, only group/"));
277 } 272 }
278 273
279 TEST_F(FieldTrialTest, DuplicateRestore) { 274 TEST_F(FieldTrialTest, DuplicateRestore) {
280 FieldTrial* trial = 275 FieldTrial* trial =
281 new FieldTrial( 276 new FieldTrial(
282 "Some name", 10, "Default some name", next_year_, 12, 31); 277 "Some name", 10, "Default some name", next_year_, 12, 31);
283 trial->AppendGroup("Winner", 10); 278 trial->AppendGroup("Winner", 10);
284 std::string save_string; 279 std::string save_string;
285 FieldTrialList::StatesToString(&save_string); 280 FieldTrialList::StatesToString(&save_string);
286 EXPECT_EQ("Some name/Winner/", save_string); 281 EXPECT_EQ("Some name/Winner/", save_string);
287 282
288 // It is OK if we redundantly specify a winner. 283 // It is OK if we redundantly specify a winner.
289 EXPECT_TRUE(FieldTrialList::CreateTrialsInChildProcess(save_string)); 284 EXPECT_TRUE(FieldTrialList::CreateTrialsInChildProcess(save_string));
290 285
291 // But it is an error to try to change to a different winner. 286 // But it is an error to try to change to a different winner.
292 EXPECT_FALSE(FieldTrialList::CreateTrialsInChildProcess( 287 EXPECT_FALSE(FieldTrialList::CreateTrialsInChildProcess("Some name/Loser/"));
293 "Some name/Loser/"));
294 } 288 }
295 289
296 TEST_F(FieldTrialTest, CreateFieldTrial) { 290 TEST_F(FieldTrialTest, CreateFieldTrial) {
297 EXPECT_TRUE(FieldTrialList::Find("Some_name") == NULL); 291 EXPECT_TRUE(FieldTrialList::Find("Some_name") == NULL);
298 292
299 FieldTrialList::CreateFieldTrial("Some_name", "Winner"); 293 FieldTrialList::CreateFieldTrial("Some_name", "Winner");
300 294
301 FieldTrial* trial = FieldTrialList::Find("Some_name"); 295 FieldTrial* trial = FieldTrialList::Find("Some_name");
302 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial); 296 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
303 EXPECT_EQ("Winner", trial->group_name()); 297 EXPECT_EQ("Winner", trial->group_name());
(...skipping 16 matching lines...) Expand all
320 } 314 }
321 315
322 TEST_F(FieldTrialTest, MakeName) { 316 TEST_F(FieldTrialTest, MakeName) {
323 FieldTrial* trial = 317 FieldTrial* trial =
324 new FieldTrial("Field Trial", 10, "Winner", next_year_, 12, 31); 318 new FieldTrial("Field Trial", 10, "Winner", next_year_, 12, 31);
325 trial->group(); 319 trial->group();
326 EXPECT_EQ("Histogram_Winner", 320 EXPECT_EQ("Histogram_Winner",
327 FieldTrial::MakeName("Histogram", "Field Trial")); 321 FieldTrial::MakeName("Histogram", "Field Trial"));
328 } 322 }
329 323
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
435 } // namespace base 324 } // 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