-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds k6 loadtesting script for ws server
- Loading branch information
Showing
5 changed files
with
54 additions
and
1 deletion.
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
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,3 @@ | ||
FROM nginx:alpine | ||
COPY hello.json /usr/share/nginx/html/hello.json | ||
COPY default.conf /etc/nginx/conf.d/default.conf |
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,7 @@ | ||
server { | ||
listen 80; | ||
location / { | ||
root /usr/share/nginx/html; | ||
index hello.json; | ||
} | ||
} |
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 @@ | ||
{"result": "success", "message": "The server is up and running."} |
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,42 @@ | ||
import ws from 'k6/ws'; | ||
import { check } from 'k6'; | ||
import { Counter } from 'k6/metrics'; | ||
|
||
// A counter for the number of messages sent | ||
const counterSent = new Counter('ws_messages_sent'); | ||
const counterRecived = new Counter('ws_messages_recived'); | ||
|
||
const expectedResponse = '{"result": "success", "message": "The server is up and running."}'; | ||
|
||
export default function () { | ||
const url = 'ws://localhost:8080/'; | ||
const params = { tags: { my_tag: 'hello' } }; | ||
|
||
let counter = 100; | ||
|
||
const res = ws.connect(url, params, function (socket) { | ||
socket.on('open', () => { | ||
console.log('connected'); | ||
// Send as many messages as possible | ||
|
||
const iterations = counter; | ||
for (let i = 0; i < iterations; i++) { | ||
socket.send('Hello, server!'); | ||
|
||
counterSent.add(1); | ||
} | ||
}); | ||
|
||
socket.on('message', (data) => { | ||
check(data, { 'Got Expected message': (d) => d === expectedResponse }); | ||
counterRecived.add(1); | ||
counter--; | ||
if (counter == 0) { | ||
socket.close(); | ||
} | ||
}); | ||
socket.on('close', () => console.log('disconnected')); | ||
}); | ||
|
||
check(res, { 'status is 101': (r) => r && r.status === 101 }); | ||
} |