Skip to content

Commit

Permalink
[ads] Profile database performance using TRACE events
Browse files Browse the repository at this point in the history
  • Loading branch information
tmancey committed Jan 10, 2025
1 parent f382995 commit d908392
Show file tree
Hide file tree
Showing 25 changed files with 232 additions and 84 deletions.
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 @@ -12,6 +12,7 @@
#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/common/database/database_column_util.h"
Expand Down Expand Up @@ -208,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 @@ -239,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 @@ -280,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 @@ -295,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 @@ -5,22 +5,58 @@

#include "brave/components/brave_ads/core/internal/common/database/database_transaction_util.h"

#include <cstdint>
#include <utility>

#include "base/functional/bind.h"
#include "base/strings/string_util.h"
#include "base/trace_event/trace_event.h"
#include "base/trace_event/trace_id_helper.h"
#include "brave/components/brave_ads/core/internal/database/database_manager.h"
#include "brave/components/brave_ads/core/internal/global_state/global_state.h"
#include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h"
#include "brave/components/brave_ads/core/public/ads_client/ads_client_callback.h"
#include "brave/components/brave_ads/core/public/ads_constants.h"

namespace brave_ads::database {

namespace {

void RunDBTransactionCallback(
::brave_ads::RunDBTransactionCallback callback,
uint64_t trace_id,
mojom::DBTransactionResultInfoPtr mojom_db_transaction_result) {
if (mojom_db_transaction_result) {
TRACE_EVENT_NESTABLE_ASYNC_END2(
kTraceEventCategory, "RunDBTransaction",
TRACE_ID_WITH_SCOPE("Database", trace_id), "statusCode",
mojom_db_transaction_result->status_code, "rowCount",
mojom_db_transaction_result->rows_union->get_rows().size());
} else {
TRACE_EVENT_NESTABLE_ASYNC_END1(kTraceEventCategory, "RunDBTransaction",
TRACE_ID_WITH_SCOPE("Database", trace_id),
"mojom_db_transaction_result", "nullptr");
}

std::move(callback).Run(std::move(mojom_db_transaction_result));
}

void RunDBTransactionForSuccessOrFailureCallback(
ResultCallback callback,
uint64_t trace_id,
mojom::DBTransactionResultInfoPtr mojom_db_transaction_result) {
if (mojom_db_transaction_result) {
TRACE_EVENT_NESTABLE_ASYNC_END2(
kTraceEventCategory, "RunDBTransaction",
TRACE_ID_WITH_SCOPE("Database", trace_id), "statusCode",
mojom_db_transaction_result->status_code, "rowCount",
mojom_db_transaction_result->rows_union->get_rows().size());
} else {
TRACE_EVENT_NESTABLE_ASYNC_END1(kTraceEventCategory, "RunDBTransaction",
TRACE_ID_WITH_SCOPE("Database", trace_id),
"mojom_db_transaction_result", "nullptr");
}

if (IsError(mojom_db_transaction_result)) {
return std::move(callback).Run(/*success=*/false);
}
Expand All @@ -44,17 +80,33 @@ bool IsError(
mojom::DBTransactionResultInfo::StatusCode::kSuccess;
}

void RunDBTransaction(mojom::DBTransactionInfoPtr mojom_db_transaction,
void RunDBTransaction(const base::Location& location,
mojom::DBTransactionInfoPtr mojom_db_transaction,
::brave_ads::RunDBTransactionCallback callback) {
const uint64_t trace_id = base::trace_event::GetNextGlobalTraceId();
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(kTraceEventCategory, "RunDBTransaction",
TRACE_ID_WITH_SCOPE("Database", trace_id),
"location", location.ToString());

GlobalState::GetInstance()->GetDatabaseManager().RunDBTransaction(
std::move(mojom_db_transaction), std::move(callback));
std::move(mojom_db_transaction),
base::BindOnce(&RunDBTransactionCallback, std::move(callback), trace_id),
trace_id);
}

void RunDBTransaction(mojom::DBTransactionInfoPtr mojom_db_transaction,
void RunDBTransaction(const base::Location& location,
mojom::DBTransactionInfoPtr mojom_db_transaction,
ResultCallback callback) {
const uint64_t trace_id = base::trace_event::GetNextGlobalTraceId();
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(kTraceEventCategory, "RunDBTransaction",
TRACE_ID_WITH_SCOPE("Database", trace_id),
"location", location.ToString());

GlobalState::GetInstance()->GetDatabaseManager().RunDBTransaction(
std::move(mojom_db_transaction),
base::BindOnce(&RunDBTransactionCallback, std::move(callback)));
base::BindOnce(&RunDBTransactionForSuccessOrFailureCallback,
std::move(callback), trace_id),
trace_id);
}

void Raze(const mojom::DBTransactionInfoPtr& mojom_db_transaction) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string>
#include <vector>

#include "base/location.h"
#include "brave/components/brave_ads/core/mojom/brave_ads.mojom-forward.h"
#include "brave/components/brave_ads/core/public/ads_client/ads_client_callback.h"

Expand All @@ -24,11 +25,13 @@ bool IsError(

// Run a database transaction. The callback takes one argument -
// `mojom::DBTransactionResultInfoPtr` containing the info of the transaction.
void RunDBTransaction(mojom::DBTransactionInfoPtr mojom_db_transaction,
void RunDBTransaction(const base::Location& location,
mojom::DBTransactionInfoPtr mojom_db_transaction,
RunDBTransactionCallback callback);

// Run a database transaction.
void RunDBTransaction(mojom::DBTransactionInfoPtr mojom_db_transaction,
void RunDBTransaction(const base::Location& location,
mojom::DBTransactionInfoPtr mojom_db_transaction,
ResultCallback callback);

// Raze the database. This must be done before any other actions are run. All
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <utility>

#include "base/check.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 @@ -54,7 +55,8 @@ void Campaigns::Delete(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 Campaigns::Insert(const mojom::DBTransactionInfoPtr& mojom_db_transaction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#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/common/database/database_column_util.h"
Expand Down Expand Up @@ -192,7 +193,8 @@ void CreativeSetConversions::Save(

Insert(mojom_db_transaction, creative_set_conversions);

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

void CreativeSetConversions::GetUnexpired(
Expand All @@ -217,7 +219,7 @@ void CreativeSetConversions::GetUnexpired(
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 @@ -248,7 +250,7 @@ void CreativeSetConversions::GetActive(
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 All @@ -262,7 +264,8 @@ void CreativeSetConversions::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 CreativeSetConversions::GetTableName() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "base/check.h"
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "brave/components/brave_ads/core/internal/common/database/database_column_util.h"
Expand Down Expand Up @@ -177,7 +178,8 @@ void CreativeAds::Delete(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 CreativeAds::GetForCreativeInstanceId(
Expand Down Expand Up @@ -214,7 +216,7 @@ void CreativeAds::GetForCreativeInstanceId(
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <utility>

#include "base/check.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_table_util.h"
Expand Down Expand Up @@ -65,7 +66,8 @@ void Dayparts::Delete(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));
}

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

#include "base/check.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_table_util.h"
Expand Down Expand Up @@ -63,7 +64,8 @@ void GeoTargets::Delete(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));
}

std::string GeoTargets::GetTableName() const {
Expand Down
Loading

0 comments on commit d908392

Please sign in to comment.