forked from mtusi2026/mtusi2026lab4
44 lines
2.3 KiB
JavaScript
44 lines
2.3 KiB
JavaScript
import http from 'k6/http';
|
|
import encoding from 'k6/encoding';
|
|
import {check, sleep} from 'k6';
|
|
import {Counter, Trend} from 'k6/metrics';
|
|
|
|
const base = __ENV.BASE_URL || 'http://127.0.0.1:18084';
|
|
const original = open('../../tests/fixtures/sample.png', 'b');
|
|
const uploaded = new Counter('attachments_ready');
|
|
const rejected = new Counter('attachments_rejected');
|
|
const latency = new Trend('attachment_upload_ms', true);
|
|
export const options = {vus: 10, iterations: 100, setupTimeout: '60s', thresholds: {checks: ['rate==1'], attachments_ready: ['count>0']}};
|
|
|
|
export function setup() {
|
|
const actors = [];
|
|
for (let i = 0; i < 10; i++) {
|
|
const username = `a_${Date.now()}_${i}`;
|
|
const password = 'synthetic-attachment-password';
|
|
const user = http.post(`${base}/api/v1/users`, JSON.stringify({username, display_name: 'Synthetic', password}), {headers: {'Content-Type': 'application/json'}});
|
|
if (user.status !== 201) throw new Error(`registration: ${user.status}`);
|
|
const session = http.post(`${base}/api/v1/auth/sessions`, null, {headers: {Authorization: `Basic ${encoding.b64encode(username + ':' + password)}`}});
|
|
if (session.status !== 201) throw new Error(`login: ${session.status}`);
|
|
const headers = {Authorization: `Bearer ${session.json('token')}`};
|
|
const chat = http.post(`${base}/api/v1/chats`, JSON.stringify({title: 'Attachment experiment', member_ids: [user.json('id')]}), {headers: {...headers, 'Content-Type': 'application/json'}});
|
|
if (chat.status !== 201) throw new Error(`chat: ${chat.status}`);
|
|
actors.push({headers, chat: chat.json('id')});
|
|
}
|
|
return actors;
|
|
}
|
|
|
|
export default function (actors) {
|
|
const actor = actors[(__VU - 1) % actors.length];
|
|
const response = http.post(`${base}/api/v1/chats/${actor.chat}/attachments?filename=sample.png`, original, {headers: {...actor.headers, 'Content-Type': 'image/png'}});
|
|
latency.add(response.timings.duration);
|
|
if (response.status === 201) {
|
|
uploaded.add(1);
|
|
const read = http.get(`${base}/api/v1/attachments/${response.json('id')}/content`, {headers: actor.headers, responseType: 'binary'});
|
|
check(read, {'download complete': r => r.status === 200 && r.body.byteLength === original.byteLength});
|
|
} else {
|
|
rejected.add(1);
|
|
check(response, {'bounded overload': r => r.status === 429 || r.status === 503});
|
|
}
|
|
sleep(0.05);
|
|
}
|