Create NativePerformance C++ module

Summary:
[Changelog][Internal]

The NativePerformance module functionality corresponds to the [timing extensions of the Performance API standard interface](https://www.w3.org/TR/user-timing/#extensions-performance-interface).

As this is logically separate from `PerformanceObserver` (which may exist without it), it makes sense to have it as a different native module, so there is no coupling between both.

Reviewed By: christophpurrer

Differential Revision: D41690145

fbshipit-source-id: 7443f4c51f54cc2fdddbdb2e89f9a1fa457ab280
This commit is contained in:
Ruslan Shestopalyuk
2022-12-03 08:32:58 -08:00
committed by Facebook GitHub Bot
parent 1c7e678e08
commit cb552f62f2
9 changed files with 120 additions and 32 deletions
@@ -0,0 +1,25 @@
/*
* 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 "NativePerformance.h"
#include <glog/logging.h>
#include "PerformanceEntryReporter.h"
namespace facebook::react {
NativePerformance::NativePerformance(std::shared_ptr<CallInvoker> jsInvoker)
: NativePerformanceCxxSpec(std::move(jsInvoker)) {}
void NativePerformance::mark(
jsi::Runtime &rt,
std::string name,
double startTime,
double duration) {
PerformanceEntryReporter::getInstance().mark(name, startTime, duration);
}
} // namespace facebook::react
@@ -0,0 +1,34 @@
/*
* 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.
*/
#pragma once
#include <FBReactNativeSpec/FBReactNativeSpecJSI.h>
#include <memory>
#include <string>
#include "NativePerformanceObserver.h"
namespace facebook::react {
class PerformanceEntryReporter;
#pragma mark - Structs
#pragma mark - implementation
class NativePerformance : public NativePerformanceCxxSpec<NativePerformance>,
std::enable_shared_from_this<NativePerformance> {
public:
NativePerformance(std::shared_ptr<CallInvoker> jsInvoker);
void
mark(jsi::Runtime &rt, std::string name, double startTime, double duration);
private:
};
} // namespace facebook::react
@@ -0,0 +1,19 @@
/**
* 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.
*
* @flow strict
* @format
*/
import type {TurboModule} from '../TurboModule/RCTExport';
import * as TurboModuleRegistry from '../TurboModule/TurboModuleRegistry';
export interface Spec extends TurboModule {
+mark?: (name: string, startTime: number, duration: number) => void;
}
export default (TurboModuleRegistry.get<Spec>('NativePerformanceCxx'): ?Spec);
@@ -22,43 +22,36 @@ static PerformanceEntryType stringToPerformanceEntryType(
NativePerformanceObserver::NativePerformanceObserver(
std::shared_ptr<CallInvoker> jsInvoker)
: NativePerformanceObserverCxxSpec(std::move(jsInvoker)),
reporter_(std::make_unique<PerformanceEntryReporter>()) {}
NativePerformanceObserver::~NativePerformanceObserver() {}
: NativePerformanceObserverCxxSpec(std::move(jsInvoker)) {}
void NativePerformanceObserver::startReporting(
jsi::Runtime &rt,
std::string entryType) {
reporter_->startReporting(stringToPerformanceEntryType(entryType));
PerformanceEntryReporter::getInstance().startReporting(
stringToPerformanceEntryType(entryType));
}
void NativePerformanceObserver::stopReporting(
jsi::Runtime &rt,
std::string entryType) {
reporter_->stopReporting(stringToPerformanceEntryType(entryType));
PerformanceEntryReporter::getInstance().stopReporting(
stringToPerformanceEntryType(entryType));
}
std::vector<RawPerformanceEntry> NativePerformanceObserver::popPendingEntries(
jsi::Runtime &rt) {
return reporter_->popPendingEntries();
return PerformanceEntryReporter::getInstance().popPendingEntries();
}
std::vector<RawPerformanceEntry> NativePerformanceObserver::getPendingEntries(
jsi::Runtime &rt) {
return reporter_->getPendingEntries();
return PerformanceEntryReporter::getInstance().getPendingEntries();
}
void NativePerformanceObserver::setOnPerformanceEntryCallback(
jsi::Runtime &rt,
std::optional<AsyncCallback<>> callback) {
reporter_->setReportingCallback(callback);
}
void NativePerformanceObserver::logEntryForDebug(
jsi::Runtime &rt,
RawPerformanceEntry entry) {
reporter_->logEntry(entry);
PerformanceEntryReporter::getInstance().setReportingCallback(callback);
}
} // namespace facebook::react
@@ -9,7 +9,6 @@
#include <FBReactNativeSpec/FBReactNativeSpecJSI.h>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <vector>
@@ -47,7 +46,6 @@ class NativePerformanceObserver
std::enable_shared_from_this<NativePerformanceObserver> {
public:
NativePerformanceObserver(std::shared_ptr<CallInvoker> jsInvoker);
~NativePerformanceObserver();
void startReporting(jsi::Runtime &rt, std::string entryType);
@@ -60,10 +58,7 @@ class NativePerformanceObserver
jsi::Runtime &rt,
std::optional<AsyncCallback<>> callback);
void logEntryForDebug(jsi::Runtime &rt, RawPerformanceEntry entry);
private:
std::unique_ptr<PerformanceEntryReporter> reporter_;
};
} // namespace facebook::react
@@ -36,9 +36,6 @@ export interface Spec extends TurboModule {
+getPendingEntries: () => $ReadOnlyArray<RawPerformanceEntry>;
+popPendingEntries?: () => $ReadOnlyArray<RawPerformanceEntry>;
+setOnPerformanceEntryCallback: (callback?: () => void) => void;
// NOTE: this is for dev-only purposes (potentially is going to be moved elsewhere)
+logEntryForDebug?: (entry: RawPerformanceEntry) => void;
}
export default (TurboModuleRegistry.get<Spec>(
+2 -9
View File
@@ -10,9 +10,7 @@
import type {HighResTimeStamp} from './PerformanceObserver';
import NativePerformanceObserver, {
RawPerformanceEntryTypeValues,
} from './NativePerformanceObserver';
import NativePerformance from './NativePerformance';
import {PerformanceEntry} from './PerformanceObserver';
type DetailType = mixed;
@@ -49,12 +47,7 @@ export default class Performance {
markOptions?: PerformanceMarkOptions,
): PerformanceMark {
const mark = new PerformanceMark(markName, markOptions);
NativePerformanceObserver?.logEntryForDebug?.({
name: markName,
entryType: RawPerformanceEntryTypeValues.MARK,
startTime: mark.startTime,
duration: mark.duration,
});
NativePerformance?.mark?.(markName, mark.startTime, mark.duration);
return mark;
}
@@ -11,6 +11,11 @@
#include "NativePerformanceObserver.h"
namespace facebook::react {
PerformanceEntryReporter &PerformanceEntryReporter::getInstance() {
static PerformanceEntryReporter instance;
return instance;
}
void PerformanceEntryReporter::setReportingCallback(
std::optional<AsyncCallback<>> callback) {
callback_ = callback;
@@ -49,4 +54,18 @@ void PerformanceEntryReporter::logEntry(const RawPerformanceEntry &entry) {
// now
callback_->callWithPriority(SchedulerPriority::IdlePriority);
}
void PerformanceEntryReporter::mark(
const std::string &name,
double startTime,
double duration) {
logEntry(
{name,
static_cast<int>(PerformanceEntryType::MARK),
startTime,
duration,
std::nullopt,
std::nullopt,
std::nullopt});
}
} // namespace facebook::react
@@ -22,6 +22,15 @@ enum class PerformanceEntryType {
class PerformanceEntryReporter {
public:
PerformanceEntryReporter(PerformanceEntryReporter const &) = delete;
void operator=(PerformanceEntryReporter const &) = delete;
// NOTE: This class is not thread safe, make sure that the calls are made from
// the same thread.
// TODO: Consider passing it as a parameter to the corresponding modules at
// creation time instead of having the singleton.
static PerformanceEntryReporter &getInstance();
void setReportingCallback(std::optional<AsyncCallback<>> callback);
void startReporting(PerformanceEntryType entryType);
void stopReporting(PerformanceEntryType entryType);
@@ -35,7 +44,11 @@ class PerformanceEntryReporter {
return reportingType_[static_cast<int>(entryType)];
}
void mark(const std::string &name, double startTime, double duration);
private:
PerformanceEntryReporter() {}
std::optional<AsyncCallback<>> callback_;
std::vector<RawPerformanceEntry> entries_;
std::array<bool, (size_t)PerformanceEntryType::_COUNT> reportingType_{false};