File: translate_metrics_unittest.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 (212 lines) | stat: -rw-r--r-- 7,935 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/translate/core/common/translate_metrics.h"

#include <memory>

#include "base/metrics/histogram.h"
#include "base/metrics/histogram_samples.h"
#include "base/metrics/metrics_hashes.h"
#include "base/metrics/statistics_recorder.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"

using base::HistogramBase;
using base::HistogramSamples;
using base::SampleCountIterator;
using base::StatisticsRecorder;
using base::TimeTicks;

namespace translate {
namespace {

class MetricsRecorder {
 public:
  explicit MetricsRecorder(const char* key) : key_(key) {
    HistogramBase* histogram = StatisticsRecorder::FindHistogram(key_);
    if (histogram)
      base_samples_ = histogram->SnapshotSamples();
  }

  MetricsRecorder(const MetricsRecorder&) = delete;
  MetricsRecorder& operator=(const MetricsRecorder&) = delete;

  void CheckLanguageVerification(int expected_model_only,
                                 int expected_unknown,
                                 int expected_model_agree,
                                 int expected_model_disagree,
                                 int expected_trust_model,
                                 int expected_model_complement_sub_code,
                                 int expected_no_page_content,
                                 int expected_model_not_available) {
    ASSERT_EQ(metrics_internal::kTranslateLanguageDetectionLanguageVerification,
              key_);

    Snapshot();

    // EXPECT_EQ(expected_model_disabled,
    //           GetCountWithoutSnapshot(kModelDisabled)); -- obsolete
    EXPECT_EQ(expected_model_only, GetCountWithoutSnapshot(static_cast<int>(
                                       LanguageVerificationType::kModelOnly)));
    EXPECT_EQ(expected_unknown, GetCountWithoutSnapshot(static_cast<int>(
                                    LanguageVerificationType::kModelUnknown)));
    EXPECT_EQ(expected_model_agree,
              GetCountWithoutSnapshot(
                  static_cast<int>(LanguageVerificationType::kModelAgrees)));
    EXPECT_EQ(expected_model_disagree,
              GetCountWithoutSnapshot(
                  static_cast<int>(LanguageVerificationType::kModelDisagrees)));
    EXPECT_EQ(expected_trust_model,
              GetCountWithoutSnapshot(
                  static_cast<int>(LanguageVerificationType::kModelOverrides)));
    EXPECT_EQ(expected_model_complement_sub_code,
              GetCountWithoutSnapshot(static_cast<int>(
                  LanguageVerificationType::kModelComplementsCountry)));
    EXPECT_EQ(expected_no_page_content,
              GetCountWithoutSnapshot(
                  static_cast<int>(LanguageVerificationType::kNoPageContent)));
    EXPECT_EQ(expected_model_not_available,
              GetCountWithoutSnapshot(static_cast<int>(
                  LanguageVerificationType::kModelNotAvailable)));
  }

  void CheckTotalCount(int count) {
    Snapshot();
    EXPECT_EQ(count, GetTotalCount());
  }

  void CheckCount(HistogramBase::Sample32 value, int expected) {
    if (!samples_) {
      Snapshot();
    }
    EXPECT_EQ(expected, GetCountWithoutSnapshot(value));
  }

  void CheckValueInLogs(double value) {
    Snapshot();
    ASSERT_TRUE(samples_.get());
    for (std::unique_ptr<SampleCountIterator> i = samples_->Iterator();
         !i->Done(); i->Next()) {
      HistogramBase::Sample32 min;
      int64_t max;
      HistogramBase::Count32 count;
      i->Get(&min, &max, &count);
      if (min <= value && value <= max && count >= 1)
        return;
    }
    EXPECT_FALSE(true);
  }

 private:
  void Snapshot() {
    HistogramBase* histogram = StatisticsRecorder::FindHistogram(key_);
    if (!histogram)
      return;
    samples_ = histogram->SnapshotSamples();
  }

  HistogramBase::Count32 GetCountWithoutSnapshot(HistogramBase::Sample32 value) {
    if (!samples_)
      return 0;
    HistogramBase::Count32 count = samples_->GetCount(value);
    if (!base_samples_)
      return count;
    return count - base_samples_->GetCount(value);
  }

  HistogramBase::Count32 GetTotalCount() {
    if (!samples_)
      return 0;
    HistogramBase::Count32 count = samples_->TotalCount();
    if (!base_samples_)
      return count;
    return count - base_samples_->TotalCount();
  }

