This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor out record long tasks into a separate function (#17)
- Loading branch information
1 parent
8381e5c
commit ce1f30e
Showing
9 changed files
with
132 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/opencensus-web-instrumentation-perf/src/long-tasks-recorder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Copyright 2019, OpenCensus Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import {PerformanceLongTaskTiming, PerformanceObserverEntryList, WindowWithLongTasks} from './perf-types'; | ||
|
||
/** Cast `window` to be access the `ocwLt` field for long task timings. */ | ||
const windowWithLongTasks = window as WindowWithLongTasks; | ||
|
||
/** | ||
* Begin recording `longtask` timings. These need to be captured using | ||
* `PerformanceObserver`. | ||
*/ | ||
export function recordLongTasks() { | ||
if (!windowWithLongTasks.performance) return; | ||
if (windowWithLongTasks.PerformanceObserver) { | ||
const longTaskObserver = | ||
new windowWithLongTasks.PerformanceObserver(onLongTasks); | ||
longTaskObserver.observe({entryTypes: ['longtask']}); | ||
} | ||
windowWithLongTasks.ocwLt = []; | ||
} | ||
|
||
function onLongTasks(entryList: PerformanceObserverEntryList) { | ||
// These must be PerformanceLongTaskTiming objects because we only observe | ||
// 'longtask' above. | ||
windowWithLongTasks.ocwLt!.push( | ||
...(entryList.getEntries() as PerformanceLongTaskTiming[])); | ||
} |
67 changes: 67 additions & 0 deletions
67
packages/opencensus-web-instrumentation-perf/src/perf-grouper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* Copyright 2019, OpenCensus Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import {PerformanceLongTaskTiming, PerformanceNavigationTimingExtended, PerformancePaintTiming, PerformanceResourceTimingExtended, WindowWithLongTasks} from './perf-types'; | ||
|
||
/** Cast `window` to be access the `ocwLt` field for long task timings. */ | ||
const windowWithLongTasks = window as WindowWithLongTasks; | ||
|
||
/** Represent all collected performance timings grouped by type. */ | ||
export interface GroupedPerfEntries { | ||
navigationTiming?: PerformanceNavigationTimingExtended; | ||
paintTimings: PerformancePaintTiming[]; | ||
resourceTimings: PerformanceResourceTimingExtended[]; | ||
longTaskTimings: PerformanceLongTaskTiming[]; | ||
} | ||
|
||
/** Returns the recorded performance entries but does not clear them. */ | ||
export function getPerfEntries(): GroupedPerfEntries { | ||
if (!window.performance) { | ||
return { | ||
resourceTimings: [], | ||
longTaskTimings: [], | ||
paintTimings: [], | ||
}; | ||
} | ||
|
||
const perf = window.performance; | ||
|
||
const longTaskTimings = windowWithLongTasks.ocwLt; | ||
|
||
const entries: GroupedPerfEntries = { | ||
resourceTimings: perf.getEntriesByType('resource') as | ||
PerformanceResourceTimingExtended[], | ||
paintTimings: perf.getEntriesByType('paint') as PerformancePaintTiming[], | ||
longTaskTimings: longTaskTimings ? longTaskTimings.slice() : [], | ||
}; | ||
|
||
const navEntries = perf.getEntriesByType('navigation'); | ||
if (navEntries.length) { | ||
entries.navigationTiming = | ||
navEntries[0] as PerformanceNavigationTimingExtended; | ||
} | ||
|
||
return entries; | ||
} | ||
|
||
/** Clears resource timings, marks, measures and stored long task timings. */ | ||
export function clearPerfEntries() { | ||
if (!window.performance) return; | ||
windowWithLongTasks.ocwLt = []; | ||
performance.clearResourceTimings(); | ||
performance.clearMarks(); | ||
performance.clearMeasures(); | ||
} |
98 changes: 0 additions & 98 deletions
98
packages/opencensus-web-instrumentation-perf/src/perf-recorder.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters