mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
cf194aebfe
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/36152 [Changelog][Internal] By [the W3C standard](https://developer.mozilla.org/en-US/docs/Web/API/PerformanceObserver/observe), `PerformanceObserver.observer` can optionally take a `durationThreshold` option, so that only entries with duration larger than the threshold are reported. This diff adds support for this on the RN side, as well as unit tests for this feature on the JS side. NOTE: The standard suggests that default value for this is 104s. I left it at 0 for now, as for the RN use cases t may be to too high (needs discussion). Reviewed By: rubennorte Differential Revision: D43154319 fbshipit-source-id: 0f9d435506f48d8e8521e408211347e8391d22fc
209 lines
5.7 KiB
JavaScript
209 lines
5.7 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.
|
|
*
|
|
* @format
|
|
* @oncall react_native
|
|
*/
|
|
|
|
import {RawPerformanceEntryTypeValues} from '../RawPerformanceEntry';
|
|
|
|
// NOTE: Jest mocks of transitive dependencies don't appear to work with
|
|
// ES6 module imports, therefore forced to use commonjs style imports here.
|
|
const NativePerformanceObserver = require('../NativePerformanceObserver');
|
|
const PerformanceObserver = require('../PerformanceObserver').default;
|
|
|
|
jest.mock(
|
|
'../NativePerformanceObserver',
|
|
() => require('../__mocks__/NativePerformanceObserver').default,
|
|
);
|
|
|
|
describe('PerformanceObserver', () => {
|
|
it('can be mocked by a reference NativePerformanceObserver implementation', async () => {
|
|
expect(NativePerformanceObserver).not.toBe(undefined);
|
|
|
|
let totalEntries = 0;
|
|
const observer = new PerformanceObserver((list, _observer) => {
|
|
expect(_observer).toBe(observer);
|
|
const entries = list.getEntries();
|
|
expect(entries).toHaveLength(1);
|
|
const entry = entries[0];
|
|
expect(entry.name).toBe('mark1');
|
|
expect(entry.entryType).toBe('mark');
|
|
totalEntries += entries.length;
|
|
});
|
|
expect(() => observer.observe({entryTypes: ['mark']})).not.toThrow();
|
|
|
|
NativePerformanceObserver.logRawEntry({
|
|
name: 'mark1',
|
|
entryType: RawPerformanceEntryTypeValues.MARK,
|
|
startTime: 0,
|
|
duration: 0,
|
|
});
|
|
|
|
await jest.runAllTicks();
|
|
expect(totalEntries).toBe(1);
|
|
observer.disconnect();
|
|
});
|
|
|
|
it('prevents durationThreshold to be used together with entryTypes', async () => {
|
|
const observer = new PerformanceObserver((list, _observer) => {});
|
|
|
|
expect(() =>
|
|
observer.observe({entryTypes: ['mark'], durationThreshold: 100}),
|
|
).toThrow();
|
|
});
|
|
|
|
it('handles durationThreshold argument as expected', async () => {
|
|
let entries = [];
|
|
const observer = new PerformanceObserver((list, _observer) => {
|
|
entries = [...entries, ...list.getEntries()];
|
|
});
|
|
|
|
observer.observe({type: 'mark', durationThreshold: 100});
|
|
|
|
NativePerformanceObserver.logRawEntry({
|
|
name: 'mark1',
|
|
entryType: RawPerformanceEntryTypeValues.MARK,
|
|
startTime: 0,
|
|
duration: 200,
|
|
});
|
|
|
|
NativePerformanceObserver.logRawEntry({
|
|
name: 'mark2',
|
|
entryType: RawPerformanceEntryTypeValues.MARK,
|
|
startTime: 0,
|
|
duration: 20,
|
|
});
|
|
|
|
NativePerformanceObserver.logRawEntry({
|
|
name: 'mark3',
|
|
entryType: RawPerformanceEntryTypeValues.MARK,
|
|
startTime: 0,
|
|
duration: 100,
|
|
});
|
|
|
|
NativePerformanceObserver.logRawEntry({
|
|
name: 'mark4',
|
|
entryType: RawPerformanceEntryTypeValues.MARK,
|
|
startTime: 0,
|
|
duration: 500,
|
|
});
|
|
|
|
await jest.runAllTicks();
|
|
expect(entries).toHaveLength(3);
|
|
expect(entries.map(e => e.name)).toStrictEqual(['mark1', 'mark3', 'mark4']);
|
|
});
|
|
|
|
it('correctly works with multiple PerformanceObservers with durationThreshold', async () => {
|
|
let entries1 = [];
|
|
const observer1 = new PerformanceObserver((list, _observer) => {
|
|
entries1 = [...entries1, ...list.getEntries()];
|
|
});
|
|
|
|
let entries2 = [];
|
|
const observer2 = new PerformanceObserver((list, _observer) => {
|
|
entries2 = [...entries2, ...list.getEntries()];
|
|
});
|
|
|
|
let entries3 = [];
|
|
const observer3 = new PerformanceObserver((list, _observer) => {
|
|
entries3 = [...entries3, ...list.getEntries()];
|
|
});
|
|
|
|
let entries4 = [];
|
|
const observer4 = new PerformanceObserver((list, _observer) => {
|
|
entries4 = [...entries4, ...list.getEntries()];
|
|
});
|
|
|
|
observer2.observe({type: 'mark', durationThreshold: 200});
|
|
observer1.observe({type: 'mark', durationThreshold: 100});
|
|
observer3.observe({type: 'mark', durationThreshold: 300});
|
|
observer3.observe({type: 'measure', durationThreshold: 500});
|
|
observer4.observe({entryTypes: ['mark', 'measure']});
|
|
|
|
NativePerformanceObserver.logRawEntry({
|
|
name: 'mark1',
|
|
entryType: RawPerformanceEntryTypeValues.MARK,
|
|
startTime: 0,
|
|
duration: 200,
|
|
});
|
|
|
|
NativePerformanceObserver.logRawEntry({
|
|
name: 'mark2',
|
|
entryType: RawPerformanceEntryTypeValues.MARK,
|
|
startTime: 0,
|
|
duration: 20,
|
|
});
|
|
|
|
NativePerformanceObserver.logRawEntry({
|
|
name: 'mark3',
|
|
entryType: RawPerformanceEntryTypeValues.MARK,
|
|
startTime: 0,
|
|
duration: 100,
|
|
});
|
|
|
|
NativePerformanceObserver.logRawEntry({
|
|
name: 'mark4',
|
|
entryType: RawPerformanceEntryTypeValues.MARK,
|
|
startTime: 0,
|
|
duration: 500,
|
|
});
|
|
|
|
await jest.runAllTicks();
|
|
observer1.disconnect();
|
|
|
|
NativePerformanceObserver.logRawEntry({
|
|
name: 'mark5',
|
|
entryType: RawPerformanceEntryTypeValues.MARK,
|
|
startTime: 0,
|
|
duration: 200,
|
|
});
|
|
|
|
NativePerformanceObserver.logRawEntry({
|
|
name: 'mark6',
|
|
entryType: RawPerformanceEntryTypeValues.MARK,
|
|
startTime: 0,
|
|
duration: 300,
|
|
});
|
|
|
|
await jest.runAllTicks();
|
|
observer3.disconnect();
|
|
|
|
NativePerformanceObserver.logRawEntry({
|
|
name: 'mark7',
|
|
entryType: RawPerformanceEntryTypeValues.MARK,
|
|
startTime: 0,
|
|
duration: 200,
|
|
});
|
|
|
|
await jest.runAllTicks();
|
|
observer4.disconnect();
|
|
|
|
expect(entries1.map(e => e.name)).toStrictEqual([
|
|
'mark1',
|
|
'mark3',
|
|
'mark4',
|
|
]);
|
|
expect(entries2.map(e => e.name)).toStrictEqual([
|
|
'mark1',
|
|
'mark4',
|
|
'mark5',
|
|
'mark6',
|
|
'mark7',
|
|
]);
|
|
expect(entries3.map(e => e.name)).toStrictEqual(['mark4', 'mark6']);
|
|
expect(entries4.map(e => e.name)).toStrictEqual([
|
|
'mark1',
|
|
'mark2',
|
|
'mark3',
|
|
'mark4',
|
|
'mark5',
|
|
'mark6',
|
|
'mark7',
|
|
]);
|
|
});
|
|
});
|