Files
react-native/Libraries/WebPerformance/__mocks__/NativePerformanceObserver.js
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

102 lines
2.8 KiB
JavaScript

/**
* 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 {
GetPendingEntriesResult,
RawPerformanceEntry,
RawPerformanceEntryType,
Spec as NativePerformanceObserver,
} from '../NativePerformanceObserver';
import {RawPerformanceEntryTypeValues} from '../RawPerformanceEntry';
const reportingType: Set<RawPerformanceEntryType> = new Set();
const eventCounts: Map<string, number> = new Map();
const durationThresholds: Map<RawPerformanceEntryType, number> = new Map();
let entries: Array<RawPerformanceEntry> = [];
let onPerformanceEntryCallback: ?() => void;
const NativePerformanceObserverMock: NativePerformanceObserver = {
startReporting: (entryType: RawPerformanceEntryType) => {
reportingType.add(entryType);
},
stopReporting: (entryType: RawPerformanceEntryType) => {
reportingType.delete(entryType);
durationThresholds.delete(entryType);
},
popPendingEntries: (): GetPendingEntriesResult => {
const res = entries;
entries = [];
return {
droppedEntriesCount: 0,
entries: res,
};
},
setOnPerformanceEntryCallback: (callback?: () => void) => {
onPerformanceEntryCallback = callback;
},
logRawEntry: (entry: RawPerformanceEntry) => {
if (reportingType.has(entry.entryType)) {
const durationThreshold = durationThresholds.get(entry.entryType);
if (
durationThreshold !== undefined &&
entry.duration < durationThreshold
) {
return;
}
entries.push(entry);
// $FlowFixMe[incompatible-call]
global.queueMicrotask(() => {
// We want to emulate the way it's done in native (i.e. async/batched)
onPerformanceEntryCallback?.();
});
}
if (entry.entryType === RawPerformanceEntryTypeValues.EVENT) {
eventCounts.set(entry.name, (eventCounts.get(entry.name) ?? 0) + 1);
}
},
getEventCounts: (): $ReadOnlyArray<[string, number]> => {
return Array.from(eventCounts.entries());
},
setDurationThreshold: (
entryType: RawPerformanceEntryType,
durationThreshold: number,
) => {
durationThresholds.set(entryType, durationThreshold);
},
clearEntries: (entryType: RawPerformanceEntryType, entryName?: string) => {
entries = entries.filter(
e =>
e.entryType === entryType &&
(entryName == null || e.name === entryName),
);
},
getEntries: (
entryType?: RawPerformanceEntryType,
entryName?: string,
): $ReadOnlyArray<RawPerformanceEntry> => {
return entries.filter(
e =>
(entryType == null || e.entryType === entryType) &&
(entryName == null || e.name === entryName),
);
},
};
export default NativePerformanceObserverMock;