-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(cherry picked from commit d423833)
- Loading branch information
Showing
6 changed files
with
116 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,5 @@ | |
/yarn-error.log | ||
yarn-debug.log* | ||
.yarn-integrity | ||
|
||
.k6/k6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# 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 | ||
``` | ||
|
||
This scenario uses ramping VUs executor, you can control the max number of VUs and the total duration via the `MAX` and `TIME` env vars (default to 20 and 120 respectively). | ||
|
||
[k6]: https://k6.io | ||
[xk6-cable]: https://github.com/anycable/xk6-cable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
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"; | ||
|
||
// The number of messages each VU sends during an iteration | ||
const MESSAGES_NUM = parseInt(__ENV.NUM || "5"); | ||
// Max VUs during the peak | ||
const MAX = parseInt(__ENV.MAX || "20"); | ||
// Total test duration | ||
const TIME = parseInt(__ENV.TIME || "120"); | ||
|
||
export let options = { | ||
thresholds: { | ||
checks: ["rate>0.9"], | ||
}, | ||
scenarios: { | ||
chat: { | ||
executor: "ramping-vus", | ||
startVUs: (MAX / 10 || 1) | 0, | ||
stages: [ | ||
{ duration: `${TIME / 3}s`, target: (MAX / 4) | 0 }, | ||
{ duration: `${(7 * TIME) / 12}s`, target: MAX }, | ||
{ duration: `${TIME / 12}s`, target: 0 }, | ||
], | ||
}, | ||
}, | ||
}; | ||
|
||
export default function () { | ||
let client = cable.connect(URL, { | ||
cookies: `uid=${userName}/${userId}`, | ||
receiveTimeoutMS: 30000, | ||
}); | ||
|
||
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(); | ||
} |