Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental Websockets merge #4131

Open
wants to merge 96 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
cabe5a0
Initial commit
mstoykov Apr 21, 2022
ea54fc1
Add golangci.yml
mstoykov Apr 21, 2022
ecc8669
License as AGPL v3
mstoykov Apr 22, 2022
00a57de
Fix race condition :facepalm:
mstoykov Apr 27, 2022
d6b4d20
Add github actions
mstoykov Apr 28, 2022
d9f44fd
Use github.com/mstoykov/k6-taskqueue-lib which was extracted from thi…
mstoykov Apr 28, 2022
f2bdcca
Make readystate an int instead of string
mstoykov Apr 29, 2022
4fc64f1
Add and test binaryType
mstoykov Apr 29, 2022
3b66170
Implement close code and reason and fix connected issues
mstoykov Apr 29, 2022
9f1aad3
Basic Autobahn test suite
codebien Jul 4, 2022
670e1c8
Added test for errors on dialing
codebien Jul 5, 2022
49e4383
Close body only if the response is not nil
codebien Jul 5, 2022
1bdcf49
Close the queue before return on errors
codebien Jul 5, 2022
66a690e
Merge pull request #2 from grafana/fix-on-error
mstoykov Jul 6, 2022
66b1dba
Merge pull request #1 from grafana/autobahn
mstoykov Jul 6, 2022
c6790c7
Update to k6 v0.39.0
mstoykov Jul 6, 2022
b362d0c
Fix binary message support :facepalm:
mstoykov Jul 6, 2022
0670435
Fix races where readyState moves "backwards"
mstoykov Jul 6, 2022
60c4ccc
Add error key to the error event
mstoykov Jul 6, 2022
216d855
Merge pull request #3 from grafana/fixesFromAutobahn
mstoykov Jul 11, 2022
3acd900
Fix `k6/x/events` -> `k6/x/timers` in example
mstoykov Aug 1, 2022
4ae9241
Rerun go mod tidy after cleaning indirect dependancies
mstoykov Aug 16, 2022
b0d7a35
Various fixes for races and leaking goroutines
mstoykov Aug 18, 2022
f2381be
Merge pull request #13 from grafana/lessRaces
oleiade Aug 19, 2022
110e88e
Trying more to not deadlock
mstoykov Aug 19, 2022
09da031
Merge pull request #15 from grafana/fixesForTheRaceFixes
oleiade Aug 19, 2022
54e1459
Don't use x/net/context
mstoykov Oct 4, 2022
52b1fdb
Bump the go version to 1.19
codebien Oct 4, 2022
0cfa5a3
Bump golangci version
codebien Oct 4, 2022
2565a84
Convert extension to use atlas-based metric tags (#12)
na-- Oct 4, 2022
17bb0ec
Support non-indexable tags (Metadata) (#14)
na-- Oct 17, 2022
54233fc
go mod tidy for the previous commit
codebien Oct 17, 2022
070ef38
Update to k6 v0.41
olegbespalov Nov 16, 2022
da21cfc
Makefile, CI, gitignore
olegbespalov Nov 16, 2022
2c250b4
Small refactoring
olegbespalov Nov 16, 2022
619987d
chore: event listeners
olegbespalov Nov 16, 2022
de2b8e5
Adding on* properties
olegbespalov Nov 16, 2022
df7f552
Allow unset the handlers, more tests
olegbespalov Nov 18, 2022
bb54907
Implement WebSocket customisation with headers (#25)
olegbespalov Nov 22, 2022
c2ef4b1
Add Cookies support
olegbespalov Nov 22, 2022
2526c53
Custom Tags (#28)
olegbespalov Nov 24, 2022
fa9b0a2
Fix the t.Fatal in go-routine
olegbespalov Nov 24, 2022
449f9b2
Compression
olegbespalov Nov 23, 2022
669bd60
Ping/Pong implementation
olegbespalov Nov 29, 2022
e1af109
chore: remove sync/atomic
olegbespalov Nov 30, 2022
a7d4fa9
Apply suggestions from code review
olegbespalov Nov 30, 2022
c0b00b6
Adress the code review comment
olegbespalov Nov 30, 2022
4ef61e9
Apply suggestions from code review
olegbespalov Nov 30, 2022
a35089c
adress the code review comment
olegbespalov Nov 30, 2022
acae12f
Local example, README fixes
olegbespalov Dec 1, 2022
d3c37a3
Update to k6 v0.43.1
mstoykov Apr 4, 2023
a1430bc
Update golangci.yml
mstoykov Apr 4, 2023
297ff14
onmesage->onmessage in test
mstoykov Apr 4, 2023
e3995f0
Fix a bunch of tests
mstoykov Apr 4, 2023
5693fec
Add goleak and update k6 to fix a leak
mstoykov Apr 4, 2023
d2d4b5a
Update k6 and add tests around races (#42)
mstoykov Apr 5, 2023
3a84011
Drop the buffering on a channel
mstoykov Apr 5, 2023
2d9dc2c
Ping should not send data if the connection is closing/closed
mstoykov Apr 5, 2023
baf151e
Use RegisterCallback more directly
mstoykov Apr 10, 2023
a9f5cb0
Drpping fmt.Println comments
mstoykov Apr 10, 2023
15fa17e
Split loop
mstoykov Apr 10, 2023
c98550d
Some more code cleanup
mstoykov Apr 10, 2023
652ea5c
Merge pull request #44 from grafana/dropDebuggingStuff
mstoykov Apr 19, 2023
c0aa605
Bump k6 version to v0.45.1
codebien Aug 10, 2023
08b8dfb
chore: refactoring of the Makefile
olegbespalov Aug 17, 2023
f15f879
CODEOWNERS
olegbespalov Aug 17, 2023
b1d7d8d
Getting rid of the local linter config
olegbespalov Aug 17, 2023
89daa2e
Makefile targets for the local linting
olegbespalov Aug 17, 2023
34d0f5f
chore: switch CI to the shared lint action
olegbespalov Aug 17, 2023
d1a7e02
Update README & templates
olegbespalov Aug 17, 2023
9822929
Apply suggestions from code review
olegbespalov Aug 18, 2023
55b7ed9
Use re-usable workflow
olegbespalov Aug 22, 2023
865528c
chore: minor change
olegbespalov Aug 22, 2023
ec8e44a
Decrease the number of connection in test because of windows
olegbespalov Aug 24, 2023
c9f0699
Use issue auto-assigner
olegbespalov Aug 24, 2023
aed7b6a
deps: k6@de19a6a1bc53a441670d1c44ae53f18c64372711
olegbespalov Oct 12, 2023
7c450fa
Add the k6's modtools_frozen to avoid dependency update
olegbespalov Oct 12, 2023
d021a4a
feat: allow name tag, and overwrite url tag with name tag if set (#60)
mkadirtan Feb 21, 2024
5558905
Use modulestest a lot more in tests
mstoykov Apr 24, 2024
7031d02
Fix all indentation in tests to be tabs and align them a bit
mstoykov Apr 24, 2024
0c7823e
Warn on not setting binarytype (#64)
mstoykov Apr 25, 2024
3ef1790
Update examples after binaryType changes
mstoykov Apr 29, 2024
d72cc50
Support subprotocols (#68)
mstoykov May 28, 2024
21b9fd5
Fix bufferedAmount not updating (#69)
mstoykov May 29, 2024
9a97c2e
Add extensions support to WebSocket
mstoykov May 23, 2024
44bae81
Bump k6 to v0.51.0
mstoykov May 29, 2024
f1d004d
Drop usage of lib.Group in tests
mstoykov Jun 6, 2024
7a69bb6
Change to using sobek instead of goja
mstoykov Jun 6, 2024
113a951
Fix passing undefined/null handler
olegbespalov Jun 7, 2024
75c13b2
Add support for Blob (#74)
joanlopez Jul 3, 2024
e2eeff7
Make blob the default binaryType
mstoykov Sep 10, 2024
3079f14
send: support ArrayBufferView as argument
mstoykov Sep 10, 2024
c8ce6f2
Make certain readyState is actually a number when returned
mstoykov Sep 27, 2024
52b1ef8
Fixes along grafana/k6#4017
mstoykov Oct 25, 2024
0972164
Add 'js/modules/k6/experimental/websockets/' from commit '52b1ef8ab51…
mstoykov Dec 18, 2024
4cf561d
Fix k6/experimental/websockets after merge of code
mstoykov Dec 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions examples/experimental/websockets/test-api.k6.io.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { randomString, randomIntBetween } from "https://jslib.k6.io/k6-utils/1.1.0/index.js";
import { WebSocket } from "k6/experimental/websockets"

let chatRoomName = 'publicRoom'; // choose your chat room name
let sessionDuration = randomIntBetween(5000, 60000); // user session between 5s and 1m


export default function() {
for (let i = 0; i < 4; i++) {
startWSWorker(i)
}
}

function startWSWorker(id) {
let url = `wss://test-api.k6.io/ws/crocochat/${chatRoomName}/`;
let ws = new WebSocket(url);
ws.binaryType = "arraybuffer";
ws.addEventListener("open", () => {
ws.send(JSON.stringify({ 'event': 'SET_NAME', 'new_name': `Croc ${__VU}:${id}` }));

ws.addEventListener("message", (e) => {
let msg = JSON.parse(e.data);
if (msg.event === 'CHAT_MSG') {
console.log(`VU ${__VU}:${id} received: ${msg.user} says: ${msg.message}`)
}
else if (msg.event === 'ERROR') {
console.error(`VU ${__VU}:${id} received:: ${msg.message}`)
}
else {
console.log(`VU ${__VU}:${id} received unhandled message: ${msg.message}`)
}
})


let intervalId = setInterval(() => {
ws.send(JSON.stringify({ 'event': 'SAY', 'message': `I'm saying ${randomString(5)}` }));
}, randomIntBetween(2000, 8000)); // say something every 2-8seconds


let timeout1id = setTimeout(function() {
clearInterval(intervalId)
console.log(`VU ${__VU}:${id}: ${sessionDuration}ms passed, leaving the chat`);
ws.send(JSON.stringify({ 'event': 'LEAVE' }));
}, sessionDuration);

let timeout2id = setTimeout(function() {
console.log(`Closing the socket forcefully 3s after graceful LEAVE`);
ws.close();
}, sessionDuration + 3000);

ws.addEventListener("close", () => {
clearTimeout(timeout1id);
clearTimeout(timeout2id);
console.log(`VU ${__VU}:${id}: disconnected`);
})
});
}

76 changes: 76 additions & 0 deletions examples/experimental/websockets/test-echo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { WebSocket } from "k6/experimental/websockets"

const CLOSED_STATE = 3

export default function() {
// local echo server should be launched with `make ws-echo-server-run`
var url = "wss://echo.websocket.org/"
var params = { "tags": { "my_tag": "hello" } };

let ws = new WebSocket(url, null, params)
ws.binaryType = "arraybuffer";
ws.onopen = () => {
console.log('connected')
ws.send(Date.now().toString())
}

let intervalId = setInterval(() => {
ws.ping();
console.log("Pinging every 1 sec (setInterval test)")
}, 1000);

let timeout1id = setTimeout(function() {
console.log('2 seconds passed, closing the socket')
clearInterval(intervalId)
ws.close()

}, 2000);

ws.onclose = () => {
clearTimeout(timeout1id);

console.log('disconnected')
}


ws.onping = () => {
console.log("PING!")
}

ws.onpong = () => {
console.log("PONG!")
}

// Multiple event handlers on the same event
ws.addEventListener("pong", () => {
console.log("OTHER PONG!")
})

ws.onmessage = (m) => {
let parsed = parseInt(m.data, 10)
if (Number.isNaN(parsed)) {
console.log('Not a number received: ', m.data)

return
}

console.log(`Roundtrip time: ${Date.now() - parsed} ms`);

let timeoutId = setTimeout(function() {
if (ws.readyState == CLOSED_STATE) {
console.log("Socket closed, not sending anything");

clearTimeout(timeoutId);
return;
}

ws.send(Date.now().toString())
}, 500);
}

ws.onerror = (e) => {
if (e.error != "websocket: close sent") {
console.log('An unexpected error occurred: ', e.error);
}
};
};
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ require (
github.com/grafana/xk6-output-prometheus-remote v0.5.0
github.com/grafana/xk6-redis v0.3.1
github.com/grafana/xk6-webcrypto v0.5.0
github.com/grafana/xk6-websockets v0.7.2
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
github.com/influxdata/influxdb1-client v0.0.0-20190402204710-8ff2fc3824fc
github.com/jhump/protoreflect v1.17.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ github.com/grafana/xk6-redis v0.3.1 h1:RqfmMLNx7vekBxwuTrFP9ErxeY/0H07a3HpQJYXYD
github.com/grafana/xk6-redis v0.3.1/go.mod h1:3e/U9i1Nm3WEaMy4nZSGMjVf8ZsFau+aXurYJhJ7MfQ=
github.com/grafana/xk6-webcrypto v0.5.0 h1:a5NMG/4itLDWprn5XbGaARwUdGPy9wO9z35Z7bDjG1k=
github.com/grafana/xk6-webcrypto v0.5.0/go.mod h1:yZMp9ZjcxLZML2ljcK6CxTI+XTP59vivtKszaH5xIE4=
github.com/grafana/xk6-websockets v0.7.2 h1:hwZfk+1zMLJZ2vXqy8WqShG4toHgY6Gw7EdFU37dSXg=
github.com/grafana/xk6-websockets v0.7.2/go.mod h1:91oE+otLmjYsPwBvxfv1+6tmoXKPZRPOXTnJveAs5Nk=
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI=
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0/go.mod h1:g5qyo/la0ALbONm6Vbp88Yd8NsDy6rZz+RcrMPxvld8=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 h1:asbCHRVmodnJTuQ3qamDwqVOIjwqUPTYmYuemVOx+Ys=
Expand Down
4 changes: 2 additions & 2 deletions js/jsmodules.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"go.k6.io/k6/js/modules/k6/experimental/csv"
"go.k6.io/k6/js/modules/k6/experimental/fs"
"go.k6.io/k6/js/modules/k6/experimental/streams"
expws "go.k6.io/k6/js/modules/k6/experimental/websockets"
"go.k6.io/k6/js/modules/k6/grpc"
"go.k6.io/k6/js/modules/k6/html"
"go.k6.io/k6/js/modules/k6/http"
Expand All @@ -26,7 +27,6 @@ import (

"github.com/grafana/xk6-redis/redis"
"github.com/grafana/xk6-webcrypto/webcrypto"
expws "github.com/grafana/xk6-websockets/websockets"
)

func getInternalJSModules() map[string]interface{} {
Expand All @@ -42,7 +42,7 @@ func getInternalJSModules() map[string]interface{} {
"k6/experimental/redis": redis.New(),
"k6/experimental/streams": streams.New(),
"k6/experimental/webcrypto": webcrypto.New(),
"k6/experimental/websockets": &expws.RootModule{},
"k6/experimental/websockets": expws.New(),
"k6/experimental/timers": newRemovedModule(
"k6/experimental/timers has been graduated, please use k6/timers instead."),
"k6/experimental/tracing": newRemovedModule(
Expand Down
25 changes: 25 additions & 0 deletions js/modules/k6/experimental/websockets/autobahn_tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Autobahn test suite

Docs: https://github.com/crossbario/autobahn-testsuite

## Usage

Run the WebSocket server.

```sh
$ docker run -it --rm \
-v ${PWD}/config:/config \
-v ${PWD}/reports:/reports \
-p 9001:9001 \
-p 8080:8080 \
--name fuzzingserver \
crossbario/autobahn-testsuite
```

Run the autobahn client test with k6.

```sh
./k6 run ./script.js
```

Open the browser to `http://localhost:8080` for checking the report.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"url": "ws://127.0.0.1:9001",
"outdir": "./reports/clients",
"cases": ["*"],
"exclude-cases": [
"9.*",
"12.*",
"13.*"
],
"exclude-agent-cases": {}
}
Empty file.
46 changes: 46 additions & 0 deletions js/modules/k6/experimental/websockets/autobahn_tests/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { WebSocket } from "k6/x/websockets"
import { sleep, check } from "k6"
import exec from 'k6/execution'

export let options = {
iterations: 247, // get this value from the Autobahn server
vus: 3,
}

const base = `ws://127.0.0.1:9001`
const agent = "k6v383"

export default function() {
let testCase = exec.scenario.iterationInTest+1
let url = `${base}/runCase?case=${testCase}&agent=${agent}`;
let ws = new WebSocket(url);

ws.addEventListener("open", () => {
console.log(`Testing case #${testCase}`)
});

ws.addEventListener("message", (e) => {
if (e.event === 'ERROR') {
console.log(`VU ${__VU}: test: #${testCase} error:`, e.data, `and message:`, e.message)
return
}
ws.send(e.data)
})

ws.addEventListener("error", (e) => {
console.error(`test: #${testCase} error:`, e)
ws.close()
})
}

export function teardown() {
let ws = new WebSocket(`${base}/updateReports?agent=${agent}`)
ws.addEventListener("open", (e) => {
console.log("Updating the report")
});

ws.addEventListener("error", (e) => {
console.error("Updating the report failed:", e)
ws.close()
});
}
Loading
Loading