-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat(profiling): add ability to search for frames #31723
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cebadfa
feat(flamegraph): add view select menu
JonasBa e1a4630
feat(profiling): use buttonbar
JonasBa ac15883
feat(profiling): add search
JonasBa 1870aa3
style(lint): Auto commit lint changes
getsantry[bot] 7ddc2f3
style(lint): Auto commit lint changes
getsantry[bot] 9c92b29
feat(profiling): decouple search fn
JonasBa aaf8e38
fix(flamegraphsearch): missing hook deps
JonasBa 5d5a436
Merge branch 'jb/profiling/tooltip' into jb/profiling/flamegraph-search
JonasBa 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
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 |
|---|---|---|
| @@ -0,0 +1,258 @@ | ||
| import * as React from 'react'; | ||
| import styled from '@emotion/styled'; | ||
| import * as Sentry from '@sentry/react'; | ||
| import Fuse from 'fuse.js'; | ||
|
|
||
| import space from 'sentry/styles/space'; | ||
| import {CanvasPoolManager} from 'sentry/utils/profiling/canvasScheduler'; | ||
| import {Flamegraph} from 'sentry/utils/profiling/flamegraph'; | ||
| import {FlamegraphFrame} from 'sentry/utils/profiling/flamegraphFrame'; | ||
| import {isRegExpString, parseRegExp} from 'sentry/utils/profiling/validators/regExp'; | ||
|
|
||
| function uniqueFrameKey(frame: FlamegraphFrame): string { | ||
| return `${frame.frame.key + String(frame.start)}`; | ||
| } | ||
|
|
||
| function frameSearch( | ||
| query: string, | ||
| frames: ReadonlyArray<FlamegraphFrame>, | ||
| index: Fuse< | ||
| FlamegraphFrame, | ||
| {includeMatches: true; keys: 'frame.name'[]; threshold: number} | ||
| > | ||
| ): Record<string, FlamegraphFrame> { | ||
| const results = {}; | ||
| if (isRegExpString(query)) { | ||
| const [_, lookup, flags] = parseRegExp(query) ?? []; | ||
|
|
||
| try { | ||
| if (!lookup) { | ||
| throw new Error('Invalid RegExp'); | ||
| } | ||
|
|
||
| for (let i = 0; i < frames.length; i++) { | ||
| const frame = frames[i]; | ||
|
|
||
| if (new RegExp(lookup, flags ?? 'g').test(frame.frame.name.trim())) { | ||
| results[ | ||
| `${ | ||
| frame.frame.name + | ||
| (frame.frame.file ? frame.frame.file : '') + | ||
| String(frame.start) | ||
| }` | ||
| ] = frame; | ||
| } | ||
| } | ||
| } catch (e) { | ||
| Sentry.captureMessage(e.message); | ||
| } | ||
|
|
||
| return results; | ||
| } | ||
|
|
||
| const fuseResults = index | ||
| .search(query) | ||
| .sort((a, b) => numericSort(a.item.start, b.item.start, 'asc')); | ||
|
|
||
| for (let i = 0; i < fuseResults.length; i++) { | ||
| const frame = fuseResults[i]; | ||
|
|
||
| results[ | ||
| `${ | ||
| frame.item.frame.name + | ||
| (frame.item.frame.file ? frame.item.frame.file : '') + | ||
| String(frame.item.start) | ||
| }` | ||
| ] = frame.item; | ||
| } | ||
|
|
||
| return results; | ||
| } | ||
|
|
||
| const numericSort = ( | ||
| a: null | undefined | number, | ||
| b: null | undefined | number, | ||
| direction: 'asc' | 'desc' | ||
| ): number => { | ||
| if (a === b) { | ||
| return 0; | ||
| } | ||
| if (a === null || a === undefined) { | ||
| return 1; | ||
| } | ||
| if (b === null || b === undefined) { | ||
| return -1; | ||
| } | ||
|
|
||
| return direction === 'asc' ? a - b : b - a; | ||
| }; | ||
|
|
||
| interface FlamegraphSearchProps { | ||
| canvasPoolManager: CanvasPoolManager; | ||
| flamegraphs: Flamegraph[]; | ||
| placement: 'top' | 'bottom'; | ||
| } | ||
|
|
||
| function FlamegraphSearch({ | ||
| flamegraphs, | ||
| canvasPoolManager, | ||
| }: FlamegraphSearchProps): React.ReactElement | null { | ||
| const ref = React.useRef<HTMLInputElement>(null); | ||
|
|
||
| const [open, setOpen] = React.useState<boolean>(false); | ||
| const [selectedNode, setSelectedNode] = React.useState<FlamegraphFrame | null>(); | ||
| const [searchResults, setSearchResults] = React.useState< | ||
| Record<string, FlamegraphFrame> | ||
| >({}); | ||
|
|
||
| const allFrames = React.useMemo(() => { | ||
| return flamegraphs.reduce( | ||
| (acc: FlamegraphFrame[], graph) => acc.concat(graph.frames), | ||
| [] | ||
| ); | ||
| }, [flamegraphs]); | ||
|
|
||
| const searchIndex = React.useMemo(() => { | ||
| return new Fuse(allFrames, { | ||
| keys: ['frame.name'], | ||
| threshold: 0.3, | ||
| includeMatches: true, | ||
| }); | ||
| }, [allFrames]); | ||
|
|
||
| const onZoomIntoFrame = React.useCallback( | ||
| (frame: FlamegraphFrame) => { | ||
| canvasPoolManager.dispatch('zoomIntoFrame', [frame]); | ||
| setSelectedNode(frame); | ||
| }, | ||
| [canvasPoolManager] | ||
| ); | ||
|
|
||
| const handleSearchInput = React.useCallback( | ||
| (evt: React.ChangeEvent<HTMLInputElement>) => { | ||
| const query = evt.currentTarget.value; | ||
|
|
||
| if (!query) { | ||
| setSearchResults({}); | ||
| canvasPoolManager.dispatch('searchResults', [{}]); | ||
| return; | ||
| } | ||
|
|
||
| const results = frameSearch(query, allFrames, searchIndex); | ||
|
|
||
| setSearchResults(results); | ||
| canvasPoolManager.dispatch('searchResults', [results]); | ||
| }, | ||
| [searchIndex, frames, canvasPoolManager, allFrames] | ||
| ); | ||
|
|
||
| const onNextSearchClick = React.useCallback(() => { | ||
| const frames = Object.values(searchResults).sort((a, b) => | ||
| a.start === b.start | ||
| ? numericSort(a.depth, b.depth, 'asc') | ||
| : numericSort(a.start, b.start, 'asc') | ||
| ); | ||
| if (!frames.length) { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (!selectedNode) { | ||
| return onZoomIntoFrame(frames[0] ?? null); | ||
| } | ||
|
|
||
| const index = frames.findIndex( | ||
| f => uniqueFrameKey(f) === uniqueFrameKey(selectedNode) | ||
| ); | ||
|
|
||
| if (index + 1 > frames.length - 1) { | ||
| return onZoomIntoFrame(frames[0]); | ||
| } | ||
| return onZoomIntoFrame(frames[index + 1]); | ||
| }, [selectedNode, searchResults, onZoomIntoFrame]); | ||
|
|
||
| const onPreviousSearchClick = React.useCallback(() => { | ||
| const frames = Object.values(searchResults).sort((a, b) => | ||
| a.start === b.start | ||
| ? numericSort(a.depth, b.depth, 'asc') | ||
| : numericSort(a.start, b.start, 'asc') | ||
| ); | ||
| if (!frames.length) { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (!selectedNode) { | ||
| return onZoomIntoFrame(frames[0] ?? null); | ||
| } | ||
| const index = frames.findIndex( | ||
| f => uniqueFrameKey(f) === uniqueFrameKey(selectedNode) | ||
| ); | ||
|
|
||
| if (index - 1 < 0) { | ||
| return onZoomIntoFrame(frames[frames.length - 1]); | ||
| } | ||
| return onZoomIntoFrame(frames[index - 1]); | ||
| }, [selectedNode, searchResults, onZoomIntoFrame]); | ||
|
|
||
| const onCmdF = React.useCallback( | ||
| (evt: KeyboardEvent) => { | ||
| if (evt.key === 'f' && evt.metaKey) { | ||
| evt.preventDefault(); | ||
| if (open) { | ||
| ref.current?.focus(); | ||
| } else { | ||
| setOpen(true); | ||
| } | ||
| } | ||
| if (evt.key === 'Escape') { | ||
| setSearchResults({}); | ||
| setOpen(false); | ||
| } | ||
| }, | ||
| [open, setSearchResults] | ||
|
Member
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. Add |
||
| ); | ||
|
|
||
| const onKeyDown = React.useCallback( | ||
| (evt: React.KeyboardEvent<HTMLInputElement>) => { | ||
| if (evt.key === 'Escape') { | ||
| setSearchResults({}); | ||
| setOpen(false); | ||
| } | ||
| if (evt.key === 'ArrowDown') { | ||
| evt.preventDefault(); | ||
| onNextSearchClick(); | ||
| } | ||
| if (evt.key === 'ArrowUp') { | ||
| evt.preventDefault(); | ||
| onPreviousSearchClick(); | ||
| } | ||
| }, | ||
| [onNextSearchClick, onPreviousSearchClick, setSearchResults] | ||
|
Member
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. Add |
||
| ); | ||
|
|
||
| React.useEffect(() => { | ||
| document.addEventListener('keydown', onCmdF); | ||
|
|
||
| return () => { | ||
| document.removeEventListener('keydown', onCmdF); | ||
| }; | ||
| }, [onCmdF]); | ||
|
|
||
| return open ? ( | ||
| <Input | ||
| ref={ref} | ||
| autoFocus | ||
| type="text" | ||
| onChange={handleSearchInput} | ||
| onKeyDown={onKeyDown} | ||
| /> | ||
| ) : null; | ||
| } | ||
|
|
||
| const Input = styled('input')` | ||
| position: absolute; | ||
| left: 50%; | ||
| top: ${space(4)}; | ||
| transform: translateX(-50%); | ||
| `; | ||
|
|
||
| export {FlamegraphSearch}; | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should
setSelectedNodebe in the dependencies here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not necessary, react guarantees that the setState fn is stable between rerenders (https://reactjs.org/docs/hooks-reference.html)