Skip to content

Commit

Permalink
fix: latency calculation in link reliability check (#7038)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCalzone authored Jul 22, 2024
1 parent f618d14 commit a0a3432
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
43 changes: 33 additions & 10 deletions packages/zwave-js/src/lib/node/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7245,6 +7245,11 @@ ${formatRouteHealthCheckSummary(this.id, otherNode.id, summary)}`,
max: 0,
average: 0,
},
rtt: {
min: Number.POSITIVE_INFINITY,
max: 0,
average: 0,
},
ackRSSI: {
min: 0,
max: Number.NEGATIVE_INFINITY,
Expand Down Expand Up @@ -7291,7 +7296,6 @@ ${formatRouteHealthCheckSummary(this.id, otherNode.id, summary)}`,
// TODO: report progress with throttle

let txReport: TXReport | undefined;
let latency = 0;

const basicSetAPI = this.commandClasses.Basic.withOptions({
// Don't change the node status when the ACK is missing. We're likely testing the limits here.
Expand Down Expand Up @@ -7330,15 +7334,34 @@ ${formatRouteHealthCheckSummary(this.id, otherNode.id, summary)}`,

// Measure the RTT or latency, whatever is available
const rtt = Date.now() - lastStart;
latency = Math.max(
latency,
txReport ? txReport.txTicks * 10 : rtt,
);
result.latency.min = Math.min(result.latency.min, latency);
result.latency.max = Math.max(result.latency.max, latency);
// incrementally update the average latency
result.latency.average += (latency - result.latency.average)
/ round;
result.rtt.min = Math.min(result.rtt.min, rtt);
result.rtt.max = Math.max(result.rtt.max, rtt);
// incrementally update the average rtt
result.rtt.average += (rtt - result.rtt.average) / round;

if (txReport) {
const latency = txReport.txTicks * 10;
if (result.latency) {
result.latency.min = Math.min(
result.latency.min,
latency,
);
result.latency.max = Math.max(
result.latency.max,
latency,
);
// incrementally update the average RTT
result.latency.average +=
(latency - result.latency.average)
/ round;
} else {
result.latency = {
min: latency,
max: latency,
average: latency,
};
}
}
} catch (e) {
if (isZWaveError(e)) {
if (
Expand Down
8 changes: 7 additions & 1 deletion packages/zwave-js/src/lib/node/_Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,13 @@ export interface LinkReliabilityCheckResult {
commandErrors: number;
missingResponses?: number;

latency: {
latency?: {
min: number;
max: number;
average: number;
};

rtt: {
min: number;
max: number;
average: number;
Expand Down

0 comments on commit a0a3432

Please sign in to comment.