-
Notifications
You must be signed in to change notification settings - Fork 257
EDSC-4613: Build metrics for how long it takes for the map to load #2083
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eudoroolivares2016
wants to merge
12
commits into
main
Choose a base branch
from
EDSC-4613
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3b37a8f
EDSC-4613: Starting metrics
eudoroolivares2016 e481fee
EDSC-4613: Adding metric for granule outline and the granule background
eudoroolivares2016 e1806cc
EDSC-4613: Fix extra outgoing requests sent out on granule requests w…
eudoroolivares2016 4b6e1b2
EDSC-4613: Core metric fix leaky listener
eudoroolivares2016 b9cd107
EDSC-4613: Cleanup
eudoroolivares2016 bfb9ac8
EDSC-4613: Clean up
eudoroolivares2016 fe518dd
EDSC-4613: Clean up; rebase
eudoroolivares2016 2f75264
EDSC-4613: Fix tooltip for inactive colls
eudoroolivares2016 14a9725
EDSC-4613: Fix test
eudoroolivares2016 74afc95
EDSC-4613: Keep map metrics and solution in sep tickets
eudoroolivares2016 6a18428
EDSC-4613: Fix lint
eudoroolivares2016 c9a8967
EDSC-4613: Push polish
eudoroolivares2016 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| import React, { | ||
| useEffect, | ||
| useRef, | ||
| useState | ||
| useState, | ||
| RefObject | ||
| } from 'react' | ||
| import { renderToString } from 'react-dom/server' | ||
|
|
||
|
|
@@ -31,6 +32,8 @@ import TileLayer from 'ol/layer/Tile' | |
| import VectorLayer from 'ol/layer/Vector' | ||
| import VectorSource from 'ol/source/Vector' | ||
| import VectorTileLayer from 'ol/layer/VectorTile' | ||
| import { unByKey } from 'ol/Observable' | ||
| import { EventsKey } from 'ol/events' | ||
|
|
||
| import { | ||
| FaCircle, | ||
|
|
@@ -45,6 +48,13 @@ import { | |
| Map as MapIcon | ||
| // @ts-expect-error The file does not have types | ||
| } from '@edsc/earthdata-react-icons/horizon-design-system/hds/ui' | ||
| import { | ||
| metricsMapFramePerformance, | ||
| metricsMapRenderPerformance, | ||
| MapPerformanceEvent | ||
| } from '../../util/metrics/metricsMap' | ||
|
|
||
| import { computePercentile } from '../../util/metrics/helpers' | ||
|
|
||
| import EDSCIcon from '../EDSCIcon/EDSCIcon' | ||
|
|
||
|
|
@@ -99,9 +109,28 @@ import { | |
| } from '../../types/sharedTypes' | ||
| import { MapView, ShapefileSlice } from '../../zustand/types' | ||
|
|
||
| interface MapPerformanceWindow { | ||
| windowStart: number | ||
| frames: number | ||
| slowFrames: number | ||
| verySlowFrames: number | ||
| renderTimes: number[] | ||
| maxRenderTimeMs: number | ||
| } | ||
|
|
||
| const createEmptyPerformanceWindow = (): MapPerformanceWindow => ({ | ||
| windowStart: performance.now(), | ||
| frames: 0, | ||
| slowFrames: 0, | ||
| verySlowFrames: 0, | ||
| renderTimes: [], | ||
| maxRenderTimeMs: 0 | ||
| }) | ||
|
|
||
| let previousGranulesKey: string | ||
| let previousProjectionCode: ProjectionCode | ||
| let layersAdded = false | ||
| const PERFORMANCE_WINDOW_MS = 3000 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be a bamboo var |
||
|
|
||
| // Render the times icon to an SVG string for use in the focused granule overlay | ||
| // Disable a testing-library rule because this isn't a test | ||
|
|
@@ -177,6 +206,32 @@ const overlayLayers: Record<string, TileLayer | VectorTileLayer | null> = { | |
| [mapLayers.placeLabels]: null | ||
| } | ||
|
|
||
| // Times a single prerender → postrender pass for a layer using .once(), | ||
| // so it only fires for the very next render triggered after this is called. | ||
| export const timeLayerRenderOnce = ( | ||
| layer: VectorLayer, | ||
| label: string, | ||
| granuleCount: number, | ||
| collectionId: string | ||
| ): EventsKey[] => { | ||
| let start = 0 | ||
|
|
||
| const preKey = layer.once(RenderEventType.PRERENDER as LayerRenderEventTypes, () => { | ||
| start = performance.now() | ||
| performance.mark(`${label}-prerender`) | ||
| }) | ||
|
|
||
| const postKey = layer.once(RenderEventType.POSTRENDER as LayerRenderEventTypes, () => { | ||
| const duration = performance.now() - start | ||
| performance.mark(`${label}-postrender`) | ||
| performance.measure(label, `${label}-prerender`, `${label}-postrender`) | ||
|
|
||
| metricsMapRenderPerformance(granuleCount, duration, label, collectionId) | ||
| }) | ||
|
|
||
| return [preKey, postKey] | ||
| } | ||
|
|
||
| // Create a view for the map. This will change when the padding needs to be updated | ||
| const createView = ({ | ||
| center, | ||
|
|
@@ -242,6 +297,7 @@ const clearFocusedGranuleSource = (map: OlMap) => { | |
| } | ||
|
|
||
| // Remove the drawing interaction from the map | ||
| // TODO double check all drawings go through this otherwise it might be a listener leak | ||
| const removeDrawingInteraction = (map: OlMap) => { | ||
| map.getInteractions().getArray().forEach((interaction) => { | ||
| if (interaction.get('id') === 'spatial-drawing-interaction') { | ||
|
|
@@ -257,6 +313,48 @@ const hasFiniteExtent = (extent: import('ol/extent').Extent | null | undefined): | |
| return extent.every((coordinate) => Number.isFinite(coordinate)) | ||
| } | ||
|
|
||
| const flushMapPerformanceMetrics = ( | ||
| performanceWindowRef: RefObject<MapPerformanceWindow>, | ||
| collectionId: string | ||
| ) => { | ||
| if (!collectionId) { | ||
| return | ||
| } | ||
|
|
||
| const metrics = performanceWindowRef.current | ||
| if (metrics.frames === 0) { | ||
| metrics.windowStart = performance.now() | ||
|
|
||
| return | ||
| } | ||
|
|
||
| const sortedRenderTimes = [...metrics.renderTimes].sort( | ||
| (a, b) => a - b | ||
| ) | ||
|
|
||
| const event: MapPerformanceEvent = { | ||
| windowDurationMs: | ||
| performance.now() - metrics.windowStart, | ||
|
|
||
| render: { | ||
| frames: metrics.frames, | ||
| p50RenderTimeMs: computePercentile(sortedRenderTimes, 0.5), | ||
| p95RenderTimeMs: computePercentile(sortedRenderTimes, 0.95), | ||
| p99RenderTimeMs: computePercentile(sortedRenderTimes, 0.99), | ||
| maxRenderTimeMs: metrics.maxRenderTimeMs, | ||
| slowFrames: metrics.slowFrames, | ||
| verySlowFrames: metrics.verySlowFrames | ||
| }, | ||
| collectionId | ||
| } | ||
| metricsMapFramePerformance(event) | ||
| // Clear the metrics for the next window | ||
| // Reinitialize performance window ref to default state | ||
| // TODO really make sure it makes sense to disable this rule | ||
| // eslint-disable-next-line no-param-reassign | ||
| performanceWindowRef.current = createEmptyPerformanceWindow() | ||
| } | ||
|
|
||
| interface MapProps { | ||
| /** The base layers of the map */ | ||
| base: { | ||
|
|
@@ -415,6 +513,15 @@ const Map: React.FC<MapProps> = ({ | |
| // Create a ref for the map and the map dome element | ||
| const mapRef = useRef<OlMap>(undefined) | ||
| const mapElRef = useRef<HTMLDivElement>(null) | ||
| const lastFrameRef = useRef<number>(performance.now()) | ||
| const frameTimesRef = useRef<number[]>([]) | ||
| const isTrackingFrameRef = useRef(false) | ||
| const performanceWindowRef = useRef<MapPerformanceWindow>(createEmptyPerformanceWindow()) | ||
| // Mirror the latest collection into refs so the long-lived | ||
| const focusedCollectionIdRef = useRef(focusedCollectionId) | ||
| useEffect(() => { | ||
| focusedCollectionIdRef.current = focusedCollectionId | ||
| }, [focusedCollectionId]) | ||
|
|
||
| const [isLayerSwitcherOpen, setIsLayerSwitcherOpen] = useState(false) | ||
|
|
||
|
|
@@ -450,6 +557,64 @@ const Map: React.FC<MapProps> = ({ | |
| }) | ||
| mapRef.current = map | ||
|
|
||
| // Only accumulate frame deltas while an interaction (pan or zoom) is in | ||
| // progress, so idle frames don't dilute the stats. On moveend we roll the | ||
| // collected frame times up into a single summary and hand it off to the | ||
| // metrics helper, rather than reporting one event per frame. | ||
| const handlePostRenderPerf = () => { | ||
| if (!isTrackingFrameRef.current) return | ||
|
|
||
| const now = performance.now() | ||
| frameTimesRef.current.push(now - lastFrameRef.current) | ||
| lastFrameRef.current = now | ||
| } | ||
|
|
||
| const handleMoveStartPerf = () => { | ||
| isTrackingFrameRef.current = true | ||
| frameTimesRef.current = [] | ||
| lastFrameRef.current = performance.now() | ||
| } | ||
|
|
||
| const handleMoveEndPerf = () => { | ||
| if (!isTrackingFrameRef.current) return | ||
|
|
||
| isTrackingFrameRef.current = false | ||
|
|
||
| const frameTimes = frameTimesRef.current | ||
| if (frameTimes.length === 0) return | ||
|
|
||
| const metrics = performanceWindowRef.current | ||
|
|
||
| metrics.frames += frameTimes.length | ||
|
|
||
| metrics.renderTimes.push(...frameTimes) | ||
|
|
||
| metrics.slowFrames += frameTimes.filter( | ||
| (time) => time > 33 | ||
| ).length | ||
|
|
||
| metrics.verySlowFrames += frameTimes.filter( | ||
| (time) => time > 100 | ||
| ).length | ||
|
|
||
| metrics.maxRenderTimeMs = Math.max( | ||
| metrics.maxRenderTimeMs, | ||
| ...frameTimes | ||
| ) | ||
|
|
||
| // Real time elapsed since the window opened, until the moveend that happened to trigger a flush check that passed." | ||
| if ( | ||
| performance.now() - metrics.windowStart | ||
| >= PERFORMANCE_WINDOW_MS | ||
| ) { | ||
| flushMapPerformanceMetrics(performanceWindowRef, focusedCollectionIdRef.current) | ||
| } | ||
| } | ||
|
|
||
| map.on('postrender', handlePostRenderPerf) | ||
| map.on('movestart', handleMoveStartPerf) | ||
| map.on('moveend', handleMoveEndPerf) | ||
|
|
||
| // Handle the map draw start event | ||
| const handleDrawingStart = (spatialType: string) => { | ||
| // Remove any existing drawing interaction | ||
|
|
@@ -680,6 +845,11 @@ const Map: React.FC<MapProps> = ({ | |
| map.un('moveend', handleMoveEnd) | ||
| map.un('pointermove', handlePointerMove) | ||
|
|
||
| // Cleanup the performance tracking event listeners | ||
| map.un('postrender', handlePostRenderPerf) | ||
| map.un('movestart', handleMoveStartPerf) | ||
| map.un('moveend', handleMoveEndPerf) | ||
|
|
||
| eventEmitter.off(mapEventTypes.DRAWSTART, handleDrawingStart) | ||
| eventEmitter.off(mapEventTypes.DRAWCANCEL, handleDrawingCancel) | ||
| eventEmitter.off(mapEventTypes.MOVEMAP, handleMoveMap) | ||
|
|
@@ -984,51 +1154,78 @@ const Map: React.FC<MapProps> = ({ | |
|
|
||
| // When the granules change, draw the granule backgrounds | ||
| useEffect(() => { | ||
| if (granules && granules.length > 0) { | ||
| // If the granules haven't changed and the projection hasn't changed, don't redraw the granule backgrounds | ||
| // Redraw the granule backgrounds if the product layer from the gibs tag has changed | ||
| if (granulesKey === previousGranulesKey && projectionCode === previousProjectionCode) return | ||
| if (granulesKey === previousGranulesKey | ||
| && projectionCode === previousProjectionCode) return undefined | ||
| // Update the previous values | ||
| previousGranulesKey = granulesKey | ||
| previousProjectionCode = projectionCode | ||
|
|
||
| // Clear the existing granule backgrounds | ||
| granuleBackgroundsSource.clear() | ||
| // Clear any existing granule highlights | ||
| unhighlightGranule(granuleHighlightsSource) | ||
|
|
||
| // Clear any existing focused granules | ||
| clearFocusedGranuleSource(mapRef.current as OlMap) | ||
|
|
||
| // Clear the granule imagery layers | ||
| granuleImageryLayerGroup.getLayers().clear() | ||
|
|
||
| // Draw the granule backgrounds | ||
| drawGranuleBackgroundsAndImagery({ | ||
| gibsLayersByCollection, | ||
| granuleImageryLayerGroup, | ||
| granulesMetadata: granules, | ||
| map: mapRef.current as OlMap, | ||
| projectionCode, | ||
| vectorSource: granuleBackgroundsSource | ||
| }) | ||
|
|
||
| // If there is a focused granule draw it | ||
| if (focusedGranuleId) { | ||
| drawFocusedGranule({ | ||
| collectionId: focusedCollectionId, | ||
| focusedGranuleSource, | ||
| granuleBackgroundsSource, | ||
| granuleId: focusedGranuleId, | ||
| isProjectPage, | ||
| map: (mapRef.current as OlMap), | ||
| onExcludeGranule, | ||
| setGranuleId, | ||
| shouldMoveMap: false, | ||
| timesIconSvg | ||
| }) | ||
| } | ||
|
|
||
| // Time this specific update's render pass for all layers | ||
| const timingKeys = [ | ||
| ...timeLayerRenderOnce(granuleBackgroundsLayer, 'granule-backgrounds', granules.length, focusedCollectionId), | ||
| ...timeLayerRenderOnce(granuleOutlinesLayer, 'granule-outlines', granules.length, focusedCollectionId) | ||
| ] | ||
|
|
||
| // Update the previous values | ||
| previousGranulesKey = granulesKey | ||
| previousProjectionCode = projectionCode | ||
| // Clean up the timing listeners if this effect reruns/unmounts before they fire | ||
| return () => { | ||
| timingKeys.forEach((key) => { | ||
| unByKey(key) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // Clear the existing granule backgrounds | ||
| granuleBackgroundsSource.clear() | ||
|
|
||
| // Clear any existing granule highlights | ||
| unhighlightGranule(granuleHighlightsSource) | ||
|
|
||
| // Clear any existing focused granules | ||
| clearFocusedGranuleSource(mapRef.current as OlMap) | ||
|
|
||
| // Clear the granule imagery layers | ||
| granuleImageryLayerGroup.getLayers().clear() | ||
|
|
||
| // Draw the granule backgrounds | ||
| drawGranuleBackgroundsAndImagery({ | ||
| gibsLayersByCollection, | ||
| granuleImageryLayerGroup, | ||
| granulesMetadata: granules, | ||
| map: mapRef.current as OlMap, | ||
| projectionCode, | ||
| vectorSource: granuleBackgroundsSource | ||
| }) | ||
| // Clear any existing focused granules | ||
| clearFocusedGranuleSource(mapRef.current as OlMap) | ||
|
|
||
| // If there is a focused granule draw it | ||
| if (focusedGranuleId) { | ||
| drawFocusedGranule({ | ||
| collectionId: focusedCollectionId, | ||
| focusedGranuleSource, | ||
| granuleBackgroundsSource, | ||
| granuleId: focusedGranuleId, | ||
| isProjectPage, | ||
| map: (mapRef.current as OlMap), | ||
| onExcludeGranule, | ||
| setGranuleId, | ||
| shouldMoveMap: false, | ||
| timesIconSvg | ||
| }) | ||
| } | ||
| return undefined | ||
| }, [granules, granulesKey, projectionCode]) | ||
|
|
||
| // When the spatial search changes, draw the spatial search | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.