|
| 1 | +// cSpell:ignore loadimpact |
| 2 | + |
| 3 | +import { Counter, Trend } from 'k6/metrics'; |
| 4 | +import { check } from 'k6'; |
| 5 | +import ws from 'k6/ws'; |
| 6 | + |
| 7 | +// eslint-disable-next-line no-undef |
| 8 | +const { TARGET_ENV, TARGET_NET, URL, WALLETS } = Object.assign({ WALLETS: '10000' }, __ENV); |
| 9 | +const url = TARGET_ENV ? `wss://${TARGET_ENV}-${TARGET_NET}${TARGET_ENV === 'ops' ? '-1' : ''}.lw.iog.io/ws` : URL; |
| 10 | + |
| 11 | +if (TARGET_ENV && !['dev', 'ops', 'staging', 'prod'].includes(TARGET_ENV)) |
| 12 | + throw new Error(`Not valid TARGET_ENV: ${TARGET_ENV}`); |
| 13 | +if (TARGET_NET && !['preview', 'preprod', 'sanchonet', 'mainnet'].includes(TARGET_NET)) |
| 14 | + throw new Error(`Not valid TARGET_NET: ${TARGET_NET}`); |
| 15 | +if (!(TARGET_ENV && TARGET_NET) && !URL) throw new Error('Please specify both TARGET_ENV and TARGET_NET or URL'); |
| 16 | + |
| 17 | +export const options = { |
| 18 | + ext: { |
| 19 | + loadimpact: { |
| 20 | + apm: [], |
| 21 | + distribution: { 'amazon:us:portland': { loadZone: 'amazon:us:portland', percent: 100 } } |
| 22 | + } |
| 23 | + }, |
| 24 | + scenarios: { |
| 25 | + connections: { |
| 26 | + executor: 'ramping-vus', |
| 27 | + gracefulRampDown: '0s', |
| 28 | + gracefulStop: '120s', |
| 29 | + stages: [{ duration: '3s', target: Number.parseInt(WALLETS, 10) }], |
| 30 | + startVUs: 10 |
| 31 | + } |
| 32 | + } |
| 33 | +}; |
| 34 | + |
| 35 | +const operationalTrend = new Trend('_operational', true); |
| 36 | +const unexpectedCloseCounter = new Counter('_unexpected_close'); |
| 37 | + |
| 38 | +export const run = () => { |
| 39 | + const begin = Date.now(); |
| 40 | + |
| 41 | + const res = ws.connect(url, null, (socket) => { |
| 42 | + let closed = false; |
| 43 | + let firstMessage = true; |
| 44 | + |
| 45 | + socket.on('message', () => { |
| 46 | + if (firstMessage) { |
| 47 | + operationalTrend.add(Date.now() - begin); |
| 48 | + firstMessage = false; |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + // Count unexpected close |
| 53 | + socket.on('close', () => { |
| 54 | + if (!closed) unexpectedCloseCounter.add(1); |
| 55 | + }); |
| 56 | + |
| 57 | + // Heartbeat |
| 58 | + socket.setTimeout(() => socket.send('{}'), 30 * 1000); |
| 59 | + |
| 60 | + // End the test after 80" |
| 61 | + socket.setTimeout(() => { |
| 62 | + closed = true; |
| 63 | + socket.close(); |
| 64 | + }, 80 * 1000); |
| 65 | + }); |
| 66 | + |
| 67 | + check(res, { 'status is 101': (r) => r && r.status === 101 }); |
| 68 | +}; |
| 69 | + |
| 70 | +export default run; |
0 commit comments