File: uma_metrics_table_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 (310 lines) | stat: -rw-r--r-- 11,831 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Copyright 2024 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/segmentation_platform/internal/database/uma_metrics_table.h"

#include <memory>

#include "base/rand_util.h"
#include "components/segmentation_platform/internal/database/ukm_database_test_utils.h"
#include "components/segmentation_platform/internal/database/ukm_types.h"
#include "sql/database.h"
#include "sql/statement.h"
#include "sql/test/test_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace segmentation_platform {

namespace {

constexpr char kProfileId[] = "123";
constexpr char kProfileIdOther[] = "1234";
constexpr char kInvalidProfileId[] = "";

UmaMetricEntry GetSampleMetricsRow() {
  return UmaMetricEntry{.type = proto::SignalType::HISTOGRAM_VALUE,
                        .name_hash = 10,
                        .time = base::Time::Now(),
                        .value = 100};
}

}  // namespace

class UmaMetricsTableTest : public testing::Test {
 public:
  UmaMetricsTableTest() = default;
  ~UmaMetricsTableTest() override = default;

  void SetUp() override {
    sql::DatabaseOptions options;
    db_ = std::make_unique<sql::Database>(options, sql::test::kTestTag);
    bool opened = db_->OpenInMemory();
    ASSERT_TRUE(opened);
    metrics_table_ = std::make_unique<UmaMetricsTable>(db_.get());
  }

  void TearDown() override {
    metrics_table_.reset();
    db_.reset();
  }

  UmaMetricsTable& metrics_table() { return *metrics_table_; }

  sql::Database& db() { return *db_; }

 private:
  std::unique_ptr<sql::Database> db_;
  std::unique_ptr<UmaMetricsTable> metrics_table_;
};

TEST_F(UmaMetricsTableTest, CreateTable) {
  EXPECT_FALSE(db().DoesTableExist(UmaMetricsTable::kTableName));
  ASSERT_TRUE(metrics_table().InitTable());

  EXPECT_TRUE(db().DoesTableExist(UmaMetricsTable::kTableName));
  EXPECT_TRUE(db().DoesIndexExist("uma_event_timestamp_index"));
  EXPECT_TRUE(db().DoesIndexExist("uma_type_index"));
  EXPECT_TRUE(db().DoesIndexExist("uma_profile_id_index"));
  EXPECT_TRUE(db().DoesIndexExist("uma_metric_hash_index"));

  // Creating table again should be noop.
  ASSERT_TRUE(metrics_table().InitTable());

  EXPECT_TRUE(db().DoesTableExist(UmaMetricsTable::kTableName));
}

TEST_F(UmaMetricsTableTest, InsertRow) {
  ASSERT_TRUE(metrics_table().InitTable());

  EXPECT_TRUE(db().DoesTableExist(UmaMetricsTable::kTableName));

  // Invalid rows should not inserted.
  // Empty row:
  UmaMetricEntry row;
  EXPECT_FALSE(metrics_table().AddUmaMetric(kProfileId, row));
  // Invalid profile ID:
  auto row1 = GetSampleMetricsRow();
  EXPECT_FALSE(metrics_table().AddUmaMetric(kInvalidProfileId, row1));
  // Empty timestamp:
  row1.time = base::Time();
  EXPECT_FALSE(metrics_table().AddUmaMetric(kProfileId, row1));

  test_util::AssertRowsInUmaMetricsTable(db(), {});

  auto row2 = GetSampleMetricsRow();
  EXPECT_TRUE(metrics_table().AddUmaMetric(kProfileId, row2));
  test_util::AssertRowsInUmaMetricsTable(db(), {row2});

  auto row3 = GetSampleMetricsRow();
  EXPECT_TRUE(metrics_table().AddUmaMetric(kProfileId, row3));
  test_util::AssertRowsInUmaMetricsTable(db(), {row2, row3});
}

