1// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/common/metrics/variations/uniformity_field_trials.h"
6
7#include "base/metrics/field_trial.h"
8#include "base/strings/stringprintf.h"
9#include "base/time/time.h"
10#include "chrome/common/metrics/variations/variations_util.h"
11
12namespace chrome_variations {
13
14namespace {
15
16// Set up a uniformity field trial. |one_time_randomized| indicates if the
17// field trial is one-time randomized or session-randomized. |trial_name_string|
18// must contain a "%d" since the percentage of the group will be inserted in
19// the trial name. |num_trial_groups| must be a divisor of 100 (e.g. 5, 20)
20void SetupSingleUniformityFieldTrial(
21    base::FieldTrial::RandomizationType randomization_type,
22    const std::string& trial_name_string,
23    const VariationID trial_base_id,
24    int num_trial_groups) {
25  // Probability per group remains constant for all uniformity trials, what
26  // changes is the probability divisor.
27  static const base::FieldTrial::Probability kProbabilityPerGroup = 1;
28  const std::string kDefaultGroupName = "default";
29  const base::FieldTrial::Probability divisor = num_trial_groups;
30
31  DCHECK_EQ(100 % num_trial_groups, 0);
32  const int group_percent = 100 / num_trial_groups;
33  const std::string trial_name = base::StringPrintf(trial_name_string.c_str(),
34                                                    group_percent);
35
36  DVLOG(1) << "Trial name = " << trial_name;
37
38  scoped_refptr<base::FieldTrial> trial(
39      base::FieldTrialList::FactoryGetFieldTrial(
40          trial_name, divisor, kDefaultGroupName, 2015, 1, 1,
41          randomization_type, NULL));
42  AssociateGoogleVariationID(GOOGLE_UPDATE_SERVICE, trial_name,
43                             kDefaultGroupName, trial_base_id);
44
45  // Loop starts with group 1 because the field trial automatically creates a
46  // default group, which would be group 0.
47  for (int group_number = 1; group_number < num_trial_groups; ++group_number) {
48    const std::string group_name =
49          base::StringPrintf("group_%02d", group_number);
50    DVLOG(1) << "    Group name = " << group_name;
51    trial->AppendGroup(group_name, kProbabilityPerGroup);
52    AssociateGoogleVariationID(
53        GOOGLE_UPDATE_SERVICE, trial_name, group_name,
54        static_cast<VariationID>(trial_base_id + group_number));
55  }
56
57  // Now that all groups have been appended, call group() on the trial to
58  // ensure that our trial is registered. This resolves an off-by-one issue
59  // where the default group never gets chosen if we don't "use" the trial.
60  const int chosen_group = trial->group();
61  DVLOG(1) << "Chosen Group: " << chosen_group;
62}
63
64// Setup a 50% uniformity trial for new installs only. This is accomplished by
65// disabling the trial on clients that were installed before a specified date.
66void SetupNewInstallUniformityTrial(const base::Time install_date) {
67  const base::Time::Exploded kStartDate = {
68    2012, 11, 0, 6,  // Nov 6, 2012
69    0, 0, 0, 0       // 00:00:00.000
70  };
71  scoped_refptr<base::FieldTrial> trial(
72      base::FieldTrialList::FactoryGetFieldTrial(
73          "UMA-New-Install-Uniformity-Trial", 100, "Disabled",
74          2015, 1, 1, base::FieldTrial::ONE_TIME_RANDOMIZED, NULL));
75  trial->AppendGroup("Control", 50);
76  trial->AppendGroup("Experiment", 50);
77  const base::Time start_date = base::Time::FromLocalExploded(kStartDate);
78  if (install_date < start_date)
79    trial->Disable();
80  else
81    trial->group();
82}
83
84}  // namespace
85
86void SetupUniformityFieldTrials(const base::Time install_date) {
87  // The 100 percent field trial in which everyone is a member is a special
88  // case. It is useful to create as it permits viewing all UMA data in UIs
89  // and tools that require a field trial.
90  base::FieldTrial* trial =
91      base::FieldTrialList::CreateFieldTrial("UMA-Uniformity-Trial-100-Percent",
92                                             "group_01");
93  // Call |group()| on the trial to ensure its reported in metrics.
94  trial->group();
95
96  // One field trial will be created for each entry in this array. The i'th
97  // field trial will have |trial_sizes[i]| groups in it, including the default
98  // group. Each group will have a probability of 1/|trial_sizes[i]|.
99  const int num_trial_groups[] = { 100, 20, 10, 5, 2 };
100
101  // Declare our variation ID bases along side this array so we can loop over it
102  // and assign the IDs appropriately. So for example, the 1 percent experiments
103  // should have a size of 100 (100/100 = 1).
104  const VariationID trial_base_ids[] = {
105    UNIFORMITY_1_PERCENT_BASE,
106    UNIFORMITY_5_PERCENT_BASE,
107    UNIFORMITY_10_PERCENT_BASE,
108    UNIFORMITY_20_PERCENT_BASE,
109    UNIFORMITY_50_PERCENT_BASE
110  };
111
112  const std::string kOneTimeRandomizedTrialName =
113      "UMA-Uniformity-Trial-%d-Percent";
114  for (size_t i = 0; i < arraysize(num_trial_groups); ++i) {
115    SetupSingleUniformityFieldTrial(base::FieldTrial::ONE_TIME_RANDOMIZED,
116                                    kOneTimeRandomizedTrialName,
117                                    trial_base_ids[i], num_trial_groups[i]);
118  }
119
120  // Setup a 5% session-randomized uniformity trial.
121  const std::string kSessionRandomizedTrialName =
122      "UMA-Session-Randomized-Uniformity-Trial-%d-Percent";
123  SetupSingleUniformityFieldTrial(
124      base::FieldTrial::SESSION_RANDOMIZED, kSessionRandomizedTrialName,
125      UNIFORMITY_SESSION_RANDOMIZED_5_PERCENT_BASE, 20);
126
127  SetupNewInstallUniformityTrial(install_date);
128}
129
130}  // namespace chrome_variations
131