Files
mostlymatter/webapp/channels/src/tests/performance_mock.ts
Harrison Healey de3c7ad544 MM-57878 Add PerformanceReporter for clientside performance metrics (#26800)
* Define rough code for PerformanceReporter

* Create a component to manage the PerformanceReporter

* Start adding tests for PerformanceReporter

* Add test for web vitals reporting

* Update schema to more closely match the API spec

* Collect marks as counters and further update structure of API payload

* Add some outstanding TODOs about the API structure

* Add counter for long tasks

* Add EnableClientMetrics without any System Console UI

* Have PerformanceReporter use EnableClientMetrics

* Have the PerformanceReporter only report results when logged in

* Add test for having PerformanceReporter fall back to fetch

* Stop logging errors for measurements failing

* Remove buffered from observer

* Remove the Mystery Ampersand

* Still record marks with telemetry actions even if telemetry is disabled

* Add timestamps to performance reports

* Reuse the new telemetry code for the old telemetry

* The second half of the last commit

* Use Node performance libraries in all tests

* Set version of PerformanceReport

* Switch to the proper version of EnableClientMetrics

* Remove TODO for unneeded field

* Add user agent and platform detection

* Updated metrics API route
2024-05-09 12:50:52 -04:00

30 строки
1.2 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {PerformanceObserver as NodePerformanceObserver, performance as nodePerformance} from 'node:perf_hooks';
// These aren't a perfect match for window.performance and PerformanceObserver, but they're close enough. They don't
// work with `jest.useFakeTimers` because that overwrites window.performance in a way that breaks the Node.js version.
//
// To use PerformanceObserver, you need to use a `setTimeout` or `await observations()` to have a PerformanceObserver's
// callback get called. See the accompanying tests for examples.
Object.defineProperty(window, 'performance', {
writable: true,
value: nodePerformance,
});
Object.defineProperty(global, 'PerformanceObserver', {
value: NodePerformanceObserver,
});
// Only Chrome-based browsers support long task timings currently, so make Node pretend it does too
Object.defineProperty(PerformanceObserver, 'supportedEntryTypes', {
value: [...PerformanceObserver.supportedEntryTypes, 'longtask'],
});
export function waitForObservations() {
// Performance observations are processed after any timeout
return new Promise((resolve) => setTimeout(resolve));
}