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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
// 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/metrics/structured/storage_manager.h"
#include <algorithm>
#include <cstdint>
#include <memory>
#include <utility>
#include <vector>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/test/bind.h"
#include "base/test/run_until.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
#include "chrome/browser/metrics/structured/storage_manager_impl.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/metrics/structured/lib/event_buffer.h"
#include "components/metrics/structured/storage_manager.h"
#include "components/metrics/structured/structured_metrics_features.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/metrics_proto/structured_data.pb.h"
namespace metrics::structured {
namespace {
using google::protobuf::RepeatedPtrField;
class TestStorageDelegate : public StorageManager::StorageDelegate {
public:
using FlushedCallback = base::RepeatingCallback<void(const FlushedKey&)>;
using DeletedCallback =
base::RepeatingCallback<void(const FlushedKey&, DeleteReason)>;
TestStorageDelegate() = default;
~TestStorageDelegate() override = default;
void SetFlushedCallback(FlushedCallback flushed) {
flushed_callback_ = std::move(flushed);
}
void SetDeletedCallback(DeletedCallback deleted) {
delete_callback_ = std::move(deleted);
}
void OnFlushed(const FlushedKey& key) override {
flushed_count_ += 1;
flushed_key_ = key;
if (flushed_callback_) {
flushed_callback_.Run(key);
}
}
void OnDeleted(const FlushedKey& key, DeleteReason reason) override {
delete_count_ += 1;
if (delete_callback_) {
delete_callback_.Run(key, reason);
}
}
int flushed_count() const { return flushed_count_; }
const std::optional<FlushedKey>& flushed_key() { return flushed_key_; }
int delete_count() const { return delete_count_; }
private:
std::optional<FlushedKey> flushed_key_;
int flushed_count_ = 0;
int delete_count_ = 0;
FlushedCallback flushed_callback_;
DeletedCallback delete_callback_;
};
StructuredEventProto BuildTestEvent(
uint64_t id = 0,
const std::vector<int64_t>& metrics = std::vector<int64_t>()) {
StructuredEventProto event;
event.set_device_project_id(id);
int metric_id = 0;
for (int64_t metric : metrics) {
auto* m = event.add_metrics();
m->set_name_hash(metric_id++);
m->set_value_int64(metric);
}
return event;
}
EventsProto BuildTestEventsProto(int num, int start_id = 0) {
EventsProto events;
for (int i = 0; i < num; ++i) {
events.mutable_events()->Add(BuildTestEvent(start_id + i, {start_id}));
}
return events;
}
StorageManagerConfig CreateManagerConfig(int32_t buffer_max_bytes,
int32_t disk_max_bytes) {
return StorageManagerConfig{
.buffer_max_bytes = buffer_max_bytes,
.disk_max_bytes = disk_max_bytes,
};
}
EventsProto ReadFlushedEvents(const base::FilePath& path) {
std::string content;
EXPECT_TRUE(base::ReadFileToString(path, &content));
EventsProto list;
EXPECT_TRUE(list.MergeFromString(content));
return list;
}
void WriteEventsProto(const base::FilePath& dir,
std::string_view name,
EventsProto&& events) {
std::string content;
ASSERT_TRUE(events.SerializeToString(&content));
ASSERT_TRUE(base::WriteFile(dir.Append(name), content));
}
} // namespace
class StorageManagerTest : public testing::Test {
public:
StorageManagerTest()
: storage_delegate_(std::make_unique<TestStorageDelegate>()),
profile_manager_(TestingBrowserProcess::GetGlobal()) {}
void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
ASSERT_TRUE(profile_manager_.SetUp());
}
base::FilePath GetArenaPath() {
return temp_dir_.GetPath().Append(FILE_PATH_LITERAL("arena_file"));
}
base::FilePath GetFlushDir() {
return temp_dir_.GetPath().Append(FILE_PATH_LITERAL("flushed_dir"));
}
void Wait() { task_environment_.RunUntilIdle(); }
std::unique_ptr<StorageManager> CreateManager(
const StorageManagerConfig& config) {
auto manager = std::make_unique<StorageManagerImpl>(config, GetArenaPath(),
GetFlushDir());
manager->set_delegate(storage_delegate_.get());
return manager;
}
void SortEvents(RepeatedPtrField<StructuredEventProto>& events) {
std::sort(events.begin(), events.end(),
[](const StructuredEventProto& l,
const StructuredEventProto& r) -> bool {
return l.device_project_id() < r.device_project_id();
});
}
private:
base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::MainThreadType::UI,
base::test::TaskEnvironment::ThreadPoolExecutionMode::QUEUED};
base::ScopedTempDir temp_dir_;
protected:
std::unique_ptr<TestStorageDelegate> storage_delegate_;
TestingProfileManager profile_manager_;
};
TEST_F(StorageManagerTest, StoreAndProvideEventsInMemory) {
std::unique_ptr<StorageManager> manager = CreateManager(
CreateManagerConfig(/*buffer_max_bytes=*/1024, /*disk_max_bytes=*/1024));
Wait();
// Add event.
manager->AddEvent(BuildTestEvent(1, {1, 2, 3}));
// Expect it to be recorded
EXPECT_EQ(manager->RecordedEventsCount(), 1);
// Expect nothing to be flushed.
EXPECT_EQ(base::ComputeDirectorySize(GetFlushDir()), 0l);
// Provide events.
RepeatedPtrField<StructuredEventProto> events = manager->TakeEvents();
EXPECT_EQ(manager->RecordedEventsCount(), 0);
EXPECT_EQ(events.size(), 1);
EXPECT_EQ(events[0].device_project_id(), 1ul);
}
TEST_F(StorageManagerTest, FlushEvents) {
std::unique_ptr<StorageManager> manager = CreateManager(
CreateManagerConfig(/*buffer_max_bytes=*/512, /*disk_max_bytes=*/1024));
Wait();
storage_delegate_->SetFlushedCallback(base::BindLambdaForTesting(
[&](const FlushedKey& key) { EXPECT_TRUE(base::PathExists(key.path)); }));
manager->AddEvent(BuildTestEvent(1, {1, 2, 3, 4}));
EXPECT_EQ(manager->RecordedEventsCount(), 1);
// A flush should occur and the event should be added.
manager->AddEvent(BuildTestEvent(2, {1, 2, 3, 4}));
Wait();
ASSERT_EQ(storage_delegate_->flushed_count(), 1);
ASSERT_TRUE(storage_delegate_->flushed_key().has_value());
EventsProto events =
ReadFlushedEvents(storage_delegate_->flushed_key()->path);
ASSERT_EQ(events.events_size(), 1);
const auto& event = events.events(0);
EXPECT_EQ(event.device_project_id(), 1ul);
EXPECT_EQ(event.metrics_size(), 4);
}
TEST_F(StorageManagerTest, FullBuffer) {
std::unique_ptr<StorageManager> manager = CreateManager(
CreateManagerConfig(/*buffer_max_bytes=*/512, /*disk_max_bytes=*/1024));
Wait();
storage_delegate_->SetFlushedCallback(base::BindLambdaForTesting(
[&](const FlushedKey& key) { EXPECT_TRUE(base::PathExists(key.path)); }));
manager->AddEvent(BuildTestEvent(1, {1, 2, 3, 4}));
EXPECT_EQ(manager->RecordedEventsCount(), 1);
// A flush should occur and the event should be added.
manager->AddEvent(BuildTestEvent(1, {1, 2, 3, 4}));
Wait();
EXPECT_EQ(manager->RecordedEventsCount(), 1);
EXPECT_EQ(storage_delegate_->flushed_count(), 1);
}
// Tests the ability of StorageManager to collect on-disk events and return them
// to be uploaded.
TEST_F(StorageManagerTest, ProvideFlushedEvents) {
// This isn't needed in other tests because FlushedMap creates the directory
// if it doesn't exist. Here we populate it before hand.
ASSERT_TRUE(base::CreateDirectory(GetFlushDir()));
WriteEventsProto(GetFlushDir(), "events1", BuildTestEventsProto(/*num=*/3));
WriteEventsProto(GetFlushDir(), "events2",
BuildTestEventsProto(/*num=*/3, /*start_id=*/3));
WriteEventsProto(GetFlushDir(), "events3",
BuildTestEventsProto(/*num=*/3, /*start_id=*/6));
std::unique_ptr<StorageManager> manager =
CreateManager(CreateManagerConfig(/*buffer_max_bytes=*/512,
/*disk_max_bytes=*/1024));
Wait();
// Returns the number of on-disk files if there are no events in memory.
ASSERT_EQ(manager->RecordedEventsCount(), 3);
storage_delegate_->SetDeletedCallback(base::BindLambdaForTesting(
[&](const FlushedKey& key, DeleteReason reason) {
EXPECT_EQ(reason, DeleteReason::kUploaded);
EXPECT_FALSE(base::PathExists(key.path));
}));
RepeatedPtrField<StructuredEventProto> events = manager->TakeEvents();
Wait();
// Since on-disk events are being read, it is expected they are deleted.
EXPECT_EQ(storage_delegate_->delete_count(), 3);
// 3 events from each of the 3 files.
EXPECT_EQ(events.size(), 9);
// Sort the events to validating all events are present easier.
SortEvents(events);
uint64_t expected_id = 0;
for (const auto& event : events) {
EXPECT_EQ(event.device_project_id(), expected_id++);
}
}
TEST_F(StorageManagerTest, ProvideFlushedAndInMemoryEvents) {
// This isn't needed in other tests because FlushedMap creates the directory
// if it doesn't exist. Here we populate it before hand.
ASSERT_TRUE(base::CreateDirectory(GetFlushDir()));
WriteEventsProto(GetFlushDir(), "events1", BuildTestEventsProto(/*num=*/3));
WriteEventsProto(GetFlushDir(), "events2",
BuildTestEventsProto(/*num=*/3, /*start_id=*/3));
WriteEventsProto(GetFlushDir(), "events3",
BuildTestEventsProto(/*num=*/3, /*start_id=*/6));
std::unique_ptr<StorageManager> manager =
CreateManager(CreateManagerConfig(/*buffer_max_bytes=*/1024,
/*disk_max_bytes=*/1024));
Wait();
manager->AddEvent(BuildTestEvent(9, {9}));
manager->AddEvent(BuildTestEvent(10, {10}));
manager->AddEvent(BuildTestEvent(11, {11}));
ASSERT_EQ(manager->RecordedEventsCount(), 3);
storage_delegate_->SetDeletedCallback(base::BindLambdaForTesting(
[&](const FlushedKey& key, DeleteReason reason) {
EXPECT_EQ(reason, DeleteReason::kUploaded);
EXPECT_FALSE(base::PathExists(key.path));
}));
RepeatedPtrField<StructuredEventProto> events = manager->TakeEvents();
Wait();
// Since on-disk events are being read, it is expected they are deleted.
EXPECT_EQ(storage_delegate_->delete_count(), 3);
// 3 events from each of the 3 files and 3 in-memory events.
EXPECT_EQ(events.size(), 12);
// Sort the events to validating all events are present easier.
SortEvents(events);
uint64_t expected_id = 0;
for (const auto& event : events) {
EXPECT_EQ(event.device_project_id(), expected_id++);
}
}
TEST_F(StorageManagerTest, Purge) {
// This isn't needed in other tests because FlushedMap creates the directory
// if it doesn't exist. Here we populate it before hand.
ASSERT_TRUE(base::CreateDirectory(GetFlushDir()));
WriteEventsProto(GetFlushDir(), "events1", BuildTestEventsProto(/*num=*/3));
WriteEventsProto(GetFlushDir(), "events2",
BuildTestEventsProto(/*num=*/3, /*start_id=*/3));
WriteEventsProto(GetFlushDir(), "events3",
BuildTestEventsProto(/*num=*/3, /*start_id=*/6));
std::unique_ptr<StorageManager> manager =
CreateManager(CreateManagerConfig(/*buffer_max_bytes=*/1024,
/*disk_max_bytes=*/1024));
Wait();
manager->AddEvent(BuildTestEvent(9, {9}));
manager->AddEvent(BuildTestEvent(10, {10}));
manager->AddEvent(BuildTestEvent(11, {11}));
ASSERT_EQ(manager->RecordedEventsCount(), 3);
manager->Purge();
ASSERT_EQ(manager->RecordedEventsCount(), 0);
EXPECT_FALSE(
base::PathExists(GetFlushDir().Append(FILE_PATH_LITERAL("events1"))));
EXPECT_FALSE(
base::PathExists(GetFlushDir().Append(FILE_PATH_LITERAL("events2"))));
EXPECT_FALSE(
base::PathExists(GetFlushDir().Append(FILE_PATH_LITERAL("events3"))));
std::optional<int64_t> size = base::GetFileSize(GetArenaPath());
ASSERT_TRUE(size.has_value());
EXPECT_EQ(size.value(), 0l);
}
TEST_F(StorageManagerTest, FlushedQuotaExceeded) {
std::unique_ptr<StorageManager> manager = CreateManager(
CreateManagerConfig(/*buffer_max_bytes=*/512, /*disk_max_bytes=*/64));
Wait();
storage_delegate_->SetFlushedCallback(base::BindLambdaForTesting(
[&](const FlushedKey& key) { EXPECT_TRUE(base::PathExists(key.path)); }));
manager->AddEvent(BuildTestEvent(1, {1, 2, 3, 4}));
EXPECT_EQ(manager->RecordedEventsCount(), 1);
// A flush should occur and the event should be added.
manager->AddEvent(BuildTestEvent(1, {1, 2, 3, 4}));
Wait();
// Only the previously added event should be represented as recorded.
EXPECT_EQ(manager->RecordedEventsCount(), 1);
// Expect 1 batch of events to be written to disk.
EXPECT_EQ(storage_delegate_->flushed_count(), 1);
FlushedKey previous_key = *storage_delegate_->flushed_key();
std::vector<FlushedKey> dropped_keys;
storage_delegate_->SetDeletedCallback(base::BindLambdaForTesting(
[&](const FlushedKey& key, DeleteReason reason) {
EXPECT_EQ(reason, DeleteReason::kExceededQuota);
// The path that was flushed first (only 2) should be the key that is
// flushed. Only one is expected so to be deleted, if more happen this
// will fail.
EXPECT_EQ(previous_key.path, key.path);
dropped_keys.push_back(key);
}));
// A flush should occur but our quota has been reached.
manager->AddEvent(BuildTestEvent(1, {1, 2, 3, 4}));
Wait();
// After the second flush, the first file should be deleted.
EXPECT_EQ(storage_delegate_->delete_count(), 1);
// Deleting of the files is asynchronous, checking once all tasks have
// completed.
for (const FlushedKey& key : dropped_keys) {
EXPECT_FALSE(base::PathExists(key.path));
}
}
} // namespace metrics::structured
|