TEST_F(UmaMetricsTableTest, CleanupItems) {
  const base::Time kTimestamp1 = base::Time::Now();
  const base::Time kTimestamp2 = kTimestamp1 + base::Seconds(1);
  const base::Time kTimestamp3 = kTimestamp1 + base::Seconds(2);
  const base::Time kTimestamp4 = kTimestamp1 + base::Seconds(3);
  const base::Time kTimestamp5 = kTimestamp1 + base::Seconds(4);
  const int64_t kHash1 = 201;
  const int64_t kHash2 = 202;
  const int64_t kHash3 = 203;
  const int64_t kHash4 = 204;

  ASSERT_TRUE(metrics_table().InitTable());

  std::vector<CleanupItem> empty_items;
  EXPECT_TRUE(metrics_table().CleanupItems(kProfileId, empty_items));
  test_util::AssertRowsInUmaMetricsTable(db(), {});

  std::vector<CleanupItem> items{
      CleanupItem(kHash1, 0, proto::SignalType::HISTOGRAM_VALUE, kTimestamp3),
      CleanupItem(kHash2, 0, proto::SignalType::HISTOGRAM_VALUE, kTimestamp3),
      CleanupItem(10, 0, proto::SignalType::HISTOGRAM_VALUE, kTimestamp3),
      CleanupItem(kHash4, 0, proto::SignalType::HISTOGRAM_VALUE, kTimestamp3),
  };
  EXPECT_TRUE(metrics_table().CleanupItems(kProfileId, items));
  test_util::AssertRowsInUmaMetricsTable(db(), {});

  auto row1 = GetSampleMetricsRow();
  row1.time = kTimestamp1;
  row1.name_hash = kHash1;
  EXPECT_TRUE(metrics_table().AddUmaMetric(kProfileId, row1));
  auto row2 = GetSampleMetricsRow();
  row2.time = kTimestamp2;
  row2.name_hash = kHash2;
  EXPECT_TRUE(metrics_table().AddUmaMetric(kProfileId, row2));
  auto row3 = GetSampleMetricsRow();
  row3.time = kTimestamp3;
  row3.name_hash = kHash3;
  EXPECT_TRUE(metrics_table().AddUmaMetric(kProfileId, row3));
  auto row4 = GetSampleMetricsRow();
  row4.time = kTimestamp4;
  row4.name_hash = kHash4;
  EXPECT_TRUE(metrics_table().AddUmaMetric(kProfileId, row4));

  test_util::AssertRowsInUmaMetricsTable(db(), {row1, row2, row3, row4});

  EXPECT_TRUE(metrics_table().CleanupItems(kProfileId, empty_items));
  test_util::AssertRowsInUmaMetricsTable(db(), {row1, row2, row3, row4});

  EXPECT_TRUE(metrics_table().CleanupItems("bad-profile", items));
  test_util::AssertRowsInUmaMetricsTable(db(), {row1, row2, row3, row4});

  EXPECT_TRUE(metrics_table().CleanupItems(kProfileId, items));
  test_util::AssertRowsInUmaMetricsTable(db(), {row3, row4});

  EXPECT_TRUE(metrics_table().CleanupItems(kProfileId, items));
  test_util::AssertRowsInUmaMetricsTable(db(), {row3, row4});

  // Add more rows.
  auto row5 = GetSampleMetricsRow();
  row5.time = kTimestamp5;
  row5.name_hash = kHash3;
  EXPECT_TRUE(metrics_table().AddUmaMetric(kProfileId, row5));
  auto row6 = GetSampleMetricsRow();
  row6.time = kTimestamp5;
  row6.name_hash = kHash3;
  EXPECT_TRUE(metrics_table().AddUmaMetric(kProfileId, row6));
  test_util::AssertRowsInUmaMetricsTable(db(), {row3, row4, row5, row6});

  EXPECT_TRUE(metrics_table().CleanupItems(kProfileId, items));
  test_util::AssertRowsInUmaMetricsTable(db(), {row3, row4, row5, row6});

  std::vector<CleanupItem> items2{
      CleanupItem(kHash3, 0, proto::SignalType::HISTOGRAM_VALUE, kTimestamp5),
      CleanupItem(10, 0, proto::SignalType::HISTOGRAM_VALUE, kTimestamp3),
      CleanupItem(kHash4, 0, proto::SignalType::HISTOGRAM_VALUE, kTimestamp3),
  };
  EXPECT_TRUE(metrics_table().CleanupItems("bad-profile", items2));
  test_util::AssertRowsInUmaMetricsTable(db(), {row3, row4, row5, row6});

  EXPECT_TRUE(metrics_table().CleanupItems(kProfileId, items2));
  test_util::AssertRowsInUmaMetricsTable(db(), {row4});
}

