From 48c8ed422c4d35bbbc8a3e8e137e5c778ac0e441 Mon Sep 17 00:00:00 2001 From: Mikhail Atuchin Date: Tue, 10 Oct 2023 01:15:54 +0100 Subject: [PATCH] Add adblock loading traces & UMAs --- .../browser/dat_file_util.cc | 6 +++- .../brave_shields/browser/ad_block_engine.cc | 35 ++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/components/brave_component_updater/browser/dat_file_util.cc b/components/brave_component_updater/browser/dat_file_util.cc index 9376d9db8850..ce8c9f55e8db 100644 --- a/components/brave_component_updater/browser/dat_file_util.cc +++ b/components/brave_component_updater/browser/dat_file_util.cc @@ -8,9 +8,10 @@ #include #include -#include "base/logging.h" #include "base/files/file_path.h" #include "base/files/file_util.h" +#include "base/logging.h" +#include "base/trace_event/trace_event.h" namespace { @@ -40,8 +41,11 @@ void GetDATFileData(const base::FilePath& file_path, namespace brave_component_updater { DATFileDataBuffer ReadDATFileData(const base::FilePath& dat_file_path) { + TRACE_EVENT_BEGIN1("brave.adblock", "ReadDATFileData", "path", + dat_file_path.MaybeAsASCII()); DATFileDataBuffer buffer; GetDATFileData(dat_file_path, &buffer); + TRACE_EVENT_END1("brave.adblock", "ReadDATFileData", "size", buffer.size()); return buffer; } diff --git a/components/brave_shields/browser/ad_block_engine.cc b/components/brave_shields/browser/ad_block_engine.cc index 5aa3f42b6551..7512329e3551 100644 --- a/components/brave_shields/browser/ad_block_engine.cc +++ b/components/brave_shields/browser/ad_block_engine.cc @@ -12,7 +12,10 @@ #include "base/containers/contains.h" #include "base/json/json_reader.h" +#include "base/metrics/histogram_functions.h" #include "base/strings/string_number_conversions.h" +#include "base/timer/elapsed_timer.h" +#include "base/trace_event/trace_event.h" #include "net/base/registry_controlled_domains/registry_controlled_domain.h" #include "url/origin.h" @@ -243,6 +246,7 @@ base::Value::List AdBlockEngine::HiddenClassIdSelectors( void AdBlockEngine::Load(bool deserialize, const DATFileDataBuffer& dat_buf, const std::string& resources_json) { + DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); if (deserialize) { OnDATLoaded(dat_buf, resources_json); } else { @@ -275,7 +279,21 @@ void AdBlockEngine::AddKnownTagsToAdBlockInstance() { void AdBlockEngine::OnListSourceLoaded(const DATFileDataBuffer& filters, const std::string& resources_json) { + DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); + + base::ElapsedTimer timer; + TRACE_EVENT_BEGIN1("brave.adblock", "MakeEngineWithRules", "size", + filters.size()); + auto result = adblock::engine_with_rules(filters); + + TRACE_EVENT_END0("brave.adblock", "MakeEngineWithRules"); + base::UmaHistogramTimes("Brave.Adblock.MakeEngineWithRules", timer.Elapsed()); + if (filters.size() > 10'000) { + base::UmaHistogramTimes("Brave.Adblock.MakeEngineWithRules.Large", + timer.Elapsed()); + } + if (result.result_kind != adblock::ResultKind::Success) { LOG(ERROR) << "AdBlockEngine::OnListSourceLoaded failed: " << result.error_message.c_str(); @@ -286,13 +304,28 @@ void AdBlockEngine::OnListSourceLoaded(const DATFileDataBuffer& filters, void AdBlockEngine::OnDATLoaded(const DATFileDataBuffer& dat_buf, const std::string& resources_json) { + DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); + // An empty buffer will not load successfully. if (dat_buf.empty()) { return; } + base::ElapsedTimer timer; + TRACE_EVENT_BEGIN1("brave.adblock", "EngineDeserialize", "size", + dat_buf.size()); + auto client = adblock::new_engine(); - if (!client->deserialize(dat_buf)) { + const auto result = client->deserialize(dat_buf); + + TRACE_EVENT_END0("brave.adblock", "EngineDeserialize"); + base::UmaHistogramTimes("Brave.Adblock.EngineDeserialize", timer.Elapsed()); + if (dat_buf.size() > 10'000) { + base::UmaHistogramTimes("Brave.Adblock.EngineDeserialize.Large", + timer.Elapsed()); + } + + if (!result) { LOG(ERROR) << "AdBlockEngine::OnDATLoaded deserialize failed"; return; }