okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1fb3e9a32
main
1"""Self-test of tools/par_load.py and par_analyze.py against an in-process fake SSE server.""" 2import json ,os ,sys ,tempfile ,threading ,time ,unittest 3from http .server import BaseHTTPRequestHandler ,ThreadingHTTPServer 4sys .path .insert (0 ,os .path .dirname (__file__ )) 5import par_load ,par_analyze 6 7 8class H (BaseHTTPRequestHandler ): 9def log_message (self ,* a ):pass 10def do_GET (self ): 11self .send_response (200 );self .end_headers ();self .wfile .write (b'{"ok":1}' ) 12def do_POST (self ): 13n = int (self .headers ["Content-Length" ]);b = json .loads (self .rfile .read (n )) 14text = b ["messages" ][0 ]["content" ] 15pt = len (text .split ())+ 10 16if not b ["stream" ]: 17self .send_response (200 );self .end_headers () 18self .wfile .write (json .dumps ({"usage" : {"prompt_tokens" :pt ,"completion_tokens" :1 }}).encode ());return 19self .send_response (200 );self .send_header ("Content-Type" ,"text/event-stream" );self .end_headers () 20k = min (b ["max_tokens" ],12 ) 21for i in range (k ): 22time .sleep (0.01 ) 23self .wfile .write (("data: " + json .dumps ({"choices" : [{"delta" : {"content" :"w " }}]})+ "\n\n" ).encode ());self .wfile .flush () 24# two tokens per chunk at the end would make SSE counts wrong: usage is the truth 25self .wfile .write (("data: " + json .dumps ({"choices" : [{"delta" : {},"finish_reason" :"length" }],"usage" : {"prompt_tokens" :pt ,"completion_tokens" :k + 3 }})+ "\n\n" ).encode ()) 26self .wfile .write (b"data: [DONE]\n\n" );self .wfile .flush () 27 28 29class T (unittest .TestCase ): 30@classmethod 31def setUpClass (cls ): 32cls .s = ThreadingHTTPServer (("127.0.0.1" ,0 ),H ) 33threading .Thread (target = cls .s .serve_forever ,daemon = True ).start () 34cls .url = f"http://127.0.0.1: { cls . s . server_address [ 1 ] } " 35@classmethod 36def tearDownClass (cls ):cls .s .shutdown () 37 38def run_ (self ,* args ): 39out = tempfile .mktemp (suffix = ".json" ) 40rc = par_load .main ([self .url ,"--out" ,out ,* args ]) 41return rc ,json .load (open (out )) 42 43def test_w2_usage_tokens (self ): 44rc ,L = self .run_ ("--workload" ,"w2" ,"--conc" ,"1,2" ,"--rounds" ,"2" ,"--max-tokens" ,"12" ,"--prompt-tokens" ,"200" ) 45self .assertEqual (rc ,0 );self .assertTrue (L ["valid" ]) 46b = L ["blocks" ][1 ] 47self .assertEqual (b ["tokens" ],4 * 15 )# usage says 15, SSE chunks were 12 48self .assertEqual (b ["requests" ],4 ) 49self .assertIsNotNone (b ["ttft_p50" ]);self .assertIsNotNone (b ["tpot_p50" ]) 50 51def test_w4_w6_open (self ): 52rc ,L = self .run_ ("--workload" ,"w4" ,"--conc" ,"2" ,"--rounds" ,"1" ,"--joiner-at" ,"0.05" ,"--joiner-tokens" ,"400" ,"--prompt-tokens" ,"100" ) 53self .assertEqual (rc ,0 );self .assertIn ("joiner_ttft" ,L ["blocks" ][0 ]) 54rc ,L = self .run_ ("--workload" ,"w6" ,"--conc" ,"2" ,"--rounds" ,"1" ,"--share" ,"0,0.9" ,"--prompt-tokens" ,"300" ) 55self .assertEqual (len (L ["blocks" ]),2 ) 56rc ,L = self .run_ ("--workload" ,"w2" ,"--open-rates" ,"20" ,"--duration" ,"1" ,"--drain-s" ,"5" ,"--max-tokens" ,"8" ,"--prompt-tokens" ,"100" ) 57self .assertEqual (rc ,0 );self .assertEqual (L ["blocks" ][0 ]["unfinished" ],0 ) 58 59def test_analyze (self ): 60rows = [] 61for i , (a ,b )in enumerate ([(10 ,21 ), (10 ,20 ), (11 ,22 )]): 62rows += [("x" , {"workload" :"w2" ,"level" :4 ,"arm" :"A" ,"pair" :str (i ),"agg_tps" :a ,"errors" :0 }), 63 ("x" , {"workload" :"w2" ,"level" :4 ,"arm" :"B" ,"pair" :str (i ),"agg_tps" :b ,"errors" :0 })] 64r = par_analyze .analyze (rows ,"agg_tps" ) 65self .assertAlmostEqual (r [0 ]["geomean" ],2.0 ,delta = 0.06 );self .assertEqual (r [0 ]["pairs_better" ],3 ) 66 67 68if __name__ == "__main__" : 69unittest .main ()