field_trials_provider_unittest.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright 2017 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. #include "components/metrics/field_trials_provider.h"
  5. #include "base/metrics/field_trial.h"
  6. #include "base/threading/platform_thread.h"
  7. #include "components/variations/active_field_trials.h"
  8. #include "components/variations/synthetic_trial_registry.h"
  9. #include "components/variations/synthetic_trials.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. #include "third_party/metrics_proto/system_profile.pb.h"
  12. using ActiveGroup = base::FieldTrial::ActiveGroup;
  13. namespace variations {
  14. namespace {
  15. constexpr const char* kSuffix = "UKM";
  16. const ActiveGroup kFieldTrials[] = {{"Trial1", "Group1"},
  17. {"Trial2", "Group2"},
  18. {"Trial3", "Group3"}};
  19. const ActiveGroup kSyntheticFieldTrials[] = {{"Synthetic1", "SyntheticGroup1"},
  20. {"Synthetic2", "SyntheticGroup2"}};
  21. ActiveGroupId ToActiveGroupId(ActiveGroup active_group,
  22. std::string suffix = "");
  23. const ActiveGroupId kFieldTrialIds[] = {ToActiveGroupId(kFieldTrials[0]),
  24. ToActiveGroupId(kFieldTrials[1]),
  25. ToActiveGroupId(kFieldTrials[2])};
  26. const ActiveGroupId kAllTrialIds[] = {
  27. ToActiveGroupId(kFieldTrials[0]), ToActiveGroupId(kFieldTrials[1]),
  28. ToActiveGroupId(kFieldTrials[2]), ToActiveGroupId(kSyntheticFieldTrials[0]),
  29. ToActiveGroupId(kSyntheticFieldTrials[1])};
  30. const ActiveGroupId kAllTrialIdsWithSuffixes[] = {
  31. ToActiveGroupId(kFieldTrials[0], kSuffix),
  32. ToActiveGroupId(kFieldTrials[1], kSuffix),
  33. ToActiveGroupId(kFieldTrials[2], kSuffix),
  34. ToActiveGroupId(kSyntheticFieldTrials[0], kSuffix),
  35. ToActiveGroupId(kSyntheticFieldTrials[1], kSuffix)};
  36. // Check that the field trials in |system_profile| correspond to |expected|.
  37. void CheckFieldTrialsInSystemProfile(
  38. const metrics::SystemProfileProto& system_profile,
  39. const ActiveGroupId* expected) {
  40. for (int i = 0; i < system_profile.field_trial_size(); ++i) {
  41. const metrics::SystemProfileProto::FieldTrial& field_trial =
  42. system_profile.field_trial(i);
  43. EXPECT_EQ(expected[i].name, field_trial.name_id());
  44. EXPECT_EQ(expected[i].group, field_trial.group_id());
  45. }
  46. }
  47. ActiveGroupId ToActiveGroupId(ActiveGroup active_group, std::string suffix) {
  48. return MakeActiveGroupId(active_group.trial_name + suffix,
  49. active_group.group_name + suffix);
  50. }
  51. } // namespace
  52. class FieldTrialsProviderTest : public ::testing::Test {
  53. public:
  54. FieldTrialsProviderTest() = default;
  55. ~FieldTrialsProviderTest() override = default;
  56. protected:
  57. void SetUp() override {
  58. // Register the field trials.
  59. for (const ActiveGroup& trial : kFieldTrials) {
  60. base::FieldTrial* field_trial = base::FieldTrialList::CreateFieldTrial(
  61. trial.trial_name, trial.group_name);
  62. // Call group() to finalize and mark the field trial as active.
  63. field_trial->group();
  64. }
  65. }
  66. // Register trials which should get recorded.
  67. void RegisterExpectedSyntheticTrials() {
  68. for (const ActiveGroup& trial : kSyntheticFieldTrials) {
  69. registry_.RegisterSyntheticFieldTrial(SyntheticTrialGroup(
  70. trial.trial_name, trial.group_name,
  71. /*annotation_mode=*/
  72. variations::SyntheticTrialAnnotationMode::kNextLog));
  73. }
  74. }
  75. // Register trial which shouldn't get recorded.
  76. void RegisterExtraSyntheticTrial() {
  77. registry_.RegisterSyntheticFieldTrial(SyntheticTrialGroup(
  78. "ExtraSynthetic", "ExtraGroup",
  79. /*annotation_mode=*/
  80. variations::SyntheticTrialAnnotationMode::kNextLog));
  81. }
  82. // Waits until base::TimeTicks::Now() no longer equals |value|. This should
  83. // take between 1-15ms per the documented resolution of base::TimeTicks.
  84. void WaitUntilTimeChanges(const base::TimeTicks& value) {
  85. while (base::TimeTicks::Now() == value) {
  86. base::PlatformThread::Sleep(base::Milliseconds(1));
  87. }
  88. }
  89. SyntheticTrialRegistry registry_;
  90. };
  91. TEST_F(FieldTrialsProviderTest, ProvideSyntheticTrials) {
  92. FieldTrialsProvider provider(&registry_, base::StringPiece());
  93. RegisterExpectedSyntheticTrials();
  94. // Make sure these trials are older than the log.
  95. WaitUntilTimeChanges(base::TimeTicks::Now());
  96. // Get the current time and wait for it to change.
  97. base::TimeTicks log_creation_time = base::TimeTicks::Now();
  98. // Make sure that the log is older than the trials that should be excluded.
  99. WaitUntilTimeChanges(log_creation_time);
  100. RegisterExtraSyntheticTrial();
  101. metrics::SystemProfileProto proto;
  102. provider.ProvideSystemProfileMetricsWithLogCreationTime(log_creation_time,
  103. &proto);
  104. EXPECT_EQ(std::size(kAllTrialIds),
  105. static_cast<size_t>(proto.field_trial_size()));
  106. CheckFieldTrialsInSystemProfile(proto, kAllTrialIds);
  107. }
  108. TEST_F(FieldTrialsProviderTest, NoSyntheticTrials) {
  109. FieldTrialsProvider provider(nullptr, base::StringPiece());
  110. metrics::SystemProfileProto proto;
  111. provider.ProvideSystemProfileMetricsWithLogCreationTime(base::TimeTicks(),
  112. &proto);
  113. EXPECT_EQ(std::size(kFieldTrialIds),
  114. static_cast<size_t>(proto.field_trial_size()));
  115. CheckFieldTrialsInSystemProfile(proto, kFieldTrialIds);
  116. }
  117. TEST_F(FieldTrialsProviderTest, ProvideCurrentSessionData) {
  118. metrics::ChromeUserMetricsExtension uma_log;
  119. uma_log.system_profile();
  120. // {1, 1} should not be in the resulting proto as ProvideCurrentSessionData()
  121. // clears existing trials and sets the trials to be those determined by
  122. // GetSyntheticFieldTrialsOlderThan() and GetFieldTrialIds().
  123. metrics::SystemProfileProto::FieldTrial* trial =
  124. uma_log.mutable_system_profile()->add_field_trial();
  125. trial->set_name_id(1);
  126. trial->set_group_id(1);
  127. FieldTrialsProvider provider(&registry_, base::StringPiece());
  128. RegisterExpectedSyntheticTrials();
  129. WaitUntilTimeChanges(base::TimeTicks::Now());
  130. provider.SetLogCreationTimeForTesting(base::TimeTicks::Now());
  131. provider.ProvideCurrentSessionData(&uma_log);
  132. EXPECT_EQ(std::size(kAllTrialIds),
  133. static_cast<size_t>(uma_log.system_profile().field_trial_size()));
  134. CheckFieldTrialsInSystemProfile(uma_log.system_profile(), kAllTrialIds);
  135. }
  136. TEST_F(FieldTrialsProviderTest, GetAndWriteFieldTrialsWithSuffixes) {
  137. metrics::ChromeUserMetricsExtension uma_log;
  138. uma_log.system_profile();
  139. FieldTrialsProvider provider(&registry_, kSuffix);
  140. RegisterExpectedSyntheticTrials();
  141. WaitUntilTimeChanges(base::TimeTicks::Now());
  142. provider.SetLogCreationTimeForTesting(base::TimeTicks::Now());
  143. provider.ProvideCurrentSessionData(&uma_log);
  144. EXPECT_EQ(std::size(kAllTrialIdsWithSuffixes),
  145. static_cast<size_t>(uma_log.system_profile().field_trial_size()));
  146. CheckFieldTrialsInSystemProfile(uma_log.system_profile(),
  147. kAllTrialIdsWithSuffixes);
  148. }
  149. } // namespace variations