Files
react-native/Libraries/WebPerformance/NativePerformanceObserver.cpp
T
Ruslan Shestopalyuk 581357bc9b Implement EventCounts Web Performance API for React Native (#36181)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/36181

[Changelog][Internal]

Implements EventCounts API (`Performance.eventCounts`) for Web Performance, according to the W3C standard, see the specs here: https://www.w3.org/TR/event-timing/#eventcounts

The rationale for why we need it is to support some advanced metrics computations, such as a ratio of "slow events" to total event count, per event type.

Reviewed By: rubennorte

Differential Revision: D43285073

fbshipit-source-id: 2c53d04d9a57c1301e37f2a5879072c8d33efbbf
2023-02-16 06:21:43 -08:00

63 lines
1.9 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 "NativePerformanceObserver.h"
#include "PerformanceEntryReporter.h"
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());
}
} // namespace facebook::react