  std::string key_;
  std::unique_ptr<HistogramSamples> base_samples_;
  std::unique_ptr<HistogramSamples> samples_;
};

TEST(TranslateMetricsTest, ReportLanguageVerification) {
  MetricsRecorder recorder(
      metrics_internal::kTranslateLanguageDetectionLanguageVerification);

  // ReportLanguageVerification(kModelDisabled); -- obsolete
  recorder.CheckLanguageVerification(0, 0, 0, 0, 0, 0, 0, 0);
  ReportLanguageVerification(LanguageVerificationType::kModelOnly);
  recorder.CheckLanguageVerification(1, 0, 0, 0, 0, 0, 0, 0);
  ReportLanguageVerification(LanguageVerificationType::kModelUnknown);
  recorder.CheckLanguageVerification(1, 1, 0, 0, 0, 0, 0, 0);
  ReportLanguageVerification(LanguageVerificationType::kModelAgrees);
  recorder.CheckLanguageVerification(1, 1, 1, 0, 0, 0, 0, 0);
  ReportLanguageVerification(LanguageVerificationType::kModelDisagrees);
  recorder.CheckLanguageVerification(1, 1, 1, 1, 0, 0, 0, 0);
  ReportLanguageVerification(LanguageVerificationType::kModelOverrides);
  recorder.CheckLanguageVerification(1, 1, 1, 1, 1, 0, 0, 0);
  ReportLanguageVerification(
      LanguageVerificationType::kModelComplementsCountry);
  recorder.CheckLanguageVerification(1, 1, 1, 1, 1, 1, 0, 0);
  ReportLanguageVerification(LanguageVerificationType::kNoPageContent);
  recorder.CheckLanguageVerification(1, 1, 1, 1, 1, 1, 1, 0);
  ReportLanguageVerification(LanguageVerificationType::kModelNotAvailable);
  recorder.CheckLanguageVerification(1, 1, 1, 1, 1, 1, 1, 1);
}

TEST(TranslateMetricsTest, ReportTimeToBeReady) {
  MetricsRecorder recorder(metrics_internal::kTranslateTimeToBeReady);
  recorder.CheckTotalCount(0);
  ReportTimeToBeReady(3.14);
  recorder.CheckValueInLogs(3.14);
  recorder.CheckTotalCount(1);
}

TEST(TranslateMetricsTest, ReportTimeToLoad) {
  MetricsRecorder recorder(metrics_internal::kTranslateTimeToLoad);
  recorder.CheckTotalCount(0);
  ReportTimeToLoad(573.0);
  recorder.CheckValueInLogs(573.0);
  recorder.CheckTotalCount(1);
}

TEST(TranslateMetricsTest, ReportTimeToTranslate) {
  MetricsRecorder recorder(metrics_internal::kTranslateTimeToTranslate);
  recorder.CheckTotalCount(0);
  ReportTimeToTranslate(4649.0);
  recorder.CheckValueInLogs(4649.0);
  recorder.CheckTotalCount(1);
}

TEST(TranslateMetricsTest, ReportTranslatedLanguageDetectionContentLength) {
  MetricsRecorder recorder(
      metrics_internal::kTranslatedLanguageDetectionContentLength);
  recorder.CheckTotalCount(0);
  ReportTranslatedLanguageDetectionContentLength(12345);
  recorder.CheckValueInLogs(12345);
  recorder.CheckTotalCount(1);
}

TEST(TranslateMetricsTest, ReportCompactInfobarEvent) {
  MetricsRecorder recorder(metrics_internal::kTranslateCompactInfobarEvent);
  recorder.CheckTotalCount(0);
  ReportCompactInfobarEvent(InfobarEvent::INFOBAR_IMPRESSION);
  ReportCompactInfobarEvent(InfobarEvent::INFOBAR_IMPRESSION);
  ReportCompactInfobarEvent(InfobarEvent::INFOBAR_IMPRESSION);
  ReportCompactInfobarEvent(InfobarEvent::INFOBAR_REVERT);
  ReportCompactInfobarEvent(InfobarEvent::INFOBAR_REVERT);
  ReportCompactInfobarEvent(
      InfobarEvent::INFOBAR_SNACKBAR_AUTO_ALWAYS_IMPRESSION);

  recorder.CheckTotalCount(6);
  recorder.CheckCount(static_cast<int>(InfobarEvent::INFOBAR_IMPRESSION), 3);
  recorder.CheckCount(static_cast<int>(InfobarEvent::INFOBAR_REVERT), 2);
  recorder.CheckCount(
      static_cast<int>(InfobarEvent::INFOBAR_SNACKBAR_AUTO_ALWAYS_IMPRESSION),
      1);
}

}  // namespace
}  // namespace translate