Skip to content

Commit

Permalink
[ads] Profile performance with TRACE events
Browse files Browse the repository at this point in the history
  • Loading branch information
tmancey committed Jan 10, 2025
1 parent c1a3e10 commit 4987e7f
Show file tree
Hide file tree
Showing 60 changed files with 562 additions and 132 deletions.
3 changes: 2 additions & 1 deletion chromium_src/base/trace_event/builtin_categories.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
// macros of the chromium builtin_categories.h.
#define BRAVE_INTERNAL_TRACE_LIST_BUILTIN_CATEGORIES(X) \
X("brave") \
X("brave.adblock")
X("brave.adblock") \
X("brave.ads")

#include "src/base/trace_event/builtin_categories.h" // IWYU pragma: export

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "base/check.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/time/time.h"
#include "brave/components/brave_ads/core/internal/account/confirmations/confirmations_util.h"
#include "brave/components/brave_ads/core/internal/account/confirmations/queue/queue_item/confirmation_queue_item_builder.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "base/functional/bind.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/location.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "brave/components/brave_ads/core/internal/account/confirmations/confirmations_feature.h"
Expand Down Expand Up @@ -303,7 +304,8 @@ void ConfirmationQueue::Save(
Insert(mojom_db_transaction, batch);
}

RunDBTransaction(std::move(mojom_db_transaction), std::move(callback));
RunDBTransaction(FROM_HERE, std::move(mojom_db_transaction),
std::move(callback));
}

void ConfirmationQueue::DeleteAll(ResultCallback callback) const {
Expand All @@ -312,7 +314,8 @@ void ConfirmationQueue::DeleteAll(ResultCallback callback) const {

DeleteTable(mojom_db_transaction, GetTableName());

RunDBTransaction(std::move(mojom_db_transaction), std::move(callback));
RunDBTransaction(FROM_HERE, std::move(mojom_db_transaction),
std::move(callback));
}

void ConfirmationQueue::Delete(const std::string& transaction_id,
Expand All @@ -327,7 +330,8 @@ void ConfirmationQueue::Delete(const std::string& transaction_id,
)",
{GetTableName(), transaction_id});

RunDBTransaction(std::move(mojom_db_transaction), std::move(callback));
RunDBTransaction(FROM_HERE, std::move(mojom_db_transaction),
std::move(callback));
}

void ConfirmationQueue::Retry(const std::string& transaction_id,
Expand Down Expand Up @@ -360,7 +364,8 @@ void ConfirmationQueue::Retry(const std::string& transaction_id,
{GetTableName(), TimeToSqlValueAsString(base::Time::Now()), retry_after,
max_retry_delay, retry_after, max_retry_delay, transaction_id});

RunDBTransaction(std::move(mojom_db_transaction), std::move(callback));
RunDBTransaction(FROM_HERE, std::move(mojom_db_transaction),
std::move(callback));
}

