Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <memory>

#include <cxxreact/JSExecutor.h>
#include <cxxreact/ReactMarker.h>
#include <jsi/instrumentation.h>
#include "NativePerformance.h"
Expand All @@ -25,6 +26,10 @@ namespace facebook::react {
NativePerformance::NativePerformance(std::shared_ptr<CallInvoker> jsInvoker)
: NativePerformanceCxxSpec(std::move(jsInvoker)) {}

double NativePerformance::now(jsi::Runtime& /*rt*/) {
return JSExecutor::performanceNow();
}

void NativePerformance::mark(
jsi::Runtime& rt,
std::string name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class NativePerformance : public NativePerformanceCxxSpec<NativePerformance>,
public:
NativePerformance(std::shared_ptr<CallInvoker> jsInvoker);

double now(jsi::Runtime& rt);

void mark(jsi::Runtime& rt, std::string name, double startTime);

void measure(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@

import type {HighResTimeStamp, PerformanceEntryType} from './PerformanceEntry';
import type {PerformanceEntryList} from './PerformanceObserver';
import type {
PerformanceMarkOptions,
PerformanceMeasureOptions,
} from './UserTiming';

import warnOnce from '../../../../Libraries/Utilities/warnOnce';
import EventCounts from './EventCounts';
import MemoryInfo from './MemoryInfo';
import {ALWAYS_LOGGED_ENTRY_TYPES, PerformanceEntry} from './PerformanceEntry';
import {ALWAYS_LOGGED_ENTRY_TYPES} from './PerformanceEntry';
import {warnNoNativePerformanceObserver} from './PerformanceObserver';
import {
performanceEntryTypeToRaw,
Expand All @@ -26,22 +30,15 @@ import {RawPerformanceEntryTypeValues} from './RawPerformanceEntry';
import ReactNativeStartupTiming from './ReactNativeStartupTiming';
import NativePerformance from './specs/NativePerformance';
import NativePerformanceObserver from './specs/NativePerformanceObserver';

type DetailType = mixed;

export type PerformanceMarkOptions = {
detail?: DetailType,
startTime?: HighResTimeStamp,
};
import {PerformanceMark, PerformanceMeasure} from './UserTiming';

declare var global: {
// This value is defined directly via JSI, if available.
+nativePerformanceNow?: ?() => number,
};

const getCurrentTimeStamp: () => HighResTimeStamp = global.nativePerformanceNow
? global.nativePerformanceNow
: () => Date.now();
const getCurrentTimeStamp: () => HighResTimeStamp =
NativePerformance?.now ?? global.nativePerformanceNow ?? (() => Date.now());

// We want some of the performance entry types to be always logged,
// even if they are not currently observed - this is either to be able to
Expand All @@ -54,49 +51,6 @@ if (NativePerformanceObserver?.setIsBuffered) {
);
}

export class PerformanceMark extends PerformanceEntry {
detail: DetailType;

constructor(markName: string, markOptions?: PerformanceMarkOptions) {
super({
name: markName,
entryType: 'mark',
startTime: markOptions?.startTime ?? getCurrentTimeStamp(),
duration: 0,
});

if (markOptions) {
this.detail = markOptions.detail;
}
}
}

export type TimeStampOrName = HighResTimeStamp | string;

export type PerformanceMeasureOptions = {
detail?: DetailType,
start?: TimeStampOrName,
end?: TimeStampOrName,
duration?: HighResTimeStamp,
};

export class PerformanceMeasure extends PerformanceEntry {
detail: DetailType;

constructor(measureName: string, measureOptions?: PerformanceMeasureOptions) {
super({
name: measureName,
entryType: 'measure',
startTime: 0,
duration: measureOptions?.duration ?? 0,
});

if (measureOptions) {
this.detail = measureOptions.detail;
}
}
}

function warnNoNativePerformance() {
warnOnce(
'missing-native-performance',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* 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.
*
* @format
* @flow strict
*/

import type {HighResTimeStamp} from './PerformanceEntry';

import {PerformanceEntry} from './PerformanceEntry';

type DetailType = mixed;

export type PerformanceMarkOptions = {
detail?: DetailType,
startTime?: HighResTimeStamp,
};

export type TimeStampOrName = HighResTimeStamp | string;

export type PerformanceMeasureOptions = {
detail?: DetailType,
start?: TimeStampOrName,
end?: TimeStampOrName,
duration?: HighResTimeStamp,
};

export class PerformanceMark extends PerformanceEntry {
detail: DetailType;

constructor(markName: string, markOptions?: PerformanceMarkOptions) {
super({
name: markName,
entryType: 'mark',
startTime: markOptions?.startTime ?? performance.now(),
duration: 0,
});

if (markOptions) {
this.detail = markOptions.detail;
}
}
}

export class PerformanceMeasure extends PerformanceEntry {
detail: DetailType;

constructor(measureName: string, measureOptions?: PerformanceMeasureOptions) {
super({
name: measureName,
entryType: 'measure',
startTime: 0,
duration: measureOptions?.duration ?? 0,
});

if (measureOptions) {
this.detail = measureOptions.detail;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type NativeMemoryInfo = {[key: string]: ?number};
export type ReactNativeStartupTiming = {[key: string]: ?number};

export interface Spec extends TurboModule {
+now?: () => number;
+mark: (name: string, startTime: number) => void;
+measure: (
name: string,
Expand Down