gpu-head-to-head
aien-dev/gpu-head-to-head
Brought by drakestapleton
gpu-head-to-head / bench / test-bench-node.mjs
| 1 | // Verification-only synthetic HTTP controls. No model or GPU required. |
| 2 | import test from 'node:test'; |
| 3 | import assert from 'node:assert/strict'; |
| 4 | import http from 'node:http'; |
| 5 | import fs from 'node:fs'; |
| 6 | import os from 'node:os'; |
| 7 | import path from 'node:path'; |
| 8 | import {spawn} from 'node:child_process'; |
| 9 | import {fileURLToPath} from 'node:url'; |
| 10 | import {oneRequest} from './bench-node.mjs'; |
| 11 | |
| 12 | const event=value=>'data: '+JSON.stringify(value)+'\r\n\r\n'; |
| 13 | const content=text=>event({choices:[{delta:{content:text},finish_reason:null}]}); |
| 14 | const finish=event({choices:[{delta:{},finish_reason:'length'}],usage:{prompt_tokens:5,completion_tokens:2}}); |
| 15 | const body=content('héllo')+finish+'data: [DONE]\r\n\r\n'; |
| 16 | async function server(handler,run) { |
| 17 | const s=http.createServer((req,res)=>{ |
| 18 | let input='';req.on('data',chunk=>input+=chunk); |
| 19 | req.on('end',()=>Promise.resolve(handler(req,res,JSON.parse(input))).catch(e=>res.destroy(e))); |
| 20 | }); |
| 21 | await new Promise(resolve=>s.listen(0,'127.0.0.1',resolve)); |
| 22 | try { return await run(s.address().port); } |
| 23 | finally { s.closeAllConnections(); await new Promise(resolve=>s.close(resolve)); } |
| 24 | } |
| 25 | const call=(port,extra={})=>oneRequest({port,model:'fixture',prompt:'public fixture',maxTokens:2,...extra}); |
| 26 | |
| 27 | test('request contract, fragmented UTF-8/SSE, reasoning, usage and raw clocks',async()=>{ |
| 28 | await server((req,res,b)=>{ |
| 29 | assert.equal(req.url,'/v1/chat/completions');assert.equal(req.method,'POST'); |
| 30 | assert.deepEqual(b,{model:'fixture',messages:[{role:'user',content:'public fixture'}],stream:true, |
| 31 | stream_options:{include_usage:true},temperature:0,top_p:1,max_tokens:2,seed:0, |
| 32 | chat_template_kwargs:{enable_thinking:false}}); |
| 33 | res.writeHead(200,{'Content-Type':'text/event-stream'}); |
| 34 | const bytes=Buffer.from(': ignored\r\n'+event({choices:[{delta:{reasoning_content:'réason '}}]})+body); |
| 35 | for(const byte of bytes) res.write(Buffer.from([byte])); |
| 36 | res.end(); |
| 37 | },async port=>{ |
| 38 | const row=await call(port);assert.equal(row.error,null);assert.equal(row.text,'réason héllo'); |
| 39 | assert.equal(row.chunks,2);assert.equal(row.finish_reason,'length'); |
| 40 | assert.deepEqual(row.usage,{prompt_tokens:5,completion_tokens:2}); |
| 41 | for(const key of ['t_send_ns','t_first_ns','t_last_ns']) assert.match(row[key],/^\d+$/); |
| 42 | }); |
| 43 | }); |
| 44 | test('busy retries retain one request start and eventually succeed',async()=>{ |
| 45 | let requests=0; |
| 46 | await server((req,res)=>{requests++;if(requests===1){res.writeHead(503);res.end('busy');} |
| 47 | else{res.writeHead(200);res.end(body);}},async port=>{ |
| 48 | const row=await call(port);assert.equal(row.error,null);assert.equal(row.busy_retries,1); |
| 49 | assert.equal(row.http_status,200);assert.equal(requests,2); |
| 50 | }); |
| 51 | }); |
| 52 | for(const [name,payload,message] of [ |
| 53 | ['malformed','data: {bad}\n\n','malformed SSE JSON'], |
| 54 | ['truncated',content('partial')+finish,'stream ended without DONE'], |
| 55 | ['missing usage',content('x')+event({choices:[{finish_reason:'stop'}]})+'data: [DONE]\n','missing token usage'], |
| 56 | ['no content',finish+'data: [DONE]\n','no content received'], |
| 57 | ['server error',event({error:{message:'fixture'}}),'server error event'], |
| 58 | ['bad delta',event({choices:[{delta:{content:{bad:true}}}]}),'invalid text delta'], |
| 59 | ]) test(name+' is recorded as a failed request',async()=>{ |
| 60 | await server((req,res)=>{res.writeHead(200);res.end(payload);},async port=>{ |
| 61 | const row=await call(port);assert.equal(row.error,message); |
| 62 | }); |
| 63 | }); |
| 64 | test('non-success HTTP and overall retry timeout refuse success',async()=>{ |
| 65 | await server((req,res)=>{res.writeHead(400);res.end('fixture');},async port=>{ |
| 66 | assert.equal((await call(port)).error,'HTTP 400'); |
| 67 | }); |
| 68 | await server((req,res)=>{res.writeHead(429);res.end('busy');},async port=>{ |
| 69 | const row=await call(port,{timeoutMs:50});assert(row.error);assert.equal(row.t_first_ns,null); |
| 70 | }); |
| 71 | }); |
| 72 | const client=fileURLToPath(new URL('./bench-node.mjs',import.meta.url)); |
| 73 | const cli=args=>new Promise(resolve=>{ |
| 74 | const child=spawn(process.execPath,[client,...args],{stdio:['ignore','pipe','pipe']}); |
| 75 | let stderr='';child.stderr.on('data',x=>stderr+=x);child.on('close',status=>resolve({status,stderr})); |
| 76 | }); |
| 77 | test('CLI writes all concurrent rows, nonce, warmup flags, and refuses overwrite',async()=>{ |
| 78 | const root=fs.mkdtempSync(path.join(os.tmpdir(),'bench-node-control-')); |
| 79 | try { |
| 80 | const prompts=path.join(root,'prompts.jsonl'),out=path.join(root,'rows.jsonl'); |
| 81 | fs.writeFileSync(prompts,'{"id":"p0","text":"zero"}\n{"id":"p1","text":"one"}\n'); |
| 82 | const seen=[]; |
| 83 | await server((req,res,b)=>{seen.push(b.messages[0].content);res.writeHead(200);res.end(body);},async port=>{ |
| 84 | const args=['--port',String(port),'--model','fixture','--arm','CONTROL','--round','R0', |
| 85 | '--metric','decode','--prompts',prompts,'--max-tokens','2','--concurrency','2', |
| 86 | '--warmup','1','--reps','1','--nonce','--out',out]; |
| 87 | assert.equal((await cli(args)).status,0); |
| 88 | const rows=fs.readFileSync(out,'utf8').trim().split('\n').map(JSON.parse); |
| 89 | assert.equal(rows.length,4);assert.deepEqual(rows.map(r=>r.warmup),[true,true,false,false]); |
| 90 | assert.deepEqual(rows.map(r=>r.prompt_id),['p0','p1','p0','p1']); |
| 91 | assert(rows.every(r=>r.error===null&&r.client==='node-sse-v1'&&r.concurrency===2)); |
| 92 | assert(seen.every(t=>/^Request id [a-f0-9]+\. Ignore this line\.\n/.test(t))); |
| 93 | assert.equal(new Set(seen).size,4); |
| 94 | assert.notEqual((await cli(args)).status,0);assert.equal(seen.length,4); |
| 95 | }); |
| 96 | } finally { fs.rmSync(root,{recursive:true,force:true}); } |
| 97 | }); |