Skip to content

Instantly share code, notes, and snippets.

@pulsejet
Last active March 1, 2024 06:14
Show Gist options
  • Save pulsejet/15278a61b47df6066346d36f9071065a to your computer and use it in GitHub Desktop.
Save pulsejet/15278a61b47df6066346d36f9071065a to your computer and use it in GitHub Desktop.
NDN-Play testbed diagnostic
/**
* Testbed diagnostic script for NDN play.
* http://play.ndn.today/?testbed=1&script=https://gist.githubusercontent.com/pulsejet/15278a61b47df6066346d36f9071065a/raw/testbed-diagnostic.ts
* It will use FCH to connect to the nearest testbed node and ping all nodes.
* A table will be printed to the JS console with ping times.
*/
import { connectToNetwork } from "@ndn/autoconfig";
import { Endpoint } from "@ndn/endpoint";
import { Forwarder } from "@ndn/fw";
try {
// wait 500ms
await new Promise((r) => setTimeout(r, 500));
console.log('Finding nearest testbed node for diagnostics');
// make FCH query
const fw = Forwarder.create();
const faces = await connectToNetwork({ fw });
if (!faces.length)
throw new Error('No testbed routers found');
// get node URL
console.log(`Connected to ${faces[0]}`);
// connect to node
const endpoint = new Endpoint({ fw });
// collect logs for table
const logs = [];
// ping each node from connected node
const nodes = node.nfw.topo.nodes;
for (const n_ of nodes.get()) {
const n = n_ as typeof node;
// strip ndn: from prefix
const prefix = n_.prefix?.replace('ndn:', '');
if (!prefix) {
console.warn('Skipping node without prefix', node);
continue;
}
// ping node
for (let i = 0; i < 3; i++) {
try {
const t0 = Date.now();
const data = await endpoint.consume(`${prefix}/ping/${Math.trunc(Math.random() * 1e8)}`);
const t = Date.now() - t0;
logs.push([n.label, n_.prefix, t])
console.log("Interest satisfied", `${data.name}`, `${t}ms`);
// mark reachable on topology
n.extra.color = "lightgreen"
break;
} catch (err: unknown) {
console.error(err);
// mark unreachable on topology
n.extra.color = "red"
}
logs.push([n.label, n_.prefix, 'FAIL'])
}
// update color immediately
n.nfw.updateColors()
}
// pretty print table to console
logs.sort((a, b) => isNaN(a[2]) ? 1 : isNaN(b[2]) ? -1 : a[2] - b[2]);
console.table(logs);
faces[0].close();
} catch (e) {
console.error('Failed to run testbed diagnostics', e)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment