Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit ce1f30e

Browse files
Refactor out record long tasks into a separate function (#17)
1 parent 8381e5c commit ce1f30e

9 files changed

Lines changed: 132 additions & 118 deletions

File tree

packages/opencensus-web-instrumentation-perf/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
17+
export * from './perf-types';
18+
export {getInitialLoadRootSpan} from './initial-load-root-span';
19+
export {recordLongTasks} from './long-tasks-recorder';
20+
export {GroupedPerfEntries, getPerfEntries, clearPerfEntries} from './perf-grouper';

packages/opencensus-web-instrumentation-perf/src/initial-load-root-span.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import {Annotation, ATTRIBUTE_HTTP_URL, ATTRIBUTE_HTTP_USER_AGENT, ATTRIBUTE_LONG_TASK_ATTRIBUTION, ATTRIBUTE_NAV_TYPE, parseUrl, RootSpan, Span, SpanKind, Tracer} from '@opencensus/web-core';
18-
import {GroupedPerfEntries} from './perf-recorder';
18+
import {GroupedPerfEntries} from './perf-grouper';
1919
import {PerformanceLongTaskTiming, PerformanceNavigationTimingExtended} from './perf-types';
2020
import {getResourceSpan} from './resource-span';
2121
import {annotationsForPerfTimeFields} from './util';
@@ -83,7 +83,7 @@ export function getInitialLoadRootSpan(
8383

8484
const resourceSpans = perfEntries.resourceTimings.map(
8585
(resourceTiming) => getResourceSpan(resourceTiming, traceId, root.id));
86-
const longTaskSpans = perfEntries.longTasks.map(
86+
const longTaskSpans = perfEntries.longTaskTimings.map(
8787
(longTaskTiming) => getLongTaskSpan(longTaskTiming, traceId, root.id));
8888

8989
root.spans = root.spans.concat(resourceSpans, longTaskSpans);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright 2019, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {PerformanceLongTaskTiming, PerformanceObserverEntryList, WindowWithLongTasks} from './perf-types';
18+
19+
/** Cast `window` to be access the `ocwLt` field for long task timings. */
20+
const windowWithLongTasks = window as WindowWithLongTasks;
21+
22+
/**
23+
* Begin recording `longtask` timings. These need to be captured using
24+
* `PerformanceObserver`.
25+
*/
26+
export function recordLongTasks() {
27+
if (!windowWithLongTasks.performance) return;
28+
if (windowWithLongTasks.PerformanceObserver) {
29+
const longTaskObserver =
30+
new windowWithLongTasks.PerformanceObserver(onLongTasks);
31+
longTaskObserver.observe({entryTypes: ['longtask']});
32+
}
33+
windowWithLongTasks.ocwLt = [];
34+
}
35+
36+
function onLongTasks(entryList: PerformanceObserverEntryList) {
37+
// These must be PerformanceLongTaskTiming objects because we only observe
38+
// 'longtask' above.
39+
windowWithLongTasks.ocwLt!.push(
40+
...(entryList.getEntries() as PerformanceLongTaskTiming[]));
41+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Copyright 2019, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {PerformanceLongTaskTiming, PerformanceNavigationTimingExtended, PerformancePaintTiming, PerformanceResourceTimingExtended, WindowWithLongTasks} from './perf-types';
18+
19+
/** Cast `window` to be access the `ocwLt` field for long task timings. */
20+
const windowWithLongTasks = window as WindowWithLongTasks;
21+
22+
/** Represent all collected performance timings grouped by type. */
23+
export interface GroupedPerfEntries {
24+
navigationTiming?: PerformanceNavigationTimingExtended;
25+
paintTimings: PerformancePaintTiming[];
26+
resourceTimings: PerformanceResourceTimingExtended[];
27+
longTaskTimings: PerformanceLongTaskTiming[];
28+
}
29+
30+
/** Returns the recorded performance entries but does not clear them. */
31+
export function getPerfEntries(): GroupedPerfEntries {
32+
if (!window.performance) {
33+
return {
34+
resourceTimings: [],
35+
longTaskTimings: [],
36+
paintTimings: [],
37+
};
38+
}
39+
40+
const perf = window.performance;
41+
42+
const longTaskTimings = windowWithLongTasks.ocwLt;
43+
44+
const entries: GroupedPerfEntries = {
45+
resourceTimings: perf.getEntriesByType('resource') as
46+
PerformanceResourceTimingExtended[],
47+
paintTimings: perf.getEntriesByType('paint') as PerformancePaintTiming[],
48+
longTaskTimings: longTaskTimings ? longTaskTimings.slice() : [],
49+
};
50+
51+
const navEntries = perf.getEntriesByType('navigation');
52+
if (navEntries.length) {
53+
entries.navigationTiming =
54+
navEntries[0] as PerformanceNavigationTimingExtended;
55+
}
56+
57+
return entries;
58+
}
59+
60+
/** Clears resource timings, marks, measures and stored long task timings. */
61+
export function clearPerfEntries() {
62+
if (!window.performance) return;
63+
windowWithLongTasks.ocwLt = [];
64+
performance.clearResourceTimings();
65+
performance.clearMarks();
66+
performance.clearMeasures();
67+
}

packages/opencensus-web-instrumentation-perf/src/perf-recorder.ts

Lines changed: 0 additions & 98 deletions
This file was deleted.

packages/opencensus-web-instrumentation-perf/src/perf-types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,8 @@ export declare interface PerformanceLongTaskTiming extends PerformanceEntry {
162162
export declare type WindowWithPerformanceObserver = Window & {
163163
readonly PerformanceObserver?: PerformanceObserver;
164164
};
165+
166+
/** Type for the `window` object with a field to track LongTask timings. */
167+
export declare type WindowWithLongTasks = WindowWithPerformanceObserver & {
168+
ocwLt?: PerformanceLongTaskTiming[];
169+
};

packages/opencensus-web-instrumentation-perf/test/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
// should import from all test files.
1919

2020
import './test-initial-load-root-span';
21-
import './test-perf-recorder';
21+
import './test-perf-grouper';
2222
import './test-resource-span';

packages/opencensus-web-instrumentation-perf/test/test-initial-load-root-span.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import {Annotation, Attributes, SpanKind, Tracer} from '@opencensus/web-core';
1818
import {getInitialLoadRootSpan} from '../src/initial-load-root-span';
19-
import {GroupedPerfEntries} from '../src/perf-recorder';
19+
import {GroupedPerfEntries} from '../src/perf-grouper';
2020

2121
const SPAN_ID_REGEX = /[0-9a-f]{16}/;
2222
const USER_AGENT = 'Mozilla/5.0 TEST';
@@ -49,7 +49,7 @@ const PERF_ENTRIES: GroupedPerfEntries = {
4949
toJSON: () => ({}),
5050
},
5151
],
52-
longTasks: [
52+
longTaskTimings: [
5353
{
5454
name: 'self',
5555
entryType: 'longtask',

packages/opencensus-web-instrumentation-perf/test/test-perf-recorder.ts renamed to packages/opencensus-web-instrumentation-perf/test/test-perf-grouper.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {clearPerfEntries, getPerfEntries, recordPerfEntries, TEST_ONLY} from '../src/perf-recorder';
17+
import {recordLongTasks} from '../src/long-tasks-recorder';
18+
import {clearPerfEntries, getPerfEntries} from '../src/perf-grouper';
1819
import {PerformanceLongTaskTiming, PerformanceNavigationTimingExtended, PerformanceObserver, PerformanceObserverConfig, PerformanceObserverEntryList, PerformancePaintTiming, PerformanceResourceTimingExtended} from '../src/perf-types';
1920

2021
const LONG_TASK_1: PerformanceLongTaskTiming = {
@@ -157,24 +158,17 @@ describe('performance recorder functions', () => {
157158
windowWithPerfObserver.PerformanceObserver = realPerformanceObserver;
158159
});
159160

160-
describe('recordPerfEntries', () => {
161-
it('increases the resource buffer size', () => {
162-
spyOn(performance, 'setResourceTimingBufferSize');
163-
recordPerfEntries();
164-
expect(performance.setResourceTimingBufferSize)
165-
.toHaveBeenCalledWith(TEST_ONLY.RESOURCE_TIMING_BUFFER_SIZE);
166-
});
167-
161+
describe('recordLongTasks', () => {
168162
it('starts tracking long tasks', () => {
169-
recordPerfEntries();
163+
recordLongTasks();
170164
expect(performanceObserver).toBeDefined();
171165
expect(performanceObserver!.config).toEqual({entryTypes: ['longtask']});
172166
});
173167
});
174168

175169
describe('getPerfEntries', () => {
176170
it('combines perf entries for nav, paint, resource and long tasks', () => {
177-
recordPerfEntries();
171+
recordLongTasks();
178172
performanceObserver!.sendMockPerfEntries(
179173
new MockPerfEntryList([LONG_TASK_1, LONG_TASK_2]));
180174
spyOn(performance, 'getEntriesByType')
@@ -186,7 +180,7 @@ describe('performance recorder functions', () => {
186180
navigationTiming: NAVIGATION_ENTRY,
187181
paintTimings: [PAINT_ENTRY],
188182
resourceTimings: [RESOURCE_ENTRY],
189-
longTasks: [LONG_TASK_1, LONG_TASK_2],
183+
longTaskTimings: [LONG_TASK_1, LONG_TASK_2],
190184
});
191185
});
192186
});
@@ -205,14 +199,14 @@ describe('performance recorder functions', () => {
205199
});
206200

207201
it('clears stored long tasks', () => {
208-
recordPerfEntries();
202+
recordLongTasks();
209203
performanceObserver!.sendMockPerfEntries(
210204
new MockPerfEntryList([LONG_TASK_1]));
211-
expect(getPerfEntries().longTasks.length).toBe(1);
205+
expect(getPerfEntries().longTaskTimings.length).toBe(1);
212206

213207
clearPerfEntries();
214208

215-
expect(getPerfEntries().longTasks.length).toBe(0);
209+
expect(getPerfEntries().longTaskTimings.length).toBe(0);
216210
});
217211
});
218212
});

0 commit comments

Comments
 (0)