File: external_metrics_integration_test.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 (90 lines) | stat: -rw-r--r-- 3,185 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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/metrics/histogram_base.h"
#include "base/metrics/statistics_recorder.h"
#include "base/process/launch.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "chrome/browser/ash/external_metrics/external_metrics.h"
#include "chrome/test/base/chromeos/crosier/ash_integration_test.h"

namespace ash {
namespace {

// The metrics_client binary on ChromeOS incorporates libmetrics and allows us
// to report metrics via its command line. This binary is normally in /usr/bin
// and should be on the path so we don't qualify the directory.
const char kMetricsClientBinaryName[] = "metrics_client";

// Wait up to this long for the metrics client to complete running.
constexpr int kMetricsClientTimeoutSec = 30;

const char kTestHistogramName[] = "ExternalMetricsIntegrationTest.Histogram";

class ExternalMetricsIntegrationTest : public AshIntegrationTest {
 public:
  void ReportMetrics(int num_samples, int value, int max) {
    std::vector<std::string> args{kMetricsClientBinaryName};
    if (num_samples > 1) {
      args.push_back("-n");
      args.push_back(base::NumberToString(num_samples));
    }
    args.push_back(kTestHistogramName);
    args.push_back("-e");
    args.push_back(base::NumberToString(value));
    args.push_back(base::NumberToString(max));

    base::LaunchOptions launch_opts;
    base::Process metrics_client = base::LaunchProcess(args, launch_opts);
    ASSERT_TRUE(metrics_client.IsValid());

    int exit_code = 0;
    ASSERT_TRUE(metrics_client.WaitForExitWithTimeout(
        base::Seconds(kMetricsClientTimeoutSec), &exit_code));
    ASSERT_EQ(0, exit_code);
  }

  void SyncHistograms() {
    ExternalMetrics* metrics = ExternalMetrics::Get();
    ASSERT_TRUE(metrics) << "BrowserMain should have set up ExternalMetrics";

    metrics->CollectNowForTesting();

    // ExternalMetrics above will have pushed the histograms into the metrics
    // system so they should be ready to use. If we need metrics from child
    // processes like renderers, we would have to call
    // FetchHistogramsAsynchronously() and
    // base::StatisticsRecorder::ImportProvidedHistogramsSync() here.
  }
};

}  // namespace

// Integration test between histograms reported by libmetrics in ChromeOS and
// Ash's importer of these metrics.
IN_PROC_BROWSER_TEST_F(ExternalMetricsIntegrationTest, SyncHistograms) {
  constexpr int kSample1 = 1;
  constexpr int kCount1 = 1;

  constexpr int kSample2 = 2;
  constexpr int kCount2 = 10;

  constexpr int kMax = 5;

  // Must be created first because this class tracks *changes* in histograms.
  base::HistogramTester tester;

  ReportMetrics(kCount1, kSample1, kMax);
  SyncHistograms();
  tester.ExpectBucketCount(kTestHistogramName, kSample1, kCount1);
  // Shouldn't have any "sample 2" yet.
  tester.ExpectBucketCount(kTestHistogramName, kSample2, 0);

  ReportMetrics(kCount2, kSample2, kMax);
  SyncHistograms();
  tester.ExpectBucketCount(kTestHistogramName, kSample2, kCount2);
}

}  // namespace ash