Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/gpu-head-to-head/blob/main/bench/test-bench-python.py # gpu-head-to-head aien-dev/gpu-head-to-head Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `gpu-head-to-head / bench / test-bench-python.py` 85 lines ยท 4017 bytes Raw Blame History 1 #!/usr/bin/env python3 2 """External Python measurement client controls; local synthetic HTTP only.""" 3 import importlib.util 4 import json 5 from pathlib import Path 6 import subprocess 7 import sys 8 import tempfile 9 import threading 10 from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer 11 import unittest 12 13 sys.dont_write_bytecode = True 14 HERE = Path(__file__).resolve().parent 15 spec = importlib.util.spec_from_file_location('bench_client', HERE / 'bench.py') 16 client = importlib.util.module_from_spec(spec) 17 spec.loader.exec_module(client) 18 19 def event(value): 20 return 'data: ' + json.dumps(value) + '\n\n' 21 CONTENT = event({'choices': [{'delta': {'content': 'fixture'}}]}) 22 FINISH = event({'choices': [{'finish_reason': 'length'}], 23 'usage': {'prompt_tokens': 5, 'completion_tokens': 2}}) 24 DONE = 'data: [DONE]\n\n' 25 26 class ClientTests(unittest.TestCase): 27 def serve(self, payload, callback, status=200): 28 class Handler(BaseHTTPRequestHandler): 29 def log_message(self, *_): 30 pass 31 def do_POST(self): 32 self.rfile.read(int(self.headers['Content-Length'])) 33 self.send_response(status) 34 self.send_header('Content-Type', 'text/event-stream') 35 self.end_headers() 36 self.wfile.write(payload.encode()) 37 with ThreadingHTTPServer(('127.0.0.1', 0), Handler) as server: 38 thread = threading.Thread(target=server.serve_forever, daemon=True) 39 thread.start() 40 try: 41 return callback(server.server_port) 42 finally: 43 server.shutdown() 44 thread.join() 45 def request(self, payload, status=200): 46 return self.serve(payload, lambda port: client.one_request( 47 '127.0.0.1', port, 'fixture', 'test', 2), status) 48 def test_complete_stream_passes(self): 49 row = self.request(CONTENT + FINISH + DONE) 50 self.assertIsNone(row['error']) 51 self.assertEqual(row['text'], 'fixture') 52 def test_truncated_stream_refused(self): 53 self.assertIsNotNone(self.request(CONTENT + FINISH)['error']) 54 def test_missing_finish_refused(self): 55 self.assertIsNotNone(self.request(CONTENT + DONE)['error']) 56 def test_empty_stream_refused(self): 57 self.assertIsNotNone(self.request(FINISH + DONE)['error']) 58 def test_server_error_event_refused(self): 59 self.assertIsNotNone(self.request(event({'error': {'message': 'fixture'}}) + DONE)['error']) 60 def test_malformed_json_refused(self): 61 self.assertIsNotNone(self.request('data: {bad}\n\n' + CONTENT + FINISH + DONE)['error']) 62 def test_http_error_refused(self): 63 self.assertIsNotNone(self.request('fixture', 400)['error']) 64 def test_missing_usage_remains_retokenizable(self): 65 row = self.request(CONTENT + event({'choices': [{'finish_reason': 'stop'}]}) + DONE) 66 self.assertIsNone(row['error']) 67 self.assertIsNone(row['usage']) 68 def test_cli_preserves_failed_row_and_exits_nonzero(self): 69 with tempfile.TemporaryDirectory() as tmp: 70 root = Path(tmp) 71 prompts, output = root / 'prompts.jsonl', root / 'rows.jsonl' 72 prompts.write_text('{"id":"p0","text":"test"}\n') 73 def run(port): 74 args = [sys.executable, str(HERE / 'bench.py'), '--port', str(port), '--model', 'fixture', 75 '--arm', 'CONTROL', '--round', 'R0', '--metric', 'decode', '--prompts', str(prompts), 76 '--max-tokens', '2', '--warmup', '0', '--reps', '1', '--out', str(output)] 77 result = subprocess.run(args, capture_output=True, text=True, timeout=5) 78 self.assertNotEqual(result.returncode, 0) 79 rows = [json.loads(x) for x in output.read_text().splitlines()] 80 self.assertEqual(len(rows), 1) 81 self.assertTrue(rows[0]['error']) 82 self.serve('fixture', run, status=400) 83 84 if __name__ == '__main__': 85 unittest.main()