Skip to content

Commit

Permalink
Merge pull request #2 from orange-cloudfoundry/options-env
Browse files Browse the repository at this point in the history
Options and protocol env
  • Loading branch information
gberche-orange authored Oct 15, 2024
2 parents 647ac24 + 8fd7d21 commit d8e6b33
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ avg by (target) (iperf_received_bytes / iperf_received_seconds * 8)

Configuration is via the following environment variables:

| Variable | Required? | Description | Default |
| ------------------ | --------- | ------------------------------------------------------------------------ | ----------------------- |
| `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) |
| Variable | Required? | Description | Default |
|--------------------|-----------|----------------------------------------------------------------------------------------------------------------|-------------------------|
| `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
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { readFileSync } from "fs";

enum ConfigKey {
TargetList = "TARGET_LIST",
Options = "OPTIONS",
TestIntervalMs = "TEST_INTERVAL_MS",
Protocol = "PROTOCOL",
}

const loadedConfig: Partial<{ [key in ConfigKey]: string }> = {};
Expand Down
48 changes: 36 additions & 12 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ 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 @@ -17,25 +19,47 @@ async function getMeasurements(): Promise<string[]> {
for (const target of targets) {
const tags = {
target,
options,
};

try {
const iperfCmd = await exec(`iperf3 -c ${target} --json`);
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 d8e6b33

Please sign in to comment.