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/MojoLlama/blob/main/src/mojollama/tests/test_batching.py # MojoLlama Archived Mojo/MAX quantizer and serving research repository (read-only history) 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 `MojoLlama / src / mojollama / tests / test_batching.py` 87 lines ยท 3468 bytes Raw Blame History 1 #!/usr/bin/env python3 2 """Continuous batching load test for server_batch_moe.py""" 3 import urllib.request, json, time, threading, sys 4 5 SERVER = "http://localhost:8081" 6 results = [] 7 errors = [] 8 9 def send_request(prompt, idx): 10 t0 = time.time() 11 data = json.dumps({"prompt": prompt, "max_tokens": 20, "temperature": 0.0}).encode() 12 req = urllib.request.Request(f"{SERVER}/v1/completions", 13 data=data, 14 headers={"Content-Type": "application/json"}) 15 try: 16 resp = urllib.request.urlopen(req, timeout=60) 17 body = json.loads(resp.read()) 18 elapsed = time.time() - t0 19 gen_text = body.get("choices", [{}])[0].get("text", "") 20 finish = body.get("choices", [{}])[0].get("finish_reason", "?") 21 results.append({"idx": idx, "elapsed": elapsed, "text": gen_text[:40], "finish": finish, "tokens": body.get("usage", {}).get("completion_tokens", 0)}) 22 print(f" [{idx}] {elapsed:.1f}s finish={finish} tokens={body.get('usage',{}).get('completion_tokens',0)} '{gen_text[:40]}'") 23 except Exception as e: 24 errors.append({"idx": idx, "error": str(e)}) 25 print(f" [{idx}] ERROR: {e}") 26 27 print("=" * 60) 28 print("ITEM 6: Continuous batching load test") 29 print("=" * 60) 30 31 # Test 1: 5 concurrent requests 32 print("\n--- Test 1: 5 concurrent requests ---") 33 threads = [] 34 prompts = [ 35 "The capital of France is", 36 "The theory of relativity states that", 37 "Machine learning is a field of", 38 "Python is a programming language that", 39 "The mitochondria is the powerhouse of", 40 ] 41 t0 = time.time() 42 for i, prompt in enumerate(prompts): 43 t = threading.Thread(target=send_request, args=(prompt, i)) 44 t.start() 45 threads.append(t) 46 for t in threads: 47 t.join() 48 t_total = time.time() - t0 49 50 print(f"\nResults: {len(results)}/{len(prompts)} OK, {len(errors)} errors") 51 if results: 52 avg_lat = sum(r["elapsed"] for r in results) / len(results) 53 max_lat = max(r["elapsed"] for r in results) 54 min_lat = min(r["elapsed"] for r in results) 55 total_tokens = sum(r["tokens"] for r in results) 56 print(f" Avg latency: {avg_lat*1000:.0f} ms") 57 print(f" Min latency: {min_lat*1000:.0f} ms") 58 print(f" Max latency: {max_lat*1000:.0f} ms") 59 print(f" Total generation tokens: {total_tokens}") 60 print(f" Total wall time: {t_total:.1f}s") 61 if errors: 62 for e in errors: 63 print(f" Error in request {e['idx']}: {e['error']}") 64 65 # Test 2: Streaming test 66 print("\n--- Test 2: Streaming test ---") 67 try: 68 data = json.dumps({"prompt": "Once upon a time,", "max_tokens": 10, "temperature": 0.0, "stream": True}).encode() 69 req = urllib.request.Request(f"{SERVER}/v1/completions", 70 data=data, 71 headers={"Content-Type": "application/json"}) 72 resp = urllib.request.urlopen(req, timeout=30) 73 chunks = [] 74 for line in resp: 75 line = line.decode().strip() 76 if line.startswith("data:"): 77 chunks.append(line) 78 print(f" Received {len(chunks)} SSE chunks") 79 for c in chunks[:5]: 80 print(f" {c[:80]}") 81 if len(chunks) > 5: 82 print(f" ... ({len(chunks)-5} more)") 83 print(f" Streaming: OK ({len(chunks)} chunks)") 84 except Exception as e: 85 print(f" Streaming ERROR: {e}") 86 87 print(f"\nContinuous batching: {len(results)}/{len(prompts)} requests OK, avg latency {avg_lat*1000:.0f} ms, max concurrent {len(prompts)}")