-
Notifications
You must be signed in to change notification settings - Fork 8.1k
/
performance_context.tsx
68 lines (61 loc) · 2.27 KB
/
performance_context.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import React, { useMemo, useState } from 'react';
import { afterFrame } from '@elastic/apm-rum-core';
import { useLocation } from 'react-router-dom';
import { perfomanceMarkers } from '../performance_markers';
import { PerformanceApi, PerformanceContext } from './use_performance_context';
function measureInteraction() {
performance.mark(perfomanceMarkers.startPageChange);
const trackedRoutes: string[] = [];
return {
/**
* Marks the end of the page ready state and measures the performance between the start of the page change and the end of the page ready state.
* @param pathname - The pathname of the page.
*/
pageReady(pathname: string) {
performance.mark(perfomanceMarkers.endPageReady);
if (!trackedRoutes.includes(pathname)) {
performance.measure(pathname, {
detail: { eventName: 'kibana:plugin_render_time', type: 'kibana:performance' },
start: perfomanceMarkers.startPageChange,
end: perfomanceMarkers.endPageReady,
});
trackedRoutes.push(pathname);
}
},
};
}
export function PerformanceContextProvider({ children }: { children: React.ReactElement }) {
const [isRendered, setIsRendered] = useState(false);
const location = useLocation();
const interaction = measureInteraction();
React.useEffect(() => {
afterFrame(() => {
setIsRendered(true);
});
return () => {
setIsRendered(false);
performance.clearMeasures(location.pathname);
};
}, [location.pathname]);
const api = useMemo<PerformanceApi>(
() => ({
onPageReady() {
if (isRendered) {
interaction.pageReady(location.pathname);
}
},
}),
[isRendered, location.pathname, interaction]
);
return <PerformanceContext.Provider value={api}>{children}</PerformanceContext.Provider>;
}
// dynamic import
// eslint-disable-next-line import/no-default-export
export default PerformanceContextProvider;