44 lines
4.3 KiB
Plaintext
44 lines
4.3 KiB
Plaintext
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import assert from 'node:assert/strict';
|
|
import { spawn, execFileSync } from 'node:child_process';
|
|
import { gitEnvironment } from '../../src/core/git-process.mjs';
|
|
import { verifyControlledApi } from '../../scripts/verify-controlled-release.mjs';
|
|
const mode = process.argv[2] ?? 'profile'; assert.ok(['profile','detached'].includes(mode));
|
|
const root = path.resolve('.qa/reliability-1.25.2/compiled-startup-' + (mode === 'profile' ? 'diagnostic' : mode));
|
|
assert.equal(fs.existsSync(root), false); fs.mkdirSync(root);
|
|
const candidate = JSON.parse(fs.readFileSync('releases/latest.json')).portableRoot;
|
|
const environment = gitEnvironment({ AGENT_WORKBENCH_APP_DIR: candidate, AGENT_WORKBENCH_DATA_DIR: path.join(root, 'data'), AGENT_WORKBENCH_PORT: '0' });
|
|
const git = fs.realpathSync.native(execFileSync('where.exe', ['git.exe'], { encoding:'utf8', windowsHide:true }).trim().split(/\r?\n/)[0]);
|
|
for (const name of Object.keys(environment)) if (name.toLowerCase() === 'path') delete environment[name];
|
|
environment.PATH = [path.dirname(git), 'C:/Windows', 'C:/Windows/System32', 'C:/Windows/System32/WindowsPowerShell/v1.0'].join(path.delimiter);
|
|
const outputFd=mode==='detached'?fs.openSync(path.join(root,'live-stdout.txt'),'wx'):null;
|
|
const errorFd=mode==='detached'?fs.openSync(path.join(root,'live-stderr.txt'),'wx'):null;
|
|
const started = Date.now(), child = spawn(path.join(candidate, 'runtime/node.exe'), ['--cpu-prof', '--cpu-prof-dir=' + root, path.join(candidate, 'dist-server/server.mjs')], { cwd:candidate, env:environment, windowsHide:true, detached:mode==='detached', stdio:mode==='detached'?['ignore',outputFd,errorFd]:['ignore','pipe','pipe'] });
|
|
let output='', errors='', url='', bootstrap, result;
|
|
const requests=[];
|
|
const decodeOutput=()=>{for(const line of output.split('\n')){try{const row=JSON.parse(line);if(row.ready)url=row.url;}catch{}}};
|
|
if(mode==='profile'){
|
|
child.stdout.on('data', bytes => { output+=bytes;decodeOutput(); });
|
|
child.stderr.on('data', bytes => { errors+=bytes; });
|
|
}
|
|
const readFiles=()=>{if(mode==='detached'){output=fs.readFileSync(path.join(root,'live-stdout.txt'),'utf8');errors=fs.readFileSync(path.join(root,'live-stderr.txt'),'utf8');decodeOutput();}};
|
|
const outputTimer=mode==='detached'?setInterval(readFiles,50):null;
|
|
const wait = ms => new Promise(resolve => setTimeout(resolve,ms));
|
|
async function until(predicate,limit=15000) { const deadline=Date.now()+limit; while(!predicate()) { assert.ok(Date.now()<deadline, 'Diagnostic host deadline'); await wait(50); } }
|
|
const api=async(route,input)=> { const start=Date.now(); try { const response=await fetch(url+'/api'+route,{method:input===undefined?'GET':'POST', signal:AbortSignal.timeout(30000), headers:{'Content-Type':'application/json','X-Workbench-Token':bootstrap.token,Connection:'close'},...(input===undefined?{}:{body:JSON.stringify(input)})}); const value=await response.json(); assert.ok(response.ok,value.error);return value; } finally { requests.push({route,method:input===undefined?'GET':'POST',elapsedMs:Date.now()-start,at:Date.now()-started}); } };
|
|
try {
|
|
await until(()=>url||child.exitCode!==null); assert.ok(url,errors);
|
|
bootstrap=await(await fetch(url+'/api/bootstrap')).json(); assert.equal((await api('/health')).pid,child.pid);
|
|
console.log('DIAGNOSTIC START '+root);
|
|
result=await verifyControlledApi({api,url,token:bootstrap.token,directory:root,appRoot:candidate,contained:true});
|
|
} catch(error) { result={success:false,error:error.stack}; process.exitCode=1; }
|
|
finally {
|
|
if(url&&bootstrap&&child.exitCode===null) { try {await api('/shutdown',{}); await until(()=>child.exitCode!==null||child.signalCode!==null);} catch(error) { result.shutdownError=error.stack; } }
|
|
assert.ok(child.exitCode!==null||child.signalCode!==null,'Own diagnostic host must exit');
|
|
clearInterval(outputTimer); readFiles();if(outputFd!==null){fs.closeSync(outputFd);fs.closeSync(errorFd);}
|
|
fs.writeFileSync(path.join(root,'stdout.txt'),output,{flag:'wx'});fs.writeFileSync(path.join(root,'stderr.txt'),errors,{flag:'wx'});
|
|
fs.writeFileSync(path.join(root,'result.json'),JSON.stringify({...result,requests,elapsedMs:Date.now()-started,realModelRequests:0},null,2)+'\n',{flag:'wx'});
|
|
console.log(JSON.stringify({success:result.success,error:result.error,elapsedMs:Date.now()-started,root}));
|
|
}
|