Skip to content

Commit

Permalink
Decouple PerformanceEntryReporter from NativePerformanceObserver
Browse files Browse the repository at this point in the history
Differential Revision: D55646394
  • Loading branch information
rubennorte authored and facebook-github-bot committed Apr 4, 2024
1 parent 196a9fe commit 2e0e8f4
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,12 @@ NativePerformanceObserver::~NativePerformanceObserver() {

void NativePerformanceObserver::startReporting(
jsi::Runtime& rt,
int32_t entryType) {
PerformanceEntryType entryType) {
PerformanceEntryReporter& reporter = PerformanceEntryReporter::getInstance();

PerformanceEntryType entryTypeEnum =
static_cast<PerformanceEntryType>(entryType);
reporter.startReporting(entryTypeEnum);
reporter.startReporting(entryType);

if (entryTypeEnum == PerformanceEntryType::EVENT &&
if (entryType == PerformanceEntryType::EVENT &&
CoreFeatures::enableReportEventPaintTime) {
UIManagerBinding::getBinding(rt)->getUIManager().registerMountHook(
reporter);
Expand All @@ -52,88 +50,84 @@ void NativePerformanceObserver::startReporting(

void NativePerformanceObserver::stopReporting(
jsi::Runtime& rt,
int32_t entryType) {
PerformanceEntryType entryType) {
PerformanceEntryReporter& reporter = PerformanceEntryReporter::getInstance();

PerformanceEntryType entryTypeEnum =
static_cast<PerformanceEntryType>(entryType);
reporter.stopReporting(entryTypeEnum);
reporter.stopReporting(entryType);

if (entryTypeEnum == PerformanceEntryType::EVENT &&
if (entryType == PerformanceEntryType::EVENT &&
CoreFeatures::enableReportEventPaintTime) {
UIManagerBinding::getBinding(rt)->getUIManager().unregisterMountHook(
reporter);
}
}

void NativePerformanceObserver::setIsBuffered(
jsi::Runtime& rt,
std::vector<int32_t> entryTypes,
jsi::Runtime& /*rt*/,
const std::vector<PerformanceEntryType> entryTypes,
bool isBuffered) {
for (const int32_t entryType : entryTypes) {
for (const PerformanceEntryType entryType : entryTypes) {
PerformanceEntryReporter::getInstance().setAlwaysLogged(
static_cast<PerformanceEntryType>(entryType), isBuffered);
entryType, isBuffered);
}
}

GetPendingEntriesResult NativePerformanceObserver::popPendingEntries(
jsi::Runtime& rt) {
PerformanceEntryReporter::PopPendingEntriesResult
NativePerformanceObserver::popPendingEntries(jsi::Runtime& /*rt*/) {
return PerformanceEntryReporter::getInstance().popPendingEntries();
}

void NativePerformanceObserver::setOnPerformanceEntryCallback(
jsi::Runtime& rt,
jsi::Runtime& /*rt*/,
std::optional<AsyncCallback<>> callback) {
PerformanceEntryReporter::getInstance().setReportingCallback(callback);
}

void NativePerformanceObserver::logRawEntry(
jsi::Runtime& rt,
RawPerformanceEntry entry) {
jsi::Runtime& /*rt*/,
const PerformanceEntry entry) {
PerformanceEntryReporter::getInstance().logEntry(entry);
}

std::vector<std::pair<std::string, uint32_t>>
NativePerformanceObserver::getEventCounts(jsi::Runtime& rt) {
NativePerformanceObserver::getEventCounts(jsi::Runtime& /*rt*/) {
const auto& eventCounts =
PerformanceEntryReporter::getInstance().getEventCounts();
return std::vector<std::pair<std::string, uint32_t>>(
eventCounts.begin(), eventCounts.end());
}

void NativePerformanceObserver::setDurationThreshold(
jsi::Runtime& rt,
int32_t entryType,
jsi::Runtime& /*rt*/,
PerformanceEntryType entryType,
double durationThreshold) {
PerformanceEntryReporter::getInstance().setDurationThreshold(
static_cast<PerformanceEntryType>(entryType), durationThreshold);
entryType, durationThreshold);
}

void NativePerformanceObserver::clearEntries(
jsi::Runtime& rt,
int32_t entryType,
jsi::Runtime& /*rt*/,
PerformanceEntryType entryType,
std::optional<std::string> entryName) {
PerformanceEntryReporter::getInstance().clearEntries(
static_cast<PerformanceEntryType>(entryType),
entryName ? entryName->c_str() : nullptr);
entryType, entryName ? entryName->c_str() : std::string_view{});
}

std::vector<RawPerformanceEntry> NativePerformanceObserver::getEntries(
jsi::Runtime& rt,
std::optional<int32_t> entryType,
std::vector<PerformanceEntry> NativePerformanceObserver::getEntries(
jsi::Runtime& /*rt*/,
std::optional<PerformanceEntryType> entryType,
std::optional<std::string> entryName) {
return PerformanceEntryReporter::getInstance().getEntries(
entryType ? std::optional{static_cast<PerformanceEntryType>(*entryType)}
: std::nullopt,
entryName ? entryName->c_str() : nullptr);
entryType, entryName ? entryName->c_str() : std::string_view{});
}

std::vector<RawPerformanceEntryType>
NativePerformanceObserver::getSupportedPerformanceEntryTypes(jsi::Runtime& rt) {
std::vector<PerformanceEntryType>
NativePerformanceObserver::getSupportedPerformanceEntryTypes(
jsi::Runtime& /*rt*/) {
return {
static_cast<RawPerformanceEntryType>(PerformanceEntryType::MARK),
static_cast<RawPerformanceEntryType>(PerformanceEntryType::MEASURE),
static_cast<RawPerformanceEntryType>(PerformanceEntryType::EVENT),
PerformanceEntryType::MARK,
PerformanceEntryType::MEASURE,
PerformanceEntryType::EVENT,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,36 @@
#include <optional>
#include <string>
#include <vector>
#include "PerformanceEntryReporter.h"

namespace facebook::react {
class PerformanceEntryReporter;

#pragma mark - Structs

using RawPerformanceEntryType = int32_t;

using RawPerformanceEntry = NativePerformanceObserverCxxRawPerformanceEntry<
/* name */ std::string,
/* type */ RawPerformanceEntryType,
/* startTime */ double,
/* duration */ double,

// For "event" entries only:
/* processingStart */ std::optional<double>,
/* processingEnd */ std::optional<double>,
/* interactionId */ std::optional<uint32_t>>;
template <>
struct Bridging<PerformanceEntryType> {
static PerformanceEntryType fromJs(
jsi::Runtime& /*rt*/,
const jsi::Value& value) {
return static_cast<PerformanceEntryType>(value.asNumber());
}

static jsi::Value toJs(
jsi::Runtime& /*rt*/,
const PerformanceEntryType& value) {
return {static_cast<int>(value)};
}
};

template <>
struct Bridging<RawPerformanceEntry>
struct Bridging<PerformanceEntry>
: NativePerformanceObserverCxxRawPerformanceEntryBridging<
RawPerformanceEntry> {};

using GetPendingEntriesResult =
NativePerformanceObserverCxxGetPendingEntriesResult<
std::vector<RawPerformanceEntry>,
uint32_t>;
PerformanceEntry> {};

template <>
struct Bridging<GetPendingEntriesResult>
struct Bridging<PerformanceEntryReporter::PopPendingEntriesResult>
: NativePerformanceObserverCxxGetPendingEntriesResultBridging<
GetPendingEntriesResult> {};
PerformanceEntryReporter::PopPendingEntriesResult> {};

#pragma mark - implementation

Expand All @@ -54,45 +51,44 @@ class NativePerformanceObserver
NativePerformanceObserver(std::shared_ptr<CallInvoker> jsInvoker);
~NativePerformanceObserver();

void startReporting(jsi::Runtime& rt, int32_t entryType);
void startReporting(jsi::Runtime& rt, PerformanceEntryType entryType);

void stopReporting(jsi::Runtime& rt, int32_t entryType);
void stopReporting(jsi::Runtime& rt, PerformanceEntryType entryType);

void setIsBuffered(
jsi::Runtime& rt,
std::vector<int32_t> entryTypes,
const std::vector<PerformanceEntryType> entryTypes,
bool isBuffered);

GetPendingEntriesResult popPendingEntries(jsi::Runtime& rt);
PerformanceEntryReporter::PopPendingEntriesResult popPendingEntries(
jsi::Runtime& rt);

void setOnPerformanceEntryCallback(
jsi::Runtime& rt,
std::optional<AsyncCallback<>> callback);

void logRawEntry(jsi::Runtime& rt, RawPerformanceEntry entry);
void logRawEntry(jsi::Runtime& rt, const PerformanceEntry entry);

std::vector<std::pair<std::string, uint32_t>> getEventCounts(
jsi::Runtime& rt);

void setDurationThreshold(
jsi::Runtime& rt,
int32_t entryType,
double durationThreshold);
PerformanceEntryType entryType,
DOMHighResTimeStamp durationThreshold);

void clearEntries(
jsi::Runtime& rt,
int32_t entryType,
PerformanceEntryType entryType,
std::optional<std::string> entryName);

std::vector<RawPerformanceEntry> getEntries(
std::vector<PerformanceEntry> getEntries(
jsi::Runtime& rt,
std::optional<int32_t> entryType,
std::optional<PerformanceEntryType> entryType,
std::optional<std::string> entryName);

std::vector<RawPerformanceEntryType> getSupportedPerformanceEntryTypes(
std::vector<PerformanceEntryType> getSupportedPerformanceEntryTypes(
jsi::Runtime& rt);

private:
};

} // namespace facebook::react
Loading

0 comments on commit 2e0e8f4

Please sign in to comment.