Skip to content

Commit eaa3c49

Browse files
committed
Fix metrics maths
1 parent 7e34373 commit eaa3c49

1 file changed

Lines changed: 44 additions & 11 deletions

File tree

tools/tpcc-dashboard/src/App.tsx

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,32 @@ import type { NewOrderThroughtputChartData } from './NewOrderThroughtputChart';
55
import NewOrderThroughtputChart from './NewOrderThroughtputChart';
66
import type { State } from './module_bindings/types';
77

8+
function getTpmC(state: State): number | null {
9+
const measureStartDate = new Date(Number(state.measureStartMs));
10+
const measureEndDate = new Date(Number(state.measureEndMs));
11+
// If we are in warmup, return "Warmup in progress..."
12+
if (Date.now() < measureStartDate.getTime()) {
13+
return null;
14+
}
15+
16+
// If the run is in progress we calculate the ellapsed time based on the current time,
17+
if (Date.now() < measureEndDate.getTime()) {
18+
const ellapsedTimeSec = (Date.now() - measureStartDate.getTime()) / 1000;
19+
const tpmC = (Number(state.orderCount) / ellapsedTimeSec) * 60;
20+
return Math.trunc(tpmC);
21+
}
22+
23+
// otherwise we calculate it based on the measure start and end date
24+
const ellapsedTimeSec =
25+
(measureEndDate.getTime() - measureStartDate.getTime()) / 1000;
26+
const tpmC = (Number(state.orderCount) / ellapsedTimeSec) * 60;
27+
return Math.trunc(tpmC);
28+
}
29+
830
function App() {
931
const conn = useContext(SpacetimeDBContext);
1032
const [state, setState] = useState<State | null>(null);
33+
const [measuredTransactionCount, setMeasuredTransactionCount] = useState(0);
1134
const [throughputData, setThroughputData] = useState<
1235
NewOrderThroughtputChartData[]
1336
>([]);
@@ -20,6 +43,12 @@ function App() {
2043
conn?.db.state.onUpdate((_, old, state) => {
2144
setState(state);
2245

46+
if (Date.now() >= Number(state.measureStartMs)) {
47+
setMeasuredTransactionCount(
48+
prev => prev + Number(state.orderCount - old.orderCount)
49+
);
50+
}
51+
2352
setThroughputData(prevData => {
2453
const next = [
2554
...prevData,
@@ -55,20 +84,24 @@ function App() {
5584
const measureStartDate = new Date(Number(state.measureStartMs));
5685
const measureEndDate = new Date(Number(state.measureEndMs));
5786

58-
// If the is in progress we calculate the ellapsed time based on the current time,
59-
// otherwise we calculate it based on the measure end date
60-
const ellapsedTimeSec =
61-
Date.now() > measureEndDate.getTime()
62-
? (measureEndDate.getTime() - measureStartDate.getTime()) / 1000
63-
: (Date.now() - measureStartDate.getTime()) / 1000;
64-
const tpmC = (Number(state.orderCount) / ellapsedTimeSec) * 60;
87+
const tpmC = getTpmC(state);
6588

6689
return (
6790
<>
68-
<p>measureStartMs: {measureStartDate.toLocaleTimeString()}</p>
69-
<p>measureEndMs: {measureEndDate.toLocaleTimeString()}</p>
70-
<p>total transactions: {state.orderCount}</p>
71-
<p>MQTh: {Math.trunc(tpmC)} tpmC</p>
91+
<p>
92+
Measured duration: {measureStartDate.toLocaleTimeString()} -{' '}
93+
{measureEndDate.toLocaleTimeString()} (
94+
{Math.round(
95+
(measureEndDate.getTime() - measureStartDate.getTime()) / 60000
96+
)}{' '}
97+
minutes)
98+
</p>
99+
<p>Total transactions: {state.orderCount}</p>
100+
<p>Measured transactions: {measuredTransactionCount}</p>
101+
<p>
102+
MQTh:{' '}
103+
{tpmC === null ? 'Warmup in progress...' : Math.trunc(tpmC) + ' tpmC'}
104+
</p>
72105
<NewOrderThroughtputChart
73106
data={throughputData}
74107
runStartMs={Number(state.runStartMs)}

0 commit comments

Comments
 (0)