github.com/anycable/anycable-go@v1.5.1/etc/k6/echo.js (about) 1 // Build k6 with xk6-cable like this: 2 // xk6 build v0.38.3 --with github.com/anycable/xk6-cable@v0.3.0 3 4 import { check, sleep, fail } from "k6"; 5 import cable from "k6/x/cable"; 6 import { randomIntBetween } from "https://jslib.k6.io/k6-utils/1.1.0/index.js"; 7 import { Trend } from "k6/metrics"; 8 9 let commandTrend = new Trend("command_duration", true); 10 11 let config = __ENV; 12 13 let url = config.CABLE_URL || "ws://localhost:8080/cable"; 14 let channelName = (config.CHANNEL_ID || 'BenchmarkChannel'); 15 16 export const options = { 17 scenarios: { 18 default: { 19 executor: 'externally-controlled', 20 vus: 30, 21 maxVUs: 500, 22 duration: '5m' 23 } 24 } 25 }; 26 27 export default function () { 28 let cableOptions = { 29 receiveTimeoutMs: 15000, 30 headers: {} 31 }; 32 33 let client 34 35 try { 36 client = cable.connect(url, cableOptions); 37 38 if ( 39 !check(client, { 40 "successful connection": (obj) => obj, 41 }) 42 ) { 43 fail("connection failed"); 44 } 45 } catch (err) { 46 console.error(err) 47 return 48 } 49 50 let channel 51 52 try { 53 channel = client.subscribe(channelName); 54 55 if ( 56 !check(channel, { 57 "successful subscription": (obj) => obj, 58 }) 59 ) { 60 fail("failed to subscribe"); 61 } 62 } catch (err) { 63 console.error(err) 64 return 65 } 66 67 for (let i = 0; i < 10; i++) { 68 let start = Date.now(); 69 channel.perform("echo", { ts: start, content: `hello from ${__VU} numero ${i+1}` }); 70 71 sleep(randomIntBetween(5, 10) / 100); 72 73 let incoming = channel.receiveAll(1); 74 75 for(let message of incoming) { 76 let received = message.__timestamp__ || Date.now(); 77 78 if (message.action == "echo") { 79 let ts = message.ts; 80 commandTrend.add(received - ts); 81 } 82 } 83 84 sleep(randomIntBetween(5, 10) / 100); 85 } 86 87 sleep(randomIntBetween(2, 5)); 88 89 client.disconnect(); 90 }