Skip to content

Commit

Permalink
Add support for udp protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
gberche-orange committed Oct 15, 2024
1 parent 07491f6 commit 8fd7d21
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Configuration is via the following environment variables:
| `TARGET_LIST` | yes | Comma separated list of host names or IP addresses to run tests against. | n/a |
| `TEST_INTERVAL_MS` | no | How often to run iperf tests. | 600000ms (= 10 minutes) |
| `OPTIONS` | no | Additional [iperf options](https://github.com/esnet/iperf/blob/master/docs/invoking.rst) (e.g. `--bitrate 1k`) | none |
| `PROTOCOL` | no | One of `tcp` or `udp` | `tcp` |

### `iperf` Server

Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ enum ConfigKey {
TargetList = "TARGET_LIST",
Options = "OPTIONS",
TestIntervalMs = "TEST_INTERVAL_MS",
Protocol = "PROTOCOL",
}

const loadedConfig: Partial<{ [key in ConfigKey]: string }> = {};
Expand Down
46 changes: 34 additions & 12 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const exec = util.promisify(execRaw);
// get config
const targetList = getConfig(ConfigKey.TargetList);
const options = getConfig(ConfigKey.Options);
const protocol = getConfig(ConfigKey.Protocol, "tcp");
const targets = targetList.split(",").map((t) => t.trim());
const testIntervalMs = parseInt(getConfig(ConfigKey.TestIntervalMs, "600000"));

Expand All @@ -22,22 +23,43 @@ async function getMeasurements(): Promise<string[]> {
};

try {
const iperfCmd = await exec(`iperf3 -c ${target} --json ${options}`);
let udpOptionStr = "";
if (protocol == "udp") {
udpOptionStr = "--udp";
}

const iperfCmd = await exec(`iperf3 -c ${target} ${udpOptionStr} --json ${options}`);
const result = JSON.parse(iperfCmd.stdout);
if (result["error"]) {
throw result["error"];
}

measurements.push(formatMeasurement("iperf_sent_bytes", tags, parseFloat(result["end"]["sum_sent"]["bytes"])));
measurements.push(
formatMeasurement("iperf_sent_seconds", tags, parseFloat(result["end"]["sum_sent"]["seconds"])),
);
measurements.push(
formatMeasurement("iperf_received_bytes", tags, parseFloat(result["end"]["sum_received"]["bytes"])),
);
measurements.push(
formatMeasurement("iperf_received_seconds", tags, parseFloat(result["end"]["sum_received"]["seconds"])),
);
switch(protocol) {
case "tcp":
measurements.push(formatMeasurement("iperf_sent_bytes", tags, parseFloat(result["end"]["sum_sent"]["bytes"])));
measurements.push(
formatMeasurement("iperf_sent_seconds", tags, parseFloat(result["end"]["sum_sent"]["seconds"])),
);
measurements.push(
formatMeasurement("iperf_received_bytes", tags, parseFloat(result["end"]["sum_received"]["bytes"])),
);
measurements.push(
formatMeasurement("iperf_received_seconds", tags, parseFloat(result["end"]["sum_received"]["seconds"])),
);
break;
case "udp":
measurements.push(
formatMeasurement("iperf_lost_packets", tags, parseFloat(result["end"]["sum"]["lost_packets"])),
);
measurements.push(
formatMeasurement("iperf_received_bytes", tags, parseFloat(result["end"]["sum"]["bytes"])),
);
measurements.push(
formatMeasurement("iperf_received_seconds", tags, parseFloat(result["end"]["sum"]["seconds"])),
);
break;
default:
throw `unsupported protocol ${protocol}`;
}
} catch (e) {
log(`Could not get iperf metrics for ${target}`, e);
continue;
Expand Down

0 comments on commit 8fd7d21

Please sign in to comment.