Files
mtusi2026lab3/student-tests/research-compose.ts
T

105 lines
8.9 KiB
TypeScript

import {resolve} from 'node:path';
import {mkdir,mkdtemp,writeFile} from 'node:fs/promises';
const root=resolve(import.meta.dir,'..');
if(process.argv.includes('--check')){console.log('Research script parsed without accessing the stand');process.exit(0);}
const nonce=crypto.randomUUID().slice(0,8),project='vp-lab3-research-'+nonce;
const parent=resolve(root,'evidence/research');await mkdir(parent,{recursive:true});
const evidence=await mkdtemp(resolve(parent,'run-'));
await writeFile(resolve(evidence,'run.json'),JSON.stringify({run_id:nonce,started_at:new Date().toISOString(),state:'started',project})+'\n',{flag:'wx'});
let stage='preflight',created=false,primary:unknown;
let rolling:Promise<{ok:boolean}>|undefined;let rollingFinished=false;
const cleanupErrors:string[]=[];
let configuration:Record<string,string>={PUBLIC_PORT:'28303',BASE_URL:'http://127.0.0.1:28303',SESSION_TTL_SECONDS:'3600'};
const base='http://127.0.0.1:28303';
async function command(args:string[],input?:string){
const p=Bun.spawn(args,{cwd:root,env:{...process.env,...configuration},stdin:input===undefined?'ignore':'pipe',stdout:'pipe',stderr:'pipe',signal:AbortSignal.timeout(120000)});
if(input!==undefined){p.stdin.write(input);p.stdin.end();}
const [out,err]=await Promise.all([new Response(p.stdout).text(),new Response(p.stderr).text()]);
if(await p.exited!==0)throw new Error(stage+': command failed ('+args[0]+')');
return out;
}
async function guard(){const p=Bun.spawn(['/home/rey/.local/bin/game-running'],{stdout:'ignore',stderr:'pipe'});await new Response(p.stderr).text();if(await p.exited!==1)throw new Error('Research requires an inactive game');}
function compose(args:string[],input?:string){return command(['docker','compose','-f',resolve(root,'compose.yaml'),'-p',project,...args],input);}
async function sql(query:string){return compose(['exec','-T','postgres','psql','-X','-qAt','-U','messenger','-d','messenger','-v','ON_ERROR_STOP=1'],query);}
const samples:any[]=[];
const poolOnly=process.argv.includes('--pool-only');
let rollingMetric:unknown;
try{
await guard();
if((await command(['ss','-H','-ltn','sport = :28303'])).trim())throw new Error('Research port is occupied');
stage='create_isolated_stand';created=true;
await compose(['up','-d','--wait','postgres','valkey']);
await compose(['run','--rm','migrate']);
const hardware=JSON.parse(await command(['lscpu','-J']));
await writeFile(resolve(evidence,'hardware.json'),JSON.stringify(hardware.lscpu.filter((x:any)=>/Model name|CPU\(s\)|Thread|Core\(s\)/.test(x.field)),null,2)+'\n');
const cases:any[]=[];
if(!poolOnly)for(const phase of ['fixed','growing'])for(const replicas of [1,2,4])for(const repeat of [1,2])cases.push({phase,replicas,repeat,pool:8});
// Pool experiment keeps the single-replica CPU/RAM budget identical.
for(const [index,pool] of [8,1,1,8].entries())cases.push({phase:'pool',replicas:1,repeat:index+1,pool});
for(const test of cases){
await guard();stage=`configure_${test.phase}_${test.replicas}_${test.repeat}_${test.pool}`;
await compose(['stop','api']);
await sql('TRUNCATE idempotency,messages,chat_members,chats,sessions,users;');
const fixed=test.phase!=='growing';
const memory=fixed?2048/test.replicas:512;
configuration={...configuration,API_REPLICAS:String(test.replicas),API_CPUS:String(fixed?2/test.replicas:1),API_MEMORY:memory+'m',GO_MEMORY_LIMIT:Math.floor(memory*.75)+'MiB',PG_MAX_CONNS:String(test.pool)};
await compose(['up','-d','--no-build','--scale','api='+test.replicas,'api','lb']);
await command([process.execPath,'--no-env-file','student-tests/wait.ts'].map(String));
const actual=await (await fetch(base+'/debug/stats')).json() as any;
if(actual.store?.pool_max!==test.pool)throw new Error('Running image did not apply the requested pool size');
const replicaIDs=(await compose(['ps','-q','api'])).trim().split(/\s+/);
if(replicaIDs.length!==test.replicas)throw new Error('Actual replica count differs');
for(const id of replicaIDs){const [cpuBytes,memoryBytes]=(await command(['docker','inspect','--format','{{.HostConfig.NanoCpus}} {{.HostConfig.Memory}}',id])).trim().split(/\s+/).map(Number);if(cpuBytes!==Number(configuration.API_CPUS)*1e9||memoryBytes!==memory*1024*1024)throw new Error('Actual API resource limits differ');}
// The wait helper needs this stand's endpoint explicitly.
stage='measure_'+test.phase;
const name=[test.phase,test.replicas,test.pool,test.repeat].join('-');
const output=resolve(evidence,name+'.json');
const ids=(await compose(['ps','-q','api','lb','postgres','valkey'])).trim().split(/\s+/);
const load=command(['python3','tests/load.py','--base-url',base,'--duration','5','--concurrency','16','--output',output]);
const snapshot=(async()=>{await Bun.sleep(2000);const text=await command(['docker','stats','--no-stream','--format','{{json .}}',...ids]);return text.trim().split('\n').filter(Boolean).map(line=>{const x=JSON.parse(line);return {name:x.Name,cpu_percent:x.CPUPerc,memory:x.MemUsage};});})();
const waits=(async()=>{await Bun.sleep(1000);const out=[];for(let i=0;i<10;i++){out.push(JSON.parse((await sql("SELECT coalesce(json_agg(t),'[]'::json) FROM (SELECT wait_event_type,wait_event,count(*) FROM pg_stat_activity WHERE datname=current_database() AND state='active' AND pid<>pg_backend_pid() GROUP BY wait_event_type,wait_event) t; ")).trim()));await Bun.sleep(200);}return out;})();
const measured=await Promise.allSettled([load,snapshot,waits]);
for(const result of measured)if(result.status==='rejected')throw result.reason;
const stats=(measured[1] as PromiseFulfilledResult<any>).value;
const postgresWaits=(measured[2] as PromiseFulfilledResult<any>).value;
await guard();
const metric=await Bun.file(output).json();
const count=Number((await sql('SELECT count(*) FROM messages;')).trim());
samples.push({...test,api_cpu_each:Number(configuration.API_CPUS),api_memory_mb_each:memory,metrics:metric,snapshot:stats,postgres_wait_samples:postgresWaits,messages:count});
await writeFile(resolve(evidence,'summary.json'),JSON.stringify({valid:true,complete:false,expected_samples:cases.length,profile:'closed-loop POST text, concurrency16,5seconds,2repetitions; fresh DB data for each run',samples},null,2)+'\n');
}
if(!poolOnly){
stage='rolling_update';
await compose(['stop','api']);await sql('TRUNCATE idempotency,messages,chat_members,chats,sessions,users;');
configuration={...configuration,API_REPLICAS:'2',API_CPUS:'1',API_MEMORY:'512m',GO_MEMORY_LIMIT:'384MiB',PG_MAX_CONNS:'8'};
await compose(['up','-d','--no-build','--scale','api=2','api','lb']);
await command([process.execPath,'--no-env-file','student-tests/wait.ts']);
const ids=(await compose(['ps','-q','api'])).trim().split(/\s+/);
rolling=command(['python3','tests/load.py','--base-url',base,'--duration','30','--concurrency','8','--output',resolve(evidence,'rolling.json')]).then(()=>({ok:true}),()=>({ok:false})).finally(()=>{rollingFinished=true;});
for(let attempt=0;attempt<100;attempt++){if(Number((await sql('SELECT count(*) FROM messages;')).trim())>10)break;await Bun.sleep(100);if(attempt===99)throw new Error('Rolling workload did not start');}
const replacements=[];
for(const id of ids){
if(rollingFinished)throw new Error('Workload ended before both replacements');
const before=Number((await sql('SELECT count(*) FROM messages;')).trim());
if((await command(['docker','inspect','--format','{{index .Config.Labels "com.docker.compose.project"}}',id])).trim()!==project)throw new Error('Replica ownership guard failed');
await command(['docker','stop','--time','8',id]);
await command(['docker','container','rm',id]);
await compose(['up','-d','--no-build','--no-deps','--scale','api=2','api']);
await command([process.execPath,'--no-env-file','student-tests/wait.ts']);
const after=Number((await sql('SELECT count(*) FROM messages;')).trim());
if(rollingFinished||after<=before)throw new Error('Replacement was not covered by successful writes');
replacements.push({old_container:id.slice(0,12),messages_before:before,messages_after:after});
}
if(!(await rolling).ok)throw new Error('Rolling load failed');await guard();
await writeFile(resolve(evidence,'replacements.json'),JSON.stringify(replacements,null,2)+'\n');
rollingMetric=await Bun.file(resolve(evidence,'rolling.json')).json();
}
}catch(error){primary=error;}
finally{
if(rolling)await rolling;
if(created)try{await compose(['down','--volumes']);}catch{cleanupErrors.push('isolated_stand');}
}
if(primary||cleanupErrors.length){await writeFile(resolve(evidence,'failure.json'),JSON.stringify({run_id:nonce,stage,primary:primary instanceof Error?primary.name:primary?'unknown':null,cleanup_errors:cleanupErrors})+'\n');throw new Error('Research failed; inspect this run failure summary');}
await writeFile(resolve(evidence,'summary.json'),JSON.stringify({run_id:nonce,valid:true,complete:true,expected_samples:poolOnly?4:16,profile:'closed-loop POST text, concurrency16,5seconds,2repetitions; fresh DB data for each run',samples,rolling:rollingMetric},null,2)+'\n');
console.log('PASS lab3 research: '+evidence);