perf(beszel): deep memoization pass for live chart rendering

- Set withDots={false} to eliminate thousands of hidden SVG Dot nodes
- Disable Recharts animation (isAnimationActive: false) during data updates
- Merge ChartPanel + BeszelAreaChart into BeszelChartPanel to fix
  React.memo being defeated by children prop creating new refs
- Memoize yAxisProps inside BeszelAreaChart instead of rebuilding inline
- Hoist chartStyle, panelStyle, CPU_Y_AXIS_DOMAIN to module constants
- Memoize subscription input/enabled in useLiveStats
- Replace O(n) find() with Map lookup in useDockerChartData hot path
This commit is contained in:
Thomas Camlong committed 2026-06-15 16:12:47 +02:00
1 parent 583b1d2935
commit 8134b168e1
3 files changed
+88 -107

No files matched your search

@@ -12,8 +12,8 @@ import classes from "./component.module.css";
import type { WidgetComponentProps } from "../definition"; import type { WidgetComponentProps } from "../definition";
import { containerColors } from "../beszel/_shared/colors"; import { containerColors } from "../beszel/_shared/colors";
import { import {
BeszelAreaChart, BeszelChartPanel,
ChartPanel, CPU_Y_AXIS_DOMAIN,
useContainerNames, useContainerNames,
useDockerChartData, useDockerChartData,
useSystemChartData, useSystemChartData,
@@ -229,105 +229,69 @@ export default function BeszelSystemStatsWidget({
{activeStats && ( {activeStats && (
<SimpleGrid cols={cols} spacing="md"> <SimpleGrid cols={cols} spacing="md">
{options.showCpu && cpuData.length > 0 && ( {options.showCpu && cpuData.length > 0 && (
<ChartPanel title={t("chart.cpu.title")} subtitle={t("chart.cpu.subtitle")}> <BeszelChartPanel
<BeszelAreaChart title={t("chart.cpu.title")}
h={CHART_HEIGHT} subtitle={t("chart.cpu.subtitle")}
data={cpuData} chartProps={{ h: CHART_HEIGHT, data: cpuData, series: cpuSeries, yAxisFormatter: chartAxisFormatters.percent, yAxisDomain: CPU_Y_AXIS_DOMAIN, tooltipProps: tooltipPercent }}
series={cpuSeries} />
yAxisFormatter={chartAxisFormatters.percent}
yAxisDomain={[0, "auto"]}
tooltipProps={tooltipPercent}
/>
</ChartPanel>
)} )}
{options.showMemory && memoryData.length > 0 && ( {options.showMemory && memoryData.length > 0 && (
<ChartPanel title={t("chart.memory.title")} subtitle={t("chart.memory.subtitle")}> <BeszelChartPanel
<BeszelAreaChart title={t("chart.memory.title")}
h={CHART_HEIGHT} subtitle={t("chart.memory.subtitle")}
data={memoryData} chartProps={{ h: CHART_HEIGHT, data: memoryData, type: "stacked", series: memorySeries, yAxisFormatter: chartAxisFormatters.gb, tooltipProps: tooltipGB }}
type="stacked" />
series={memorySeries}
yAxisFormatter={chartAxisFormatters.gb}
tooltipProps={tooltipGB}
/>
</ChartPanel>
)} )}
{options.showDisk && diskData.length > 0 && ( {options.showDisk && diskData.length > 0 && (
<ChartPanel title={t("chart.disk.title")} subtitle={t("chart.disk.subtitle")}> <BeszelChartPanel
<BeszelAreaChart title={t("chart.disk.title")}
h={CHART_HEIGHT} subtitle={t("chart.disk.subtitle")}
data={diskData} chartProps={{ h: CHART_HEIGHT, data: diskData, series: diskSeries, yAxisFormatter: chartAxisFormatters.gb, tooltipProps: tooltipGB }}
series={diskSeries} />
yAxisFormatter={chartAxisFormatters.gb}
tooltipProps={tooltipGB}
/>
</ChartPanel>
)} )}
{options.showDiskIO && diskIOData.length > 0 && ( {options.showDiskIO && diskIOData.length > 0 && (
<ChartPanel title={t("chart.diskIO.title")} subtitle={t("chart.diskIO.subtitle")}> <BeszelChartPanel
<BeszelAreaChart title={t("chart.diskIO.title")}
h={CHART_HEIGHT} subtitle={t("chart.diskIO.subtitle")}
data={diskIOData} chartProps={{ h: CHART_HEIGHT, data: diskIOData, series: diskIOSeries, yAxisFormatter: chartAxisFormatters.rate, tooltipProps: tooltipRate }}
series={diskIOSeries} />
yAxisFormatter={chartAxisFormatters.rate}
tooltipProps={tooltipRate}
/>
</ChartPanel>
)} )}
{options.showNetwork && networkData.length > 0 && ( {options.showNetwork && networkData.length > 0 && (
<ChartPanel title={t("chart.network.title")} subtitle={t("chart.network.subtitle")}> <BeszelChartPanel
<BeszelAreaChart title={t("chart.network.title")}
h={CHART_HEIGHT} subtitle={t("chart.network.subtitle")}
data={networkData} chartProps={{ h: CHART_HEIGHT, data: networkData, series: networkSeries, yAxisFormatter: chartAxisFormatters.rate, tooltipProps: tooltipRate }}
series={networkSeries} />
yAxisFormatter={chartAxisFormatters.rate}
tooltipProps={tooltipRate}
/>
</ChartPanel>
)} )}
{showDocker && containerSeries.length > 0 && ( {showDocker && containerSeries.length > 0 && (
<> <>
{options.showDockerCpu && dockerCpuData.length > 0 && ( {options.showDockerCpu && dockerCpuData.length > 0 && (
<ChartPanel title={t("chart.dockerCpu.title")} subtitle={t("chart.dockerCpu.subtitle")}> <BeszelChartPanel
<BeszelAreaChart title={t("chart.dockerCpu.title")}
h={CHART_HEIGHT} subtitle={t("chart.dockerCpu.subtitle")}
data={dockerCpuData} chartProps={{ h: CHART_HEIGHT, data: dockerCpuData, type: "stacked", series: containerSeries, yAxisFormatter: chartAxisFormatters.percent, tooltipProps: tooltipPercentTotal }}
type="stacked" />
series={containerSeries}
yAxisFormatter={chartAxisFormatters.percent}
tooltipProps={tooltipPercentTotal}
/>
</ChartPanel>
)} )}
{options.showDockerMemory && dockerMemData.length > 0 && ( {options.showDockerMemory && dockerMemData.length > 0 && (
<ChartPanel title={t("chart.dockerMemory.title")} subtitle={t("chart.dockerMemory.subtitle")}> <BeszelChartPanel
<BeszelAreaChart title={t("chart.dockerMemory.title")}
h={CHART_HEIGHT} subtitle={t("chart.dockerMemory.subtitle")}
data={dockerMemData} chartProps={{ h: CHART_HEIGHT, data: dockerMemData, type: "stacked", series: containerSeries, yAxisFormatter: chartAxisFormatters.bytes, tooltipProps: tooltipBytesTotal }}
type="stacked" />
series={containerSeries}
yAxisFormatter={chartAxisFormatters.bytes}
tooltipProps={tooltipBytesTotal}
/>
</ChartPanel>
)} )}
{options.showDockerNetwork && dockerNetData.length > 0 && ( {options.showDockerNetwork && dockerNetData.length > 0 && (
<ChartPanel title={t("chart.dockerNetwork.title")} subtitle={t("chart.dockerNetwork.subtitle")}> <BeszelChartPanel
<BeszelAreaChart title={t("chart.dockerNetwork.title")}
h={CHART_HEIGHT} subtitle={t("chart.dockerNetwork.subtitle")}
data={dockerNetData} chartProps={{ h: CHART_HEIGHT, data: dockerNetData, series: containerSeries, yAxisFormatter: chartAxisFormatters.rate, tooltipProps: tooltipRate }}
series={containerSeries} />
yAxisFormatter={chartAxisFormatters.rate}
tooltipProps={tooltipRate}
/>
</ChartPanel>
)} )}
</> </>
)} )}
+37 -24
View File
@@ -19,17 +19,19 @@ function prepareRecords<T>(records: T[], live: boolean) {
} }
const yAxisBase = { tickMargin: 0, tick: { fontSize: 10 } } as const; const yAxisBase = { tickMargin: 0, tick: { fontSize: 10 } } as const;
const activeDot = { r: 1, strokeWidth: 3 } as const; const chartStyle = { minWidth: 0, minHeight: 0 } as const;
const hiddenDot = { r: 0, strokeWidth: 0 } as const; const panelStyle = { minWidth: 0, overflow: "hidden" } as const;
const noAnimation = { isAnimationActive: false } as const;
export const CPU_Y_AXIS_DOMAIN: [number, string] = [0, "auto"];
interface ChartPanelProps { interface BeszelChartPanelProps {
title: string; title: string;
subtitle?: string; subtitle?: string;
children: React.ReactNode; chartProps: BeszelAreaChartProps;
} }
export const ChartPanel = memo(({ title, subtitle, children }: ChartPanelProps) => ( export const BeszelChartPanel = memo(({ title, subtitle, chartProps }: BeszelChartPanelProps) => (
<Stack gap={4} style={{ minWidth: 0, overflow: "hidden" }}> <Stack gap={4} style={panelStyle}>
<Group gap="xs"> <Group gap="xs">
<Text size="sm" fw={600}> <Text size="sm" fw={600}>
{title} {title}
@@ -40,7 +42,7 @@ export const ChartPanel = memo(({ title, subtitle, children }: ChartPanelProps)
</Text> </Text>
)} )}
</Group> </Group>
{children} <BeszelAreaChart {...chartProps} />
</Stack> </Stack>
)); ));
@@ -49,35 +51,45 @@ type BeszelAreaChartProps = Omit<AreaChartProps, "dataKey" | "curveType" | "with
yAxisDomain?: [number, string]; yAxisDomain?: [number, string];
}; };
export const BeszelAreaChart = memo( const BeszelAreaChart = memo(({
({ yAxisFormatter, yAxisDomain, yAxisProps, style, type = "default", ...props }: BeszelAreaChartProps) => ( yAxisFormatter,
yAxisDomain,
yAxisProps: yAxisPropsOverride,
type = "default",
...props
}: BeszelAreaChartProps) => {
const mergedYAxis = useMemo(
() => ({
...yAxisBase,
width: 32,
tickMargin: 2,
...(yAxisDomain ? { domain: yAxisDomain } : {}),
tickFormatter: yAxisFormatter,
...yAxisPropsOverride,
}),
[yAxisFormatter, yAxisDomain, yAxisPropsOverride],
);
return (
<AreaChart <AreaChart
dataKey="time" dataKey="time"
curveType="monotone" curveType="monotone"
withGradient={false} withGradient={false}
connectNulls connectNulls
withDots={true} withDots={false}
type={type} type={type}
strokeWidth={1} strokeWidth={1}
fillOpacity={0.2} fillOpacity={0.2}
activeDotProps={activeDot} areaChartProps={noAnimation}
dotProps={hiddenDot}
withXAxis withXAxis
withYAxis withYAxis
w="100%" w="100%"
style={{ minWidth: 0, minHeight: 0, ...style }} style={chartStyle}
yAxisProps={{ yAxisProps={mergedYAxis}
...yAxisBase,
width: 32,
tickMargin: 2,
...(yAxisDomain ? { domain: yAxisDomain } : {}),
tickFormatter: yAxisFormatter,
...yAxisProps,
}}
{...props} {...props}
/> />
), );
); });
export const useSystemChartData = ( export const useSystemChartData = (
systemStats: BeszelSystemStatsRecord[] | undefined, systemStats: BeszelSystemStatsRecord[] | undefined,
@@ -135,8 +147,9 @@ export const useDockerChartData = (
const { fmt, ordered } = prepareRecords(containerStats, live); const { fmt, ordered } = prepareRecords(containerStats, live);
return ordered.map((record) => { return ordered.map((record) => {
const point: Record<string, unknown> = { time: fmt(record.created) }; const point: Record<string, unknown> = { time: fmt(record.created) };
const byName = new Map(record.stats.map((c) => [c.n, c]));
for (const name of containerNames) { for (const name of containerNames) {
point[name] = extract(record.stats.find((c) => c.n === name)); point[name] = extract(byName.get(name));
} }
return point; return point;
}); });
@@ -1,6 +1,6 @@
"use client"; "use client";
import { useCallback, useEffect, useState } from "react"; import { useCallback, useEffect, useMemo, useState } from "react";
import { clientApi } from "@homarr/api/client"; import { clientApi } from "@homarr/api/client";
import type { BeszelContainerStatsRecord, BeszelSystemStatsRecord } from "@homarr/integrations/types"; import type { BeszelContainerStatsRecord, BeszelSystemStatsRecord } from "@homarr/integrations/types";
@@ -41,10 +41,14 @@ export const useLiveStats = (integrationIds: string[], systemId: string, enabled
setError(err instanceof Error ? err : new Error(String(err))); setError(err instanceof Error ? err : new Error(String(err)));
}, []); }, []);
clientApi.widget.beszel.subscribeSystemStats.useSubscription( const subscriptionInput = useMemo(() => ({ integrationIds, systemId }), [integrationKey, systemId]);
{ integrationIds, systemId }, const subscriptionEnabled = enabled && systemId !== "";
{ enabled: enabled && systemId !== "", onData, onError },
); clientApi.widget.beszel.subscribeSystemStats.useSubscription(subscriptionInput, {
enabled: subscriptionEnabled,
onData,
onError,
});
return { data, error }; return { data, error };
}; };