mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-19 22:46:20 -05:00
* Speed Test calculator: ping, download, and upload against the API Users asking whether the hotspot is fast enough, or how many people can stream video at once, had no objective tool that works offline. This adds one under Calculators > Tools & Reference. Backend (wrolpi/speedtest.py, /api/speedtest): * ping: tiny no-store JSON the browser times for round trip latency. * download: streams slices of a pre-built random buffer for a clamped number of seconds (0.1-15). Random so no proxy or compression can flatter the result; slices so no bytes are generated per request. * upload: streaming-body handler that reads and discards the body and reports bytes and elapsed time. Requests over 8 MiB are refused. * info: the client address Caddy forwarded plus how many tests are in flight. Sanic now trusts one proxy hop (PROXIES_COUNT = 1) so request.remote_addr is the browser, not Caddy. Nothing else read that field before. There is deliberately no one-test-at-a-time lock. Several people testing at once is how a user learns how the link shares between streamers, so the server only counts in-flight tests (a shared_ctx Value created pre-fork so every worker sees the same number) and the UI reports how many others ran alongside. Both Caddyfiles route /api/speedtest/* unbuffered, as the SSE chat route already is, so throughput is measured live rather than in bursts. Frontend: * speedtest.js holds the logic with no React in it (bucketed throughput, steady-state average that drops the first second, ping stats, stream-capacity tiers, hotspot address check) so Jest drives it with fake fetch and streams. * SpeedTestCalculator runs 20 pings, a 10 s download, and a 10 s upload with a live figure and rolling trace, then shows Mbps and MB/s, ping verdict, the client address with a hotspot badge, and a table of how many 480p..4K streams the download speed supports. Upload is sequential POSTs of one 4 MiB Blob because Safari cannot stream a request body. Nothing is saved. * Sparkline is a new inline-SVG ui component (no chart dependency), added to /theme-sample and checked in all four themes. A missing sample is a gap, not a plunge to zero. The page states that it measures the network path, not the drive that actually serves video files. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * speedtest: bound uploads in time, close the Start race, address review Review on #708 found three bugs and some polish: * Start could be clicked again while the first fetchInfo() was still pending, because the phase only left `idle` after that await. A second click made a second AbortController and a second concurrent run, and Cancel only reached the later one. start() now refuses when a controller already exists and enters a `starting` phase synchronously, so the button is Cancel before the first await. * The upload phase could overrun the advertised 10 s by however long the in-flight 4 MiB POST took: on a slow hotspot, the very link this tool exists for, that is tens of seconds. runUpload() now aborts the in-flight request at the phase deadline (counting only completed requests) and uses 1 MiB bodies so a few-Mbps link still completes several inside the phase. * The server upload sink was capped by size only, so a slow or paused body could hold a worker until 8 MiB eventually arrived. It now also enforces MAX_UPLOAD_SECONDS (15 s, matching download) and answers 408 when exceeded. Both caps together are what "cannot hold a worker" actually requires. Also: a NaN `seconds` now gets a NaN message instead of "must not be negative"; the module, Sparkline, and gallery docstrings are trimmed to purpose; and the upload counter is covered for the mid-stream 413, the 408, and a broken body. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
285 lines
12 KiB
JavaScript
285 lines
12 KiB
JavaScript
import React, {useContext, useEffect, useRef, useState} from "react";
|
|
import {StatusContext} from "../../contexts/contexts";
|
|
import {Button, Grid, Header, Label, Message, Panel, Progress, Sparkline, Statistic, StatisticGroup, Table, Text} from "../ui";
|
|
import {
|
|
bytesToMbps,
|
|
describePing,
|
|
fetchInfo,
|
|
formatMbps,
|
|
formatMs,
|
|
isHotspotAddress,
|
|
mbpsToMBps,
|
|
otherActiveTests,
|
|
PHASE_SECONDS,
|
|
PING_SAMPLES,
|
|
runDownload,
|
|
runPing,
|
|
runUpload,
|
|
streamCapacity,
|
|
} from "./speedtest";
|
|
|
|
const PHASES = {
|
|
idle: {label: 'Ready', order: 0},
|
|
starting: {label: 'Connecting…', order: 1},
|
|
ping: {label: 'Measuring latency…', order: 1},
|
|
download: {label: 'Downloading…', order: 2},
|
|
upload: {label: 'Uploading…', order: 3},
|
|
done: {label: 'Complete', order: 4},
|
|
cancelled: {label: 'Cancelled', order: 4},
|
|
error: {label: 'Failed', order: 4},
|
|
};
|
|
|
|
// The rolling trace shows this many buckets (PHASE_SECONDS at 250 ms each).
|
|
const TRACE_LENGTH = PHASE_SECONDS * 4;
|
|
|
|
const emptyResults = () => ({ping: null, download: null, upload: null});
|
|
|
|
/**
|
|
* Measures the network between this browser and the WROLPi: ping, then a 10 s download, then a
|
|
* 10 s upload, all against the API. Nothing is stored.
|
|
*/
|
|
export function SpeedTestCalculator() {
|
|
const {status} = useContext(StatusContext) || {};
|
|
const hotspotConnected = status?.hotspot_status === 'connected';
|
|
|
|
const [phase, setPhase] = useState('idle');
|
|
const [results, setResults] = useState(emptyResults);
|
|
const [live, setLive] = useState({mbps: 0, trace: [], progress: 0, rtts: []});
|
|
const [info, setInfo] = useState(null);
|
|
const [activeDuring, setActiveDuring] = useState(0);
|
|
const [error, setError] = useState(null);
|
|
const controllerRef = useRef(null);
|
|
|
|
// Abort a running test if the user navigates away.
|
|
useEffect(() => () => controllerRef.current?.abort(), []);
|
|
|
|
const onThroughput = buckets => {
|
|
const trace = buckets.samples.slice(-TRACE_LENGTH).map(s => bytesToMbps(s.bytes, s.width));
|
|
setLive({
|
|
mbps: buckets.currentMbps,
|
|
trace,
|
|
progress: Math.min(100, (buckets.elapsedMs / (PHASE_SECONDS * 1000)) * 100),
|
|
rtts: [],
|
|
});
|
|
};
|
|
|
|
const sampleActive = async () => {
|
|
try {
|
|
const during = await fetchInfo();
|
|
setActiveDuring(prev => Math.max(prev, during.active_tests));
|
|
} catch (e) {
|
|
// Context only; the test result does not depend on it.
|
|
}
|
|
};
|
|
|
|
const start = async () => {
|
|
if (controllerRef.current) {
|
|
// A run is already in progress (its first await has not resolved yet).
|
|
return;
|
|
}
|
|
const controller = new AbortController();
|
|
controllerRef.current = controller;
|
|
const {signal} = controller;
|
|
// Leave `idle` synchronously so the button reads Cancel before the first await.
|
|
setPhase('starting');
|
|
setResults(emptyResults());
|
|
setError(null);
|
|
setActiveDuring(0);
|
|
setLive({mbps: 0, trace: [], progress: 0, rtts: []});
|
|
|
|
try {
|
|
try {
|
|
setInfo(await fetchInfo());
|
|
} catch (e) {
|
|
setInfo(null);
|
|
throw new Error('The WROLPi API could not be reached.');
|
|
}
|
|
|
|
setPhase('ping');
|
|
const ping = await runPing({
|
|
signal,
|
|
onSample: rtts => setLive({mbps: 0, trace: [], progress: (rtts.length / PING_SAMPLES) * 100, rtts}),
|
|
});
|
|
if (signal.aborted) return;
|
|
setResults(r => ({...r, ping}));
|
|
|
|
setPhase('download');
|
|
setLive({mbps: 0, trace: [], progress: 0, rtts: []});
|
|
// Sample the counter while our own stream is open so concurrent testers show up.
|
|
const midDownload = setTimeout(sampleActive, (PHASE_SECONDS * 1000) / 2);
|
|
let download;
|
|
try {
|
|
download = await runDownload({signal, onProgress: onThroughput});
|
|
} finally {
|
|
clearTimeout(midDownload);
|
|
}
|
|
if (signal.aborted) return;
|
|
setResults(r => ({...r, download}));
|
|
|
|
setPhase('upload');
|
|
setLive({mbps: 0, trace: [], progress: 0, rtts: []});
|
|
const midUpload = setTimeout(sampleActive, (PHASE_SECONDS * 1000) / 2);
|
|
let upload;
|
|
try {
|
|
upload = await runUpload({signal, onProgress: onThroughput});
|
|
} finally {
|
|
clearTimeout(midUpload);
|
|
}
|
|
if (signal.aborted) return;
|
|
setResults(r => ({...r, upload}));
|
|
|
|
setPhase('done');
|
|
} catch (e) {
|
|
if (signal.aborted) {
|
|
return;
|
|
}
|
|
console.error(e);
|
|
setError(e.message || 'The speed test failed.');
|
|
setPhase('error');
|
|
} finally {
|
|
if (signal.aborted) {
|
|
setPhase('cancelled');
|
|
}
|
|
controllerRef.current = null;
|
|
}
|
|
};
|
|
|
|
const cancel = () => controllerRef.current?.abort();
|
|
|
|
const running = ['starting', 'ping', 'download', 'upload'].includes(phase);
|
|
const finished = ['done', 'cancelled', 'error'].includes(phase);
|
|
const others = otherActiveTests(info?.active_tests, activeDuring);
|
|
|
|
const liveRtt = live.rtts.length ? live.rtts[live.rtts.length - 1] : null;
|
|
const liveTrace = phase === 'ping' ? live.rtts : live.trace;
|
|
const liveValue = phase === 'ping'
|
|
? (liveRtt === null ? '—' : formatMs(liveRtt))
|
|
: formatMbps(live.mbps);
|
|
const liveUnit = phase === 'ping' ? 'ms' : 'Mbps';
|
|
|
|
return <div className='wrolpi-speedtest'>
|
|
<Panel>
|
|
<Grid>
|
|
<Grid.Col span={{base: 12, sm: 4}}>
|
|
{running
|
|
? <Button color='red' onClick={cancel} icon='stop'>Cancel</Button>
|
|
: <Button color='blue' onClick={start} icon='tachometer alternate'>
|
|
{finished ? 'Run again' : 'Start test'}
|
|
</Button>}
|
|
</Grid.Col>
|
|
<Grid.Col span={{base: 12, sm: 8}}>
|
|
<Text size='sm' c='dimmed' data-testid='speedtest-phase'>{PHASES[phase].label}</Text>
|
|
{running && <Progress percent={live.progress} showPercent={false} color='blue'
|
|
indeterminate={phase === 'starting'}/>}
|
|
</Grid.Col>
|
|
</Grid>
|
|
|
|
{running && phase !== 'starting' && <div style={{marginTop: '1em'}} data-testid='speedtest-live'>
|
|
<div style={{display: 'flex', alignItems: 'baseline', gap: '0.5em'}}>
|
|
<span style={{fontSize: '2.5rem', fontWeight: 600, fontVariantNumeric: 'tabular-nums'}}>
|
|
{liveValue}
|
|
</span>
|
|
<span style={{color: 'var(--muted)'}}>{liveUnit}</span>
|
|
</div>
|
|
<Sparkline values={liveTrace} height={56} color='blue'
|
|
label={phase === 'ping' ? 'Round trip time per ping' : `${PHASES[phase].label} throughput`}/>
|
|
</div>}
|
|
|
|
{error && <Message kind='error' title='Speed test failed'>{error}</Message>}
|
|
</Panel>
|
|
|
|
{(results.ping || results.download || results.upload) && <>
|
|
<Header as='h3'>Results</Header>
|
|
<StatisticGroup data-testid='speedtest-results'>
|
|
{results.download && <Statistic value={`${formatMbps(results.download.mbps)} Mbps`} label='Download'/>}
|
|
{results.upload && <Statistic value={`${formatMbps(results.upload.mbps)} Mbps`} label='Upload'/>}
|
|
{results.ping && <Statistic value={`${formatMs(results.ping.median)} ms`} label='Ping'/>}
|
|
{results.ping && <Statistic value={`${formatMs(results.ping.jitter)} ms`} label='Jitter'/>}
|
|
</StatisticGroup>
|
|
|
|
<Table>
|
|
<Table.Body>
|
|
{results.download && <Table.Row>
|
|
<Table.Cell style={{fontWeight: 600}}>Download</Table.Cell>
|
|
<Table.Cell>{formatMbps(results.download.mbps)} Mbps</Table.Cell>
|
|
<Table.Cell>{formatMbps(mbpsToMBps(results.download.mbps))} MB/s</Table.Cell>
|
|
<Table.Cell>peak {formatMbps(results.download.peak)} Mbps</Table.Cell>
|
|
</Table.Row>}
|
|
{results.upload && <Table.Row>
|
|
<Table.Cell style={{fontWeight: 600}}>Upload</Table.Cell>
|
|
<Table.Cell>{formatMbps(results.upload.mbps)} Mbps</Table.Cell>
|
|
<Table.Cell>{formatMbps(mbpsToMBps(results.upload.mbps))} MB/s</Table.Cell>
|
|
<Table.Cell>peak {formatMbps(results.upload.peak)} Mbps</Table.Cell>
|
|
</Table.Row>}
|
|
{results.ping && <Table.Row>
|
|
<Table.Cell style={{fontWeight: 600}}>Ping</Table.Cell>
|
|
<Table.Cell>{formatMs(results.ping.median)} ms median</Table.Cell>
|
|
<Table.Cell>{formatMs(results.ping.min)} ms best</Table.Cell>
|
|
<Table.Cell>
|
|
<PingVerdict median={results.ping.median}/>
|
|
{results.ping.lost > 0 && ` ${results.ping.lost} of ${PING_SAMPLES} lost`}
|
|
</Table.Cell>
|
|
</Table.Row>}
|
|
</Table.Body>
|
|
</Table>
|
|
|
|
<ConnectionContext info={info} hotspotConnected={hotspotConnected} others={others}/>
|
|
|
|
{results.download && <StreamCapacity mbps={results.download.mbps}/>}
|
|
</>}
|
|
|
|
{phase === 'idle' && <Text size='sm' c='dimmed' style={{marginTop: '1em'}}>
|
|
Runs {PING_SAMPLES} pings, then a {PHASE_SECONDS} second download and a {PHASE_SECONDS} second
|
|
upload between this device and the WROLPi. Nothing is saved.
|
|
</Text>}
|
|
</div>
|
|
}
|
|
|
|
function PingVerdict({median}) {
|
|
const {kind, text} = describePing(median);
|
|
const color = {success: 'green', warning: 'yellow', danger: 'red'}[kind] || 'grey';
|
|
return <Label color={color}>{text}</Label>;
|
|
}
|
|
|
|
export function ConnectionContext({info, hotspotConnected, others}) {
|
|
if (!info) {
|
|
return null;
|
|
}
|
|
const viaHotspot = hotspotConnected && isHotspotAddress(info.client_ip);
|
|
return <Text size='sm' style={{marginTop: '0.5em'}} data-testid='speedtest-context'>
|
|
Tested from <code>{info.client_ip}</code>{' '}
|
|
{viaHotspot
|
|
? <Label color='blue' icon='wifi'>via WROLPi hotspot</Label>
|
|
: <Label color='grey'>via LAN</Label>}
|
|
{others > 0 && <span> {others === 1 ? '1 other speed test was' : `${others} other speed tests were`} running
|
|
at the same time, so the link was shared.</span>}
|
|
</Text>;
|
|
}
|
|
|
|
export function StreamCapacity({mbps}) {
|
|
const rows = streamCapacity(mbps);
|
|
return <div data-testid='speedtest-capacity'>
|
|
<Header as='h3'>Simultaneous video streams</Header>
|
|
<Table>
|
|
<Table.Header>
|
|
<Table.Row>
|
|
<Table.HeaderCell>Quality</Table.HeaderCell>
|
|
<Table.HeaderCell>About</Table.HeaderCell>
|
|
<Table.HeaderCell>Streams at once</Table.HeaderCell>
|
|
</Table.Row>
|
|
</Table.Header>
|
|
<Table.Body>
|
|
{rows.map(row => <Table.Row key={row.key}>
|
|
<Table.Cell style={{fontWeight: 600}}>{row.label}</Table.Cell>
|
|
<Table.Cell>{row.mbps} Mbps</Table.Cell>
|
|
<Table.Cell>{row.streams}</Table.Cell>
|
|
</Table.Row>)}
|
|
</Table.Body>
|
|
</Table>
|
|
<Text size='sm' c='dimmed'>
|
|
Measures the network between this device and the WROLPi. Videos are served from the drive, which
|
|
can also limit how many play at once. Bitrates are typical for each resolution; individual videos
|
|
vary.
|
|
</Text>
|
|
</div>;
|
|
}
|