-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added graph benchmark with a naive fps gauge
- Avoiding sub-pixel rendering - Toggled `onlyRenderVisibleElements` to improve performance
- Loading branch information
Showing
8 changed files
with
10,814 additions
and
142 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
56 changes: 56 additions & 0 deletions
56
...s/security/packages/kbn-cloud-security-posture/graph/src/components/graph/fps_monitor.tsx
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,56 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React, { useEffect, useRef, useState } from 'react'; | ||
import { css } from '@emotion/react'; | ||
import { FpsTrendline } from './fps_trendline'; | ||
|
||
export const FpsMonitor: React.FC = () => { | ||
const [fps, setFps] = useState(0); | ||
const frameCount = useRef(0); | ||
const lastTimestamp = useRef(performance.now()); | ||
|
||
useEffect(() => { | ||
let animationFrameId: number; | ||
|
||
const calculateFPS = (timestamp: DOMHighResTimeStamp) => { | ||
frameCount.current += 1; | ||
const delta = timestamp - lastTimestamp.current; | ||
|
||
// Update FPS every second | ||
if (delta >= 1000) { | ||
setFps((frameCount.current * 1000) / delta); | ||
frameCount.current = 0; | ||
lastTimestamp.current = timestamp; | ||
} | ||
|
||
animationFrameId = requestAnimationFrame(calculateFPS); | ||
}; | ||
|
||
animationFrameId = requestAnimationFrame(calculateFPS); | ||
|
||
return () => cancelAnimationFrame(animationFrameId); | ||
}, []); | ||
|
||
return ( | ||
<div | ||
style={{ | ||
padding: '10px', | ||
position: 'fixed', | ||
}} | ||
> | ||
<strong>{'FPS:'}</strong> {Math.round(fps)} <br /> | ||
<strong>{'Nodes:'}</strong> {document.getElementsByClassName('react-flow__node').length}{' '} | ||
<strong>{'Edges:'}</strong> {document.getElementsByClassName('react-flow__edge').length}{' '} | ||
<FpsTrendline | ||
css={css` | ||
width: 300px; | ||
`} | ||
/> | ||
</div> | ||
); | ||
}; |
82 changes: 82 additions & 0 deletions
82
...security/packages/kbn-cloud-security-posture/graph/src/components/graph/fps_trendline.tsx
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,82 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React, { useEffect, useRef, useState } from 'react'; | ||
import { CommonProps } from '@elastic/eui'; | ||
|
||
export const FpsTrendline: React.FC<CommonProps> = (props: CommonProps) => { | ||
const [fpsSamples, setFpsSamples] = useState<number[]>([]); | ||
const frameCount = useRef(0); | ||
const lastTimestamp = useRef(performance.now()); | ||
|
||
useEffect(() => { | ||
let animationFrameId: number; | ||
|
||
const calculateFPS = (timestamp: number) => { | ||
frameCount.current += 1; | ||
const delta = timestamp - lastTimestamp.current; | ||
|
||
if (delta >= 1000) { | ||
const fps = (frameCount.current * 1000) / delta; | ||
setFpsSamples((prevSamples) => { | ||
const updatedSamples = [...prevSamples, fps]; | ||
return updatedSamples.slice(-20); | ||
}); | ||
frameCount.current = 0; | ||
lastTimestamp.current = timestamp; | ||
} | ||
|
||
animationFrameId = requestAnimationFrame(calculateFPS); | ||
}; | ||
|
||
animationFrameId = requestAnimationFrame(calculateFPS); | ||
|
||
return () => cancelAnimationFrame(animationFrameId); | ||
}, []); | ||
|
||
const getBarColor = (fps: number): string => { | ||
if (fps >= 50) return '#4caf50'; // Green | ||
if (fps >= 30) return '#ffeb3b'; // Yellow | ||
return '#f44336'; // Red | ||
}; | ||
|
||
return ( | ||
<div {...props}> | ||
<div | ||
style={{ | ||
display: 'flex', | ||
alignItems: 'flex-end', | ||
height: '30px', | ||
padding: '5px', | ||
}} | ||
> | ||
{fpsSamples.map((fps, index) => ( | ||
<div | ||
key={index} | ||
style={{ | ||
height: `${Math.min(fps, 60) * (100 / 60)}%`, | ||
width: '5%', | ||
backgroundColor: getBarColor(fps), | ||
marginRight: '2px', | ||
}} | ||
title={`${fps.toFixed(2)} FPS`} | ||
> | ||
<div | ||
style={{ | ||
fontSize: '8px', | ||
padding: '2px', | ||
left: `${index * 5 + 5}%`, | ||
}} | ||
> | ||
{fps.toFixed(0)} | ||
</div> | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
); | ||
}; |
Oops, something went wrong.