Files
react-native/Libraries/WebPerformance/NativePerformanceObserver.cpp
T
Ruslan Shestopalyuk bc56f66b8d Implement Performance.getEntries* W3C extension for Performance Timeline API (#36314)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/36314

## Changelog:

[Internal] - Added support for W3C Performance API extension (Performance,getEntries*)

See [here for the details](https://www.w3.org/TR/performance-timeline/#extensions-to-the-performance-interface).

This only supports `mark` and `measure` performance entry types (not `events`, as those are not supported by browsers either, they recommend using the `PerformanceObserver` API instead).

From the implementation perspective, we already maintained persistent buffer for `mark` entry types, which was required in order to look up measures.

The buffer is circular, limited to 1K entries by default.

I basically mimic the same behavior for `measure` entry types as well, since it's the simplest way of doing it at this point,

If we happen to want adding some other entry types that would be available via `Performance.getEntries*` API in the future, we may want to iterate on the internal data structures representation inside `PerformanceEntryReporter`, but for now I believe this approach should work just fine, and whatever changes we may want to do will be purely internal implementation detail.

Reviewed By: rubennorte

Differential Revision: D43625488

fbshipit-source-id: dd315b3f8488e910749a8e2a4158246e94d76f99
2023-03-01 04:14:51 -08:00

101 lines
3.1 KiB
C++

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <memory>
#include "NativePerformanceObserver.h"
#include "PerformanceEntryReporter.h"
#include "Plugins.h"
std::shared_ptr<facebook::react::TurboModule>
NativePerformanceObserverModuleProvider(
std::shared_ptr<facebook::react::CallInvoker> jsInvoker) {
return std::make_shared<facebook::react::NativePerformanceObserver>(
std::move(jsInvoker));
}
namespace facebook::react {
NativePerformanceObserver::NativePerformanceObserver(
std::shared_ptr<CallInvoker> jsInvoker)
: NativePerformanceObserverCxxSpec(std::move(jsInvoker)) {
setEventLogger(&PerformanceEntryReporter::getInstance());
}
NativePerformanceObserver::~NativePerformanceObserver() {
setEventLogger(nullptr);
}
void NativePerformanceObserver::startReporting(
jsi::Runtime &rt,
int32_t entryType) {
PerformanceEntryReporter::getInstance().startReporting(
static_cast<PerformanceEntryType>(entryType));
}
void NativePerformanceObserver::stopReporting(
jsi::Runtime &rt,
int32_t entryType) {
PerformanceEntryReporter::getInstance().stopReporting(
static_cast<PerformanceEntryType>(entryType));
}
GetPendingEntriesResult NativePerformanceObserver::popPendingEntries(
jsi::Runtime &rt) {
return PerformanceEntryReporter::getInstance().popPendingEntries();
}
void NativePerformanceObserver::setOnPerformanceEntryCallback(
jsi::Runtime &rt,
std::optional<AsyncCallback<>> callback) {
PerformanceEntryReporter::getInstance().setReportingCallback(callback);
}
void NativePerformanceObserver::logRawEntry(
jsi::Runtime &rt,
RawPerformanceEntry entry) {
PerformanceEntryReporter::getInstance().logEntry(entry);
}
std::vector<std::pair<std::string, uint32_t>>
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,
double durationThreshold) {
PerformanceEntryReporter::getInstance().setDurationThreshold(
static_cast<PerformanceEntryType>(entryType), durationThreshold);
}
void NativePerformanceObserver::clearEntries(
jsi::Runtime &rt,
int32_t entryType,
std::optional<std::string> entryName) {
PerformanceEntryReporter::getInstance().clearEntries(
static_cast<PerformanceEntryType>(entryType),
entryName ? entryName->c_str() : nullptr);
}
std::vector<RawPerformanceEntry> NativePerformanceObserver::getEntries(
jsi::Runtime &rt,
std::optional<int32_t> entryType,
std::optional<std::string> entryName) {
return PerformanceEntryReporter::getInstance().getEntries(
entryType ? static_cast<PerformanceEntryType>(*entryType)
: PerformanceEntryType::UNDEFINED,
entryName ? entryName->c_str() : nullptr);
}
} // namespace facebook::react