Skip to content

Commit 812bda0

Browse files
feat: calculate throughput
1 parent c57bc41 commit 812bda0

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/features/StartupProgress/Firedancer/SnapshotLoadingCard.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import type { ByteSizeResult } from "byte-size";
44
import byteSize from "byte-size";
55
import clsx from "clsx";
66
import { Bars } from "./Bars";
7+
import { useValuePerSecond } from "./useValuePerSecond";
78

8-
const MAX_THROUGHPUT = 300000000;
9+
const MAX_THROUGHPUT_BYTES_PER_S = 300_000_000;
910

1011
interface SnapshotLoadingCardProps {
1112
title: string;
@@ -17,8 +18,7 @@ export function SnapshotLoadingCard({
1718
completed,
1819
total,
1920
}: SnapshotLoadingCardProps) {
20-
// TODO: calculate throughput
21-
const throughput = 0;
21+
const throughput = useValuePerSecond(completed);
2222

2323
const throughputObj = throughput == null ? undefined : byteSize(throughput);
2424
const completedObj = completed == null ? undefined : byteSize(completed);
@@ -58,7 +58,7 @@ export function SnapshotLoadingCard({
5858
)}
5959
</Flex>
6060

61-
<Bars value={throughput ?? 0} max={MAX_THROUGHPUT} />
61+
<Bars value={throughput ?? 0} max={MAX_THROUGHPUT_BYTES_PER_S} />
6262
</Flex>
6363
</Card>
6464
);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}

0 commit comments

Comments
 (0)