Skip to content

Commit

Permalink
bench: add k6 example script
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Aug 19, 2021
1 parent 300234e commit d423833
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@
/yarn-error.log
yarn-debug.log*
.yarn-integrity

.k6/k6
23 changes: 23 additions & 0 deletions .k6/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Benchmarking with k6

This folder contains benchmark/stress test scenarios to be used with [k6][].

We rely on the [xk6-cable][] extension, so you must first build a custom k6 executable (following the instructions described in the xk6-cable repo) and keep it in the `.k6/` folder.

## Scenarios

All scenarios support the following env vars:

- `CABLE_URL`: WebSocket connection url (defaults to `ws://localhost:8080/cable`).
- `WORKSPACE`: Workspace ID.

### `chat.js`

Connect to a chat room, send N (env `NUM` or 5) messages and expect to receive own messages back:

```sh
./k6 run --vus 20 --duration 10s chat.js
```

[k6]: https://k6.io
[xk6-cable]: https://github.com/anycable/xk6-cable
66 changes: 66 additions & 0 deletions .k6/chat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { check, sleep, fail } from "k6";
import cable from "k6/x/cable";
import { randomIntBetween } from "https://jslib.k6.io/k6-utils/1.1.0/index.js";

import { Trend } from "k6/metrics";

let rttTrend = new Trend("rtt", true);

let userId = `100${__VU}`;
let userName = `Kay${userId}`;

const URL = __ENV.CABLE_URL || "ws://localhost:8080/cable";
const WORKSPACE = __ENV.WORKSPACE || "demo";
const MESSAGES_NUM = parseInt(__ENV.NUM || "5");

export let options = {
thresholds: {
checks: ["rate>0.9"],
},
};

export default function () {
let client = cable.connect(URL, {
cookies: `uid=${userName}/${userId}`,
});

if (
!check(client, {
"successful connection": (obj) => obj,
})
) {
fail("connection failed");
}

let channel = client.subscribe("ChatChannel", { id: WORKSPACE });

if (
!check(channel, {
"successful subscription": (obj) => obj,
})
) {
fail("failed to subscribe");
}

for (let i = 0; i < MESSAGES_NUM; i++) {
let startMessage = Date.now();
channel.perform("speak", { message: `hello from ${userName}` });

let message = channel.receive({ author_id: userId });

if (
!check(message, {
"received its own message": (obj) => obj,
})
) {
fail("expected message hasn't been received");
}

let endMessage = Date.now();
rttTrend.add(endMessage - startMessage);

sleep(randomIntBetween(5, 10) / 10);
}

client.disconnect();
}

0 comments on commit d423833

Please sign in to comment.