okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8820fa433
main
1import test from 'node:test' ; 2import assert from 'node:assert/strict' ; 3import { createServer } from 'node:http' ; 4import { proxyCompletion } from '../server/inference.mjs' ; 5 6/* The speed shown while an answer streams must be the decode speed of everything generated, reasoning included, from the first 7token (the owner saw 4-5 tok/s in chat while the engine log said 48-57): the stand-in engine streams 90 reasoning tokens at about 840 a second, a 4 s "prompt" before the first one, then a short answer. */ 9function engine ({ promptMs, reasoning, answer, perSecond}) { 10return createServer (( req , res ) => { 11res . writeHead ( 200 , { 'content-type' :'text/event-stream' }); 12const send = obj => res . write ( `data: ${ JSON . stringify ( obj )} \n\n` ); 13let sent = 0 ; 14setTimeout ( function tick () { 15if ( sent < reasoning + answer ) { 16send ({ choices :[{ delta :sent < reasoning ?{ reasoning_content :'tok ' } :{ content :'ans ' } }] }); 17sent ++ ; 18return setTimeout ( tick , 1000 / perSecond ); 19} 20send ({ choices :[], usage :{ prompt_tokens :20 , completion_tokens :reasoning + answer } }); 21res . write ( 'data: [DONE]\n\n' ); res . end (); 22}, promptMs ); 23}); 24} 25 26test ( 'live speed counts reasoning tokens over decode time, not the answer over the whole wait' , async () => { 27const server = engine ({ promptMs :4000 , reasoning :90 , answer :10 , perSecond :40 }); 28await new Promise ( r => server . listen ( 0 , '127.0.0.1' , r )); 29const frames = []; 30try { 31const result = await proxyCompletion ({ baseUrl :`http://127.0.0.1: ${ server . address (). port } /v1` , message :'hi' , onFrame :f => frames . push ( f ), engine :{ beginReading () {}, beginThinking () {}, beginGenerating () {} } }); 32const stats = frames . filter ( f => f . type === 'stats' ); 33assert . ok ( stats . length >= 2 , 'live frames while it streams' ); 34const last = stats . at ( - 1 ); 35assert . ok ( last . tps > 22 && last . tps < 80 , `live tok/s ${ last . tps } is the decode rate, not tokens over the wait including the prompt (~14)` ); 36assert . ok ( last . tokensOut >= 50 , 'reasoning tokens are counted' ); 37assert . equal ( result . tokensOut , 100 ); 38assert . ok ( result . tps > 22 && result . tps < 80 , `final tok/s ${ result . tps } ` ); 39} finally { server . close (); } 40});