File tree Expand file tree Collapse file tree 2 files changed +34
-4
lines changed
src/features/StartupProgress/Firedancer Expand file tree Collapse file tree 2 files changed +34
-4
lines changed Original file line number Diff line number Diff line change @@ -4,8 +4,9 @@ import type { ByteSizeResult } from "byte-size";
4
4
import byteSize from "byte-size" ;
5
5
import clsx from "clsx" ;
6
6
import { Bars } from "./Bars" ;
7
+ import { useValuePerSecond } from "./useValuePerSecond" ;
7
8
8
- const MAX_THROUGHPUT = 300000000 ;
9
+ const MAX_THROUGHPUT_BYTES_PER_S = 300_000_000 ;
9
10
10
11
interface SnapshotLoadingCardProps {
11
12
title : string ;
@@ -17,8 +18,7 @@ export function SnapshotLoadingCard({
17
18
completed,
18
19
total,
19
20
} : SnapshotLoadingCardProps ) {
20
- // TODO: calculate throughput
21
- const throughput = 0 ;
21
+ const throughput = useValuePerSecond ( completed ) ;
22
22
23
23
const throughputObj = throughput == null ? undefined : byteSize ( throughput ) ;
24
24
const completedObj = completed == null ? undefined : byteSize ( completed ) ;
@@ -58,7 +58,7 @@ export function SnapshotLoadingCard({
58
58
) }
59
59
</ Flex >
60
60
61
- < Bars value = { throughput ?? 0 } max = { MAX_THROUGHPUT } />
61
+ < Bars value = { throughput ?? 0 } max = { MAX_THROUGHPUT_BYTES_PER_S } />
62
62
</ Flex >
63
63
</ Card >
64
64
) ;
Original file line number Diff line number Diff line change
1
+ import { useEffect , useMemo , useState } from "react" ;
2
+
3
+ export function useValuePerSecond ( cumulativeValue ?: number | null ) {
4
+ const [ values , setValues ] = useState < [ number , number ] [ ] > ( [ ] ) ;
5
+
6
+ useEffect ( ( ) => {
7
+ if ( cumulativeValue == null ) return ;
8
+
9
+ const now = performance . now ( ) ;
10
+ setValues ( ( prev ) => {
11
+ const newValues = [ ...prev , [ cumulativeValue , now ] ] satisfies [
12
+ number ,
13
+ number ,
14
+ ] [ ] ;
15
+ while ( newValues [ 0 ] && newValues [ 0 ] [ 1 ] <= now - 500 ) {
16
+ newValues . shift ( ) ;
17
+ }
18
+ return newValues ;
19
+ } ) ;
20
+ } , [ cumulativeValue ] ) ;
21
+
22
+ return useMemo ( ( ) => {
23
+ if ( values . length <= 1 ) return ;
24
+
25
+ return (
26
+ ( 1_000 * ( values [ values . length - 1 ] [ 0 ] - values [ 0 ] [ 0 ] ) ) /
27
+ ( values [ values . length - 1 ] [ 1 ] - values [ 0 ] [ 1 ] )
28
+ ) ;
29
+ } , [ values ] ) ;
30
+ }
You can’t perform that action at this time.
0 commit comments