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