TEST_F(UmaMetricsTableTest, DeleteBeforeTimestamp) {
  const base::Time kTimestamp1 = base::Time::Now();
  const base::Time kTimestamp2 = kTimestamp1 + base::Seconds(1);
  const base::Time kTimestamp3 = kTimestamp1 + base::Seconds(2);
  const base::Time kTimestamp4 = kTimestamp1 + base::Seconds(3);
  const base::Time kTimestamp5 = kTimestamp1 + base::Seconds(4);
  const int64_t kHash1 = 201;
  const int64_t kHash2 = 202;
  const int64_t kHash3 = 203;
  const int64_t kHash4 = 204;

  ASSERT_TRUE(metrics_table().InitTable());

  // Delete on empty table does nothing.
  std::vector<std::string> empty_set;
  EXPECT_TRUE(metrics_table().DeleteEventsBeforeTimestamp(base::Time()));
  test_util::AssertRowsInUmaMetricsTable(db(), {});
  EXPECT_TRUE(metrics_table().DeleteEventsBeforeTimestamp(kTimestamp1));
  test_util::AssertRowsInUmaMetricsTable(db(), {});

  auto row1 = GetSampleMetricsRow();
  row1.time = kTimestamp1;
  row1.name_hash = kHash1;
  EXPECT_TRUE(metrics_table().AddUmaMetric(kProfileId, row1));
  auto row2 = GetSampleMetricsRow();
  row2.time = kTimestamp2;
  row2.name_hash = kHash2;
  EXPECT_TRUE(metrics_table().AddUmaMetric(kProfileId, row2));
  auto row3 = GetSampleMetricsRow();
  row3.time = kTimestamp3;
  row3.name_hash = kHash3;
  EXPECT_TRUE(metrics_table().AddUmaMetric(kProfileId, row3));
  auto row4 = GetSampleMetricsRow();
  row4.time = kTimestamp4;
  row4.name_hash = kHash4;
  EXPECT_TRUE(metrics_table().AddUmaMetric(kProfileId, row4));

  test_util::AssertRowsInUmaMetricsTable(db(), {row1, row2, row3, row4});

  // Delete with a time before all rows, does nothing.
  EXPECT_TRUE(metrics_table().DeleteEventsBeforeTimestamp(base::Time()));
  test_util::AssertRowsInUmaMetricsTable(db(), {row1, row2, row3, row4});
  EXPECT_TRUE(metrics_table().DeleteEventsBeforeTimestamp(kTimestamp1 -
                                                          base::Seconds(1)));
  test_util::AssertRowsInUmaMetricsTable(db(), {row1, row2, row3, row4});

  // Remove single row.
  EXPECT_TRUE(metrics_table().DeleteEventsBeforeTimestamp(kTimestamp1));
  test_util::AssertRowsInUmaMetricsTable(db(), {row2, row3, row4});

  // Add more rows.
  auto row5 = GetSampleMetricsRow();
  row5.time = kTimestamp5;
  row5.name_hash = kHash3;
  EXPECT_TRUE(metrics_table().AddUmaMetric(kProfileId, row5));
  auto row6 = GetSampleMetricsRow();
  row6.time = kTimestamp5;
  row6.name_hash = kHash3;
  EXPECT_TRUE(metrics_table().AddUmaMetric(kProfileId, row6));
  test_util::AssertRowsInUmaMetricsTable(db(), {row2, row3, row4, row5, row6});

  // Remove a bunch of rows.
  EXPECT_TRUE(metrics_table().DeleteEventsBeforeTimestamp(kTimestamp4));
  test_util::AssertRowsInUmaMetricsTable(db(), {row5, row6});

  // Insert entry with an older timestamp out of order and remove old entries
  // should still work.
  EXPECT_TRUE(metrics_table().AddUmaMetric(kProfileId, row1));
  test_util::AssertRowsInUmaMetricsTable(db(), {row5, row6, row1});
  EXPECT_TRUE(metrics_table().DeleteEventsBeforeTimestamp(kTimestamp4));
  test_util::AssertRowsInUmaMetricsTable(db(), {row5, row6});

  // Removing all metrics.
  EXPECT_TRUE(metrics_table().DeleteEventsBeforeTimestamp(kTimestamp5));
  test_util::AssertRowsInUmaMetricsTable(db(), {});
}

