forked from mtusi2026/mtusi2026lab3
Add API integration checks, migration compatibility, rolling restart scenarios and a report verification collector.
65 lines
2.5 KiB
JavaScript
65 lines
2.5 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { spawn } from 'node:child_process';
|
|
import { createServer } from 'node:http';
|
|
import test from 'node:test';
|
|
|
|
test('scaling benchmark schedules reads and writes and reports distinct instances', async () => {
|
|
let sequence = 0;
|
|
const server = createServer(async (request, response) => {
|
|
for await (const _ of request) {
|
|
// Consume the request body before responding.
|
|
}
|
|
response.setHeader('Content-Type', 'application/json');
|
|
response.setHeader('X-Instance-Id', 'mock1');
|
|
let status = 200;
|
|
let body = {};
|
|
if (request.url === '/api/v1/users' && request.method === 'POST') {
|
|
status = 201;
|
|
body = { id: `user-${++sequence}` };
|
|
} else if (request.url === '/api/v1/auth/sessions') {
|
|
status = 201;
|
|
body = { token: 'synthetic-token' };
|
|
} else if (request.url === '/api/v1/chats' && request.method === 'POST') {
|
|
status = 201;
|
|
body = { id: 'chat-1' };
|
|
} else if (request.url.startsWith('/api/v1/chats/chat-1/messages')) {
|
|
if (request.method === 'POST') {
|
|
status = 201;
|
|
body = { id: `message-${++sequence}` };
|
|
} else {
|
|
body = { items: [] };
|
|
}
|
|
} else {
|
|
status = 404;
|
|
}
|
|
response.writeHead(status);
|
|
response.end(JSON.stringify(body));
|
|
});
|
|
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
|
|
try {
|
|
const port = server.address().port;
|
|
const args = [
|
|
'scripts/benchmark-scaling.mjs', '--base-url', `http://127.0.0.1:${port}`,
|
|
'--label', 'mock', '--rate', '20', '--duration-seconds', '2',
|
|
'--warmup-seconds', '1', '--seed-messages', '2',
|
|
'--read-percent', '50', '--expected-instances', '1',
|
|
];
|
|
const child = spawn(process.execPath, args, { cwd: process.cwd() });
|
|
let stdout = '';
|
|
let stderr = '';
|
|
child.stdout.on('data', (chunk) => { stdout += chunk; });
|
|
child.stderr.on('data', (chunk) => { stderr += chunk; });
|
|
const code = await new Promise((resolve) => child.on('close', resolve));
|
|
assert.equal(code, 0, stderr);
|
|
const result = JSON.parse(stdout);
|
|
assert.equal(result.measurement.scheduled_slots, 40);
|
|
assert.equal(result.measurement.client_dropped_slots, 0);
|
|
assert.equal(result.measurement.all.successful, 40);
|
|
assert.equal(result.measurement.all.instance_counts.mock1, 40);
|
|
assert.ok(result.measurement.read.successful > 0);
|
|
assert.ok(result.measurement.write.successful > 0);
|
|
} finally {
|
|
await new Promise((resolve) => server.close(resolve));
|
|
}
|
|
});
|