void ConfirmationQueue::GetAll(GetConfirmationQueueCallback callback) const {
Expand Down Expand Up @@ -393,7 +398,7 @@ void ConfirmationQueue::GetAll(GetConfirmationQueueCallback callback) const {
BindColumnTypes(mojom_db_action);
mojom_db_transaction->actions.push_back(std::move(mojom_db_action));

RunDBTransaction(std::move(mojom_db_transaction),
RunDBTransaction(FROM_HERE, std::move(mojom_db_transaction),
base::BindOnce(&GetCallback, std::move(callback)));
}

Expand Down Expand Up @@ -429,7 +434,7 @@ void ConfirmationQueue::GetNext(GetConfirmationQueueCallback callback) const {
BindColumnTypes(mojom_db_action);
mojom_db_transaction->actions.push_back(std::move(mojom_db_action));

RunDBTransaction(std::move(mojom_db_transaction),
RunDBTransaction(FROM_HERE, std::move(mojom_db_transaction),
base::BindOnce(&GetCallback, std::move(callback)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "base/check.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/strings/string_util.h"
#include "brave/components/brave_ads/core/internal/common/database/database_column_util.h"
#include "brave/components/brave_ads/core/internal/common/database/database_statement_util.h"
Expand Down Expand Up @@ -133,7 +134,8 @@ void Deposits::Save(const DepositInfo& deposit, ResultCallback callback) {

Insert(mojom_db_transaction, deposit);

RunDBTransaction(std::move(mojom_db_transaction), std::move(callback));
RunDBTransaction(FROM_HERE, std::move(mojom_db_transaction),
std::move(callback));
}

void Deposits::Insert(const mojom::DBTransactionInfoPtr& mojom_db_transaction,
Expand Down Expand Up @@ -186,7 +188,7 @@ void Deposits::GetForCreativeInstanceId(const std::string& creative_instance_id,
BindColumnTypes(mojom_db_action);
mojom_db_transaction->actions.push_back(std::move(mojom_db_action));

RunDBTransaction(std::move(mojom_db_transaction),
RunDBTransaction(FROM_HERE, std::move(mojom_db_transaction),
base::BindOnce(&GetForCreativeInstanceIdCallback,
creative_instance_id, std::move(callback)));
}
Expand All @@ -201,7 +203,8 @@ void Deposits::PurgeExpired(ResultCallback callback) const {
$2 >= expire_at;)",
{GetTableName(), TimeToSqlValueAsString(base::Time::Now())});

RunDBTransaction(std::move(mojom_db_transaction), std::move(callback));
RunDBTransaction(FROM_HERE, std::move(mojom_db_transaction),
std::move(callback));
}

std::string Deposits::GetTableName() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <utility>

#include "base/functional/bind.h"
#include "base/location.h"
#include "base/time/time.h"
#include "brave/components/brave_ads/core/internal/account/issuers/issuers_info.h"
#include "brave/components/brave_ads/core/internal/account/issuers/url_request/issuers_url_request_builder.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
#include "base/check.h"
#include "base/debug/dump_without_crashing.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/strings/string_util.h"
#include "base/time/time.h"
#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h"
#include "brave/components/brave_ads/core/internal/common/database/database_column_util.h"
#include "brave/components/brave_ads/core/internal/common/database/database_statement_util.h"
#include "brave/components/brave_ads/core/internal/common/database/database_table_util.h"
Expand All @@ -24,7 +24,6 @@
#include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h"
#include "brave/components/brave_ads/core/public/account/confirmations/confirmation_type.h"
#include "brave/components/brave_ads/core/public/ad_units/ad_type.h"
#include "brave/components/brave_ads/core/public/ads_client/ads_client.h"

namespace brave_ads::database::table {

Expand Down Expand Up @@ -210,7 +209,8 @@ void Transactions::Save(const TransactionList& transactions,

Insert(mojom_db_transaction, transactions);

RunDBTransaction(std::move(mojom_db_transaction), std::move(callback));
RunDBTransaction(FROM_HERE, std::move(mojom_db_transaction),
std::move(callback));
}

void Transactions::GetForDateRange(base::Time from_time,
Expand Down Expand Up @@ -241,7 +241,7 @@ void Transactions::GetForDateRange(base::Time from_time,
BindColumnTypes(mojom_db_action);
mojom_db_transaction->actions.push_back(std::move(mojom_db_action));

RunDBTransaction(std::move(mojom_db_transaction),
RunDBTransaction(FROM_HERE, std::move(mojom_db_transaction),
base::BindOnce(&GetCallback, std::move(callback)));
}

Expand Down Expand Up @@ -282,7 +282,8 @@ void Transactions::Reconcile(const PaymentTokenList& payment_tokens,

mojom_db_transaction->actions.push_back(std::move(mojom_db_action));

RunDBTransaction(std::move(mojom_db_transaction), std::move(callback));
RunDBTransaction(FROM_HERE, std::move(mojom_db_transaction),
std::move(callback));
}

void Transactions::PurgeExpired(ResultCallback callback) const {
Expand All @@ -297,7 +298,8 @@ void Transactions::PurgeExpired(ResultCallback callback) const {
{GetTableName(),
TimeToSqlValueAsString(base::Time::Now() - base::Days(90))});

RunDBTransaction(std::move(mojom_db_transaction), std::move(callback));
RunDBTransaction(FROM_HERE, std::move(mojom_db_transaction),
std::move(callback));
}

std::string Transactions::GetTableName() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <utility>

#include "base/functional/bind.h"
#include "base/location.h"
#include "base/time/time.h"
#include "brave/components/brave_ads/core/internal/account/tokens/payment_tokens/payment_token_util.h"
#include "brave/components/brave_ads/core/internal/account/utility/redeem_payment_tokens/redeem_payment_tokens_util.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "base/functional/bind.h"
#include "base/json/json_reader.h"
#include "base/location.h"
#include "base/strings/strcat.h"
#include "base/time/time.h"
#include "base/types/expected.h"
Expand Down
27 changes: 17 additions & 10 deletions components/brave_ads/core/internal/ads_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "base/debug/dump_without_crashing.h"
#include "base/functional/bind.h"
#include "base/trace_event/trace_event.h"
#include "brave/components/brave_ads/core/internal/account/wallet/wallet_util.h"
#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h"
#include "brave/components/brave_ads/core/internal/ads_core/ads_core_util.h"
Expand All @@ -27,20 +28,11 @@
#include "brave/components/brave_ads/core/internal/legacy_migration/confirmations/legacy_confirmation_migration.h"
#include "brave/components/brave_ads/core/internal/user_engagement/ad_events/ad_events.h"
#include "brave/components/brave_ads/core/public/ads_client/ads_client.h"
#include "brave/components/brave_ads/core/public/ads_constants.h"
#include "brave/components/brave_ads/core/public/service/ads_service_callback.h"

namespace brave_ads {

namespace {

void FailedToInitialize(InitializeCallback callback) {
BLOG(0, "Failed to initialize ads");

std::move(callback).Run(/*success=*/false);
}

} // namespace

AdsImpl::AdsImpl(AdsClient& ads_client,
const base::FilePath& database_path,
std::unique_ptr<TokenGeneratorInterface> token_generator)
Expand Down Expand Up @@ -76,6 +68,9 @@ void AdsImpl::Initialize(mojom::WalletInfoPtr mojom_wallet,
InitializeCallback callback) {
BLOG(1, "Initializing ads");

TRACE_EVENT_NESTABLE_ASYNC_BEGIN0(kTraceEventCategory, "AdsImpl::Initialize",
TRACE_ID_LOCAL(this));

if (is_initialized_) {
BLOG(1, "Already initialized ads");
return FailedToInitialize(std::move(callback));
Expand Down Expand Up @@ -413,8 +408,20 @@ void AdsImpl::CreateOrOpenDatabaseCallback(mojom::WalletInfoPtr mojom_wallet,
std::move(mojom_wallet), std::move(callback)));
}

void AdsImpl::FailedToInitialize(InitializeCallback callback) {
TRACE_EVENT_NESTABLE_ASYNC_END0(kTraceEventCategory, "AdsImpl::Initialize",
TRACE_ID_LOCAL(this));

BLOG(0, "Failed to initialize ads");

std::move(callback).Run(/*success=*/false);
}

void AdsImpl::SuccessfullyInitialized(mojom::WalletInfoPtr mojom_wallet,
InitializeCallback callback) {
TRACE_EVENT_NESTABLE_ASYNC_END0(kTraceEventCategory, "AdsImpl::Initialize",
TRACE_ID_LOCAL(this));

BLOG(1, "Successfully initialized ads");

is_initialized_ = true;
Expand Down
1 change: 1 addition & 0 deletions components/brave_ads/core/internal/ads_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class AdsImpl final : public Ads {
void CreateOrOpenDatabaseCallback(mojom::WalletInfoPtr mojom_wallet,
InitializeCallback callback,
bool success);
void FailedToInitialize(InitializeCallback callback);
void SuccessfullyInitialized(mojom::WalletInfoPtr mojom_wallet,
InitializeCallback callback);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <utility>

#include "base/functional/bind.h"
#include "base/location.h"
#include "base/time/time.h"
#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h"
#include "brave/components/brave_ads/core/internal/ads_notifier_manager.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "base/ranges/algorithm.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h"
#include "brave/components/brave_ads/core/internal/catalog/campaign/catalog_campaign_info.h"
#include "brave/components/brave_ads/core/internal/catalog/campaign/creative_set/catalog_conversion_info.h"
Expand All @@ -21,9 +22,11 @@

namespace brave_ads::json::reader {

namespace {

// TODO(https://github.com/brave/brave-browser/issues/25987): Reduce cognitive
// complexity.
std::optional<CatalogInfo> ReadCatalog(const std::string& json) {
std::optional<CatalogInfo> ReadCatalogImpl(const std::string& json) {
rapidjson::Document document;
document.Parse(json.c_str());

Expand Down Expand Up @@ -448,4 +451,20 @@ std::optional<CatalogInfo> ReadCatalog(const std::string& json) {
return catalog;
}

} // namespace

std::optional<CatalogInfo> ReadCatalog(const std::string& json) {
TRACE_EVENT_BEGIN(kTraceEventCategory, "ReadCatalog");

const std::optional<CatalogInfo> catalog = ReadCatalogImpl(json);
if (catalog) {
TRACE_EVENT_END2(kTraceEventCategory, "ReadCatalog", "id", catalog->id,
"success", true);
} else {
TRACE_EVENT_END1(kTraceEventCategory, "ReadCatalog", "success", false);
}

return catalog;
}

} // namespace brave_ads::json::reader
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace json::reader {
std::optional<CatalogInfo> ReadCatalog(const std::string& json);

} // namespace json::reader

} // namespace brave_ads

#endif // BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_CATALOG_CATALOG_URL_REQUEST_JSON_READER_H_
Loading

0 comments on commit 4987e7f

Please sign in to comment.