TEST_F(UmaMetricsTableTest, MatchHashesTest) {
  const int64_t metric_hash1 = (1);
  const int64_t metric_hash2 = (0x9CEA8CBC362AB242);
  const int64_t metric_hash3 = (std::numeric_limits<uint64_t>::max());
  ASSERT_TRUE(metrics_table().InitTable());

  UmaMetricEntry row1 = GetSampleMetricsRow();
  row1.name_hash = metric_hash1;
  EXPECT_TRUE(metrics_table().AddUmaMetric(kProfileId, row1));
  UmaMetricEntry row2 = GetSampleMetricsRow();
  row2.name_hash = metric_hash2;
  EXPECT_TRUE(metrics_table().AddUmaMetric(kProfileId, row2));
  UmaMetricEntry row3 = GetSampleMetricsRow();
  row3.name_hash = metric_hash3;
  EXPECT_TRUE(metrics_table().AddUmaMetric(kProfileId, row3));
  UmaMetricEntry row4 = GetSampleMetricsRow();
  row4.name_hash = metric_hash1;
  EXPECT_TRUE(metrics_table().AddUmaMetric(kProfileIdOther, row4));

  auto result1 = test_util::GetUmaMetricsRowWithQuery(
      "SELECT * FROM uma_metrics WHERE metric_hash = '1' AND profile_id='123'",
      db());
  ASSERT_EQ(1u, result1.size());
  test_util::ExpectUmaRowIsEqual(row1, result1[0]);

  auto result2 = test_util::GetUmaMetricsRowWithQuery(
      "SELECT * FROM uma_metrics WHERE metric_hash = '9CEA8CBC362AB242' AND "
      "profile_id='123'",
      db());
  ASSERT_EQ(1u, result2.size());
  test_util::ExpectUmaRowIsEqual(row2, result2[0]);

  auto result3 = test_util::GetUmaMetricsRowWithQuery(
      "SELECT * FROM uma_metrics WHERE metric_hash = 'FFFFFFFFFFFFFFFF' AND "
      "profile_id='123'",
      db());
  ASSERT_EQ(1u, result3.size());
  test_util::ExpectUmaRowIsEqual(row3, result3[0]);

  auto result4 = test_util::GetUmaMetricsRowWithQuery(
      "SELECT * FROM uma_metrics WHERE metric_hash = '1' ORDER BY id", db());
  ASSERT_EQ(2u, result4.size());
  test_util::ExpectUmaRowIsEqual(row1, result4[0]);
  test_util::ExpectUmaRowIsEqual(row4, result4[1]);
}

}  // namespace segmentation_platform