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