File: metrics_apitest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (165 lines) | stat: -rw-r--r-- 5,955 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stddef.h>

#include <array>
#include <map>

#include "base/containers/span.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/statistics_recorder.h"
#include "base/test/metrics/user_action_tester.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "content/public/test/browser_test.h"

namespace extensions {

namespace {

// The tests that are run by this extension are expected to record the following
// user actions, with the specified counts.  If the tests in test.js are
// modified, this array may need to be updated.
constexpr struct RecordedUserAction {
  const char* name;
  int count;  // number of times the metric was recorded.
} g_user_actions[] = {
    {"test.ua.1", 1},
    {"test.ua.2", 2},
};

// The tests that are run by this extension are expected to record the following
// histograms.  If the tests in test.js are modified, this array may need to be
// updated.
constexpr struct RecordedHistogram {
  const char* name;
  base::HistogramType type;
  int min;
  int max;
  size_t buckets;
  int count;
} g_histograms[] = {
    {"test.h.1", base::HISTOGRAM, 1, 100, 50, 1},          // custom
    {"test.h.2", base::LINEAR_HISTOGRAM, 1, 200, 50, 1},   // custom
    {"test.h.3", base::LINEAR_HISTOGRAM, 1, 101, 102, 2},  // percentage
    {"test.sparse.1", base::SPARSE_HISTOGRAM, 0, 0, 0, 1},
    {"test.sparse.2", base::SPARSE_HISTOGRAM, 0, 0, 0, 2},
    {"test.sparse.3", base::SPARSE_HISTOGRAM, 0, 0, 0, 6},
    {"test.time", base::HISTOGRAM, 1, 10000, 50, 1},
    {"test.medium.time", base::HISTOGRAM, 1, 180000, 50, 1},
    {"test.long.time", base::HISTOGRAM, 1, 3600000, 50, 1},
    {"test.count", base::HISTOGRAM, 1, 1000000, 50, 1},
    {"test.medium.count", base::HISTOGRAM, 1, 10000, 50, 1},
    {"test.small.count", base::HISTOGRAM, 1, 100, 50, 1},
    {"test.bucketchange.linear", base::LINEAR_HISTOGRAM, 1, 100, 10, 2},
    {"test.bucketchange.log", base::HISTOGRAM, 1, 100, 10, 2},
};

// Represents a bucket in a sparse histogram.
struct Bucket {
  int histogram_value;
  int count;
};

// We expect the following sparse histograms.
constexpr struct SparseHistogram {
  const char* name;
  int bucket_count;
  std::array<Bucket, 3> buckets;
} g_sparse_histograms[] = {{"test.sparse.1", 1, {{{42, 1}}}},
                           {"test.sparse.2", 1, {{{24, 2}}}},
                           {"test.sparse.3", 3, {{{1, 1}, {2, 2}, {3, 3}}}}};

void ValidateUserActions(const base::UserActionTester& user_action_tester,
                         base::span<const RecordedUserAction> recorded) {
  for (const auto& ua : recorded) {
    EXPECT_EQ(ua.count, user_action_tester.GetActionCount(ua.name));
  }
}

void ValidateSparseHistogramSamples(
    const std::string& name,
    const base::HistogramSamples& samples) {
  for (const auto& sparse_histogram : g_sparse_histograms) {
    if (std::string(name) == sparse_histogram.name) {
      for (int j = 0; j < sparse_histogram.bucket_count; ++j) {
        const Bucket& bucket = sparse_histogram.buckets[j];
        EXPECT_EQ(bucket.count, samples.GetCount(bucket.histogram_value));
      }
    }
  }
}

void ValidateHistograms(base::span<const RecordedHistogram> recorded) {
  const base::StatisticsRecorder::Histograms histograms =
      base::StatisticsRecorder::GetHistograms();

  // Code other than the tests tun here will record some histogram values, but
  // we will ignore those. This function validates that all the histogram we
  // expect to see are present in the list, and that their basic info is
  // correct.
  for (const auto& r : recorded) {
    size_t j = 0;
    for (j = 0; j < histograms.size(); ++j) {
      base::HistogramBase* histogram(histograms[j]);
      if (std::string(r.name) == histogram->histogram_name()) {
        std::unique_ptr<base::HistogramSamples> snapshot =
            histogram->SnapshotSamples();
        base::HistogramBase::Count32 sample_count = snapshot->TotalCount();
        EXPECT_EQ(r.count, sample_count);

        EXPECT_EQ(r.type, histogram->GetHistogramType());
        if (r.type == base::SPARSE_HISTOGRAM) {
          ValidateSparseHistogramSamples(r.name, *snapshot);
        } else {
          EXPECT_TRUE(
              histogram->HasConstructionArguments(r.min, r.max, r.buckets));
        }
        break;
      }
    }
    EXPECT_LT(j, histograms.size());
  }
}

}  // namespace

using ContextType = extensions::browser_test_util::ContextType;

class ExtensionMetricsApiTest
    : public ExtensionApiTest,
      public testing::WithParamInterface<ContextType> {
 public:
  ExtensionMetricsApiTest() : ExtensionApiTest(GetParam()) {}
  ~ExtensionMetricsApiTest() override = default;
  ExtensionMetricsApiTest(const ExtensionMetricsApiTest&) = delete;
  ExtensionMetricsApiTest& operator=(const ExtensionMetricsApiTest&) = delete;
};

INSTANTIATE_TEST_SUITE_P(PersistentBackground,
                         ExtensionMetricsApiTest,
                         ::testing::Values(ContextType::kPersistentBackground));

INSTANTIATE_TEST_SUITE_P(ServiceWorker,
                         ExtensionMetricsApiTest,
                         ::testing::Values(ContextType::kServiceWorker));

IN_PROC_BROWSER_TEST_P(ExtensionMetricsApiTest, Metrics) {
  base::UserActionTester user_action_tester;

  base::FieldTrialList::CreateFieldTrial("apitestfieldtrial2", "group1");

  ASSERT_TRUE(base::AssociateFieldTrialParams("apitestfieldtrial2", "group1",
                                              {{"a", "aa"}, {"b", "bb"}}));

  ASSERT_TRUE(RunExtensionTest("metrics", {}, {.load_as_component = true}))
      << message_;

  ValidateUserActions(user_action_tester, g_user_actions);
  ValidateHistograms(g_histograms);
}

}  // namespace extensions