okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
656478c68
main
1/** "Answer speed" is one number everywhere: every token the model produced over its decode time, never over a time that holds the prompt being read. */ 2import { fileURLToPath } from 'node:url' ; 3import test , { mock } from 'node:test' ; 4import assert from 'node:assert/strict' ; 5import { build } from 'esbuild' ; 6import { sumEngineRequests , turnEngineSpeed } from '../server/agent/engine-speed.mjs' ; 7import { TurnProjector } from '../server/agent/events.mjs' ; 8import { answerSpeed } from '../server/model-providers.mjs' ; 9import * as monitor from '../server/monitor.mjs' ; 10import * as serving from '../server/serving.mjs' ; 11 12async function moduleAt ( path ) { const result = await build ({ entryPoints :[ fileURLToPath ( new URL ( path , import . meta. url ))], bundle :true , write :false , platform :'node' , format :'esm' }); return import ( `data:text/javascript;base64, ${ Buffer . from ( result . outputFiles [ 0 ]. text ). toString ( 'base64' )} ` ); } 13const { chatMetrics} = await moduleAt ( '../src/lib/chat-metrics.ts' ); 14 15// Two real requests from the owner PC (Gyro-S with MTP drafting, RTX 5070), as the engine's history lists them. 16const REAL = [ 17{ time :1000.0 , prompt_tokens :62 , reused :0 , prompt_ms :941 , decode_ms :711 , engine_generated :34 , output_tokens :34 , drafts_offered :34 , drafts_accepted :18 }, 18{ time :1010.0 , prompt_tokens :308 , reused :0 , prompt_ms :1212 , decode_ms :1108 , engine_generated :73 , output_tokens :73 , drafts_offered :68 , drafts_accepted :45 }, 19]; 20 21test ( 'the engine speed of a turn: produced tokens over decode time, prompt reading kept apart' , () => { 22const one = sumEngineRequests ( REAL , { since :1009_000 }); 23assert . equal ( one . tokens , 73 ); 24assert . equal ( one . tps , 65.9 ); 25assert . equal ( one . promptTps , 254.1 ); 26const both = sumEngineRequests ( REAL , { since :999_000 }); 27assert . equal ( both . tokens , 107 ); 28assert . equal ( both . tps , Number (( 107 / 1.819 ). toFixed ( 1 ))); // 58.8: the sum over the sum, not the mean of the two 29// dividing by the whole time (reading + decoding) is the number users saw as "lower than before" 30assert . ok ( 73 / (( 1212 + 1108 ) / 1000 ) < 32 ); 31}); 32 33test ( 'drafted tokens are not output: the speed uses the engine count, whatever was offered' , () => { 34const r = sumEngineRequests ([{ ...REAL [ 1 ], drafts_offered :9999 }], { since :1009_000 }); 35assert . equal ( r . tps , 65.9 ); 36}); 37 38test ( "another chat's requests on the same engine are not this turn's" , () => { 39const stranger = { time :1011.0 , prompt_tokens :10 , reused :0 , prompt_ms :100 , decode_ms :1000 , engine_generated :10 }; 40const r = sumEngineRequests ([ REAL [ 1 ], stranger ], { since :1009_000 , attempts :1 }); 41assert . equal ( r . tokens , 10 ); // the newest one only: the turn made one model request 42assert . equal ( sumEngineRequests ([], { since :0 }), null ); 43assert . equal ( sumEngineRequests ([{ time :5 , engine_generated :5 , decode_ms :0 }], { since :0 }), null ); 44}); 45 46test ( 'reading the engine: its own /metrics with the key, null when it fails' , async () => { 47let seen ; 48const fetcher = async ( url , init ) => { seen = { url, auth :init . headers . authorization }; return { ok :true , json :async () => ({ requests :REAL }) }; }; 49const r = await turnEngineSpeed ({ baseUrl :'http://127.0.0.1:8080/v1' , key :'k' , since :1009_000 , fetcher}); 50assert . equal ( r . tps , 65.9 ); 51assert . equal ( seen . url , 'http://127.0.0.1:8080/metrics?requests=all' ); 52assert . equal ( seen . auth , 'Bearer k' ); 53assert . equal ( await turnEngineSpeed ({ baseUrl :'http://127.0.0.1:8080/v1' , since :0 , fetcher :async () => { throw new Error ( 'down' ); } }), null ); 54assert . equal ( await turnEngineSpeed ({ baseUrl :'http://127.0.0.1:8080/v1' , since :0 , fetcher :async () => ({ ok :false }) }), null ); 55}); 56 57const chunk = ( attemptId , c ) => ({ type :'assistant-stream' , frame :{ type :'chunk' , attemptId, chunk :c } }); 58 59test ( 'the finished agent turn shows the engine speed and the prompt reading speed' , () => { 60const p = new TurnProjector ({ startedAt :Date . now () }); 61p . consume ( chunk ( 'a1' , { type :'text-delta' , text :'Done.' })); 62p . consume ( chunk ( 'a1' , { type :'usage' , usage :{ inputTokens :308 , outputTokens :73 } })); 63p . ended = { kind :'completed' }; 64const done = p . finish ({ model :'m' , contextMax :32768 , engine :{ tps :65.9 , promptTps :254.1 } }). find ( f => f . type === 'done' ); 65assert . equal ( done . tps , 65.9 ); 66assert . equal ( done . promptTps , 254.1 ); 67assert . equal ( done . tokensOut , 73 ); 68}); 69 70test ( 'a short reply still gets a speed (it used to come out empty under 0.3 s of streaming)' , () => { 71mock . timers . enable ({ apis :[ 'Date' ], now :3_000_000 }); 72try { 73const p = new TurnProjector ({ startedAt :Date . now () }); 74mock . timers . tick ( 2000 ); // reading the prompt 75p . consume ( chunk ( 'a1' , { type :'text-delta' , text :'Hi!' })); 76p . consume ( chunk ( 'a1' , { type :'usage' , usage :{ inputTokens :40 , outputTokens :3 } })); 77mock . timers . tick ( 100 ); 78p . ended = { kind :'completed' }; 79const done = p . finish ({}). find ( f => f . type === 'done' ); 80assert . ok ( done . tps > 0 && done . tps <= 60 , ` ${ done . tps } ` ); 81} finally { mock . timers . reset (); } 82}); 83 84test ( 'a model API reply is timed from its first token, not from the request' , () => { 85assert . equal ( answerSpeed ( 100 , 12_000 , 2_000 ), 10 ); // 100 tokens in the 10 s after the first token 86assert . ok ( 100 / 12 < answerSpeed ( 100 , 12_000 , 2_000 )); // the whole-wait number is the lower one 87assert . equal ( answerSpeed ( 50 , 1000 , null ), 50 ); // no first-token time: the whole time is all there is 88assert . equal ( answerSpeed ( 50 , 1000 , 900 ), 50 ); // under a quarter second left: not used 89assert . equal ( answerSpeed ( null , 1000 , 0 ), null ); 90assert . equal ( answerSpeed ( 0 , 1000 , 0 ), null ); 91}); 92 93test ( 'the monitor and the request list average answer speeds, not tokens over the whole request' , () => { 94monitor . record ({ tokensIn :308 , tokensOut :73 , durationMs :2320 , tps :65.9 }); 95monitor . record ({ tokensIn :62 , tokensOut :34 , durationMs :1652 , tps :47.8 }); 96assert . equal ( monitor . snapshot (). totals . avgTps , 56.9 ); 97monitor . record ({ tokensIn :1 , tokensOut :5 , durationMs :100 }); // no decode figure: left out, not divided by the wall time 98assert . equal ( monitor . snapshot (). totals . avgTps , 56.9 ); 99const a = serving . open ({ client :'t' , endpoint :'/x' , model :'m' }); 100serving . mark ( a . id , { tps :65.9 }); 101serving . finish ( a . id , { tokensOut :73 , durationMs :2320 }); 102assert . equal ( serving . rateSample (), 65.9 ); 103}); 104 105const reply = ( o ) => ({ role :'assistant' , at :new Date (). toISOString (), content :'x' , ...o }); 106const chat = ( messages ) => ({ id :'s' , messages, tokensIn :0 , tokensOut :0 , statsAvailable :true }); 107 108test ( 'the footer shows the last reply own speed, and the prompt reading speed next to it' , () => { 109const s = chat ([ reply ({ tps :61 , inputTokens :10 , tokens :5 }), reply ({ tps :66 , promptTps :254 , inputTokens :20 , tokens :7 })]); 110const m = chatMetrics ( s , null , 32768 ); 111assert . equal ( m . speed , 66 ); 112assert . equal ( m . promptSpeed , 254 ); 113// an older reply's number is never shown under "last reply" 114const m2 = chatMetrics ( chat ([ reply ({ tps :61 , inputTokens :10 , tokens :5 }), reply ({ inputTokens :20 , tokens :7 })]), null , 32768 ); 115assert . equal ( m2 . speed , null ); 116}); 117 118test ( 'while it thinks the footer reads the engine like the monitor does' , () => { 119const s = chat ([ reply ({ streaming :true , tps :12 })]); 120const monitorState = { phase :'thinking' , outputTps :64.2 , context :{ sessionId :'s' , usedTokens :100 , maxTokens :32768 } }; 121assert . equal ( chatMetrics ( s , monitorState , 32768 ). speed , 64.2 ); 122assert . equal ( chatMetrics ( s , { ...monitorState , outputTps :0 }, 32768 ). speed , 12 ); // no reading yet: the stream's own estimate 123});