Created
July 11, 2025 17:56
-
-
Save ftes/a24ffd276f47c34e188f40b82ac2a25f to your computer and use it in GitHub Desktop.
Load test phoenix liveview form submission with k6
This file contains hidden or 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
import http from "k6/http"; | |
import ws from "k6/ws"; | |
export const options = { | |
// Key configurations for spike in this section | |
stages: [ | |
{ duration: "2m", target: 100 }, // fast ramp-up to a high point | |
// No plateau | |
{ duration: "1m", target: 0 }, // quick ramp-down to 0 users | |
], | |
}; | |
const host = "myliveview.example.com"; | |
export default function () { | |
// Step 1: GET request to retrieve CSRF token | |
const res = http.get(`https://${host}/`); | |
const html = res.html(); | |
const csrfToken = html.find('meta[name="csrf-token"]').attr("content"); | |
const phxMain = html.find("[data-phx-main]"); | |
const phxId = phxMain.attr("id"); | |
const phxSession = phxMain.attr("data-phx-session"); | |
const phxStatic = phxMain.attr("data-phx-static"); | |
// Step 2: Establish WebSocket connection | |
const url = `wss://${host}/live/websocket?_csrf_token=${csrfToken}&vsn=2.0.0`; | |
ws.connect(url, {}, function (socket) { | |
socket.on("open", function open() { | |
console.log("open"); | |
// Join the LiveView | |
socket.send( | |
JSON.stringify([ | |
"4", | |
"4", | |
`lv:${phxId}`, | |
"phx_join", | |
{ | |
url: `https://${host}/`, | |
params: { _csrf_token: csrfToken, _mounts: 0, _mount_attempts: 0 }, | |
session: phxSession, | |
static: phxStatic, | |
}, | |
]), | |
); | |
}); | |
socket.on("message", function (message) { | |
const [a, b, c, type, payload] = JSON.parse(message); | |
if ( | |
a == "4" && | |
b == "4" && | |
type == "phx_reply" && | |
payload.status == "ok" | |
) { | |
console.log("sum"); | |
socket.send( | |
JSON.stringify([ | |
"4", | |
"26", | |
`lv:${phxId}`, | |
"event", | |
{ | |
type: "form", | |
event: "sum_submitted", | |
value: "number=9999999999", | |
}, | |
]), | |
); | |
} | |
}); | |
socket.setTimeout(function () { | |
socket.close(); | |
}, 120_000); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment