Skip to content

Commit

Permalink
Adds k6 loadtesting script for ws server
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Apr 20, 2024
1 parent dcb0937 commit 1b90ba8
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/http_backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func main() {

dispatcher := dispatch.NewPipeDispatcher(backend)
dispatcher.Use(ErrHandler)
dispatcher.Use(request.NewTrottlerMiddleware(10))
dispatcher.Use(request.NewTrottlerMiddleware(100))

channel := channel.NewDefaultChannel("/", dispatcher, channel.WithOriginPatterns("*"))

Expand Down
3 changes: 3 additions & 0 deletions loadtesting/Dockerfile
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
7 changes: 7 additions & 0 deletions loadtesting/default.conf
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;
}
}
1 change: 1 addition & 0 deletions loadtesting/hello.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"result": "success", "message": "The server is up and running."}
42 changes: 42 additions & 0 deletions loadtesting/k6.js
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 });
}

0 comments on commit 1b90ba8

Please sign in to comment.