SPB Git

Repository Public

perf-stub.py 7.3 KB 121 lines python

1#!/usr/bin/env python32# SP Chat iOS3# File: Scripts/perf-stub.py4# Description: Local stand-in backend for the PERFORMANCE measurements only (never used by functional tests): serves a5#              synthetic 450-model catalogue and a 200-message conversation so launch time, opening and scrolling a6#              long transcript and memory can be measured without calling a model. usage: perf-stub.py [port]7#8# Author: Simon-Pierre Boucher9# Contact: contact@spboucher.ai10import json11import sys12import time13from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer14from urllib.parse import urlparse1516PORT = int(sys.argv[1]) if len(sys.argv) > 1 else 859917MAKERS = ["openai", "anthropic", "google", "meta", "mistral", "deepseek", "qwen", "xai", "cohere", "nvidia", "amazon", "microsoft", "perplexity", "moonshot", "zai", "inconnu"]181920def model(i: int) -> dict:21    maker = MAKERS[i % len(MAKERS)]22    direct = i < 4423    return {24        "id": f"perf-direct-{i}" if direct else f"or:{maker}/perf-model-{i}", "provider": maker if direct else "openrouter", "displayName": f"Perf Model {i}",25        "kind": "chat", "status": "verified" if direct else "catalog", "available": True, "tested": direct,26        "capabilities": {"text": True, "tools": i % 3 != 0, "vision": i % 2 == 0, "reasoning": i % 4 == 0, "streaming": True},27        "contextWindow": 32000 * (1 + i % 32), "maxOutputTokens": 8000 * (1 + i % 8), "pricing": {"input": (i % 9) * 0.5, "output": (i % 9) * 2.0, "notes": "synthétique"},28        "benchmark": {"qualityScore": 60 + i % 40, "p50LatencyMs": 900 + i * 7, "p50TtftMs": 300 + i} if direct else None, "hidden": False, "favorite": i % 50 == 0,29        "route": "direct" if direct else "openrouter", "creator": maker, "creatorName": maker.capitalize(), "logo": None if maker == "inconnu" else maker,30        "description": "Modèle synthétique pour les mesures de performance. " * 3, "categories": ["tools", "vision"] if i % 2 == 0 else ["reasoning"],31        "catalog": None if direct else {"slug": f"{maker}/perf-model-{i}", "source": "openrouter", "created": 1700000000 + i * 86400, "inputModalities": ["text", "image"], "outputModalities": ["text"],32                                        "supportedParameters": ["tools", "temperature", "max_tokens", "reasoning"], "tokenizer": "Other", "rankedIn": []},33        "lastUsedAt": None,34    }353637MODELS = [model(i) for i in range(450)]38PARAGRAPH = ("La synchronisation repose sur un flux d'événements numérotés : chaque changement nomme l'entité touchée, et le client relit la ligne concernée. "39             "Le serveur reste la source de vérité ; la dernière écriture l'emporte selon son horloge.")40ANSWER = ("## Point {n}\n\n" + PARAGRAPH + "\n\n- premier élément avec du **gras** et du `code`\n- second élément, un peu plus long pour forcer un retour à la ligne sur un iPhone\n\n"41          "```swift\nfunc mesure{n}(_ valeurs: [Double]) -> Double {{\n    valeurs.reduce(0, +) / Double(max(valeurs.count, 1))\n}}\n```\n\n"42          "| Mesure | Valeur |\n|---|---|\n| lot | {n} |\n| durée | {n}0 ms |\n\n" + PARAGRAPH)434445def conversation(count: int = 200) -> dict:46    messages, parent = [], None47    for i in range(count):48        user = i % 2 == 049        mid = f"msg_perf{i:04d}"50        text = f"Question numéro {i // 2 + 1} : peux-tu détailler ce point avec un exemple ?" if user else ANSWER.format(n=i // 2 + 1)51        m = {"id": mid, "conversation_id": "conv_perf200", "parent_id": parent, "role": "user" if user else "assistant", "parts": [{"type": "text", "text": text}], "text": text,52             "model_id": None if user else MODELS[(i // 2) % 3]["id"], "provider": None if user else "anthropic", "settings": None, "routing": None,53             "usage": None if user else {"input": 1200 + i, "output": 340}, "cost_usd": None if user else 0.0021, "latency_ms": 1800, "ttft_ms": 420, "status": "complete", "error": None,54             "error_code": None, "active": True, "created_at": f"2026-11-18T{10 + i // 60:02d}:{i % 60:02d}:00.000Z", "followups": None}55        if not user and i % 20 == 1:56            m["parts"] = [{"type": "citation", "url": f"https://example.org/source-{i}", "title": f"Source {i}", "snippet": "Extrait."}] + m["parts"]57        messages.append(m)58        parent = mid59    return {"conversation": CONV, "messages": messages, "siblings": [], "attachments": [], "artifacts": [], "draft": None, "liveRun": None}606162CONV = {"id": "conv_perf200", "project_id": None, "title": "Transcription de 200 messages", "model_id": MODELS[0]["id"], "settings": {"routingMode": "manual", "toolsEnabled": True}, "pinned": False,63        "favorite": False, "archived_at": None, "parent_id": None, "mode": "chat", "message_count": 200, "total_cost_usd": 0.21, "last_message_at": "2026-11-18T13:20:00.000Z",64        "created_at": "2026-11-18T10:00:00.000Z", "updated_at": "2026-11-18T13:20:00.000Z", "project_name": None, "project_color": None, "has_draft": False, "streaming": False}65ROUTES = {66    "/api/health": {"ok": True},67    "/api/auth/state": {"rpId": "localhost", "configOk": True, "signedIn": True, "enrolment": None},68    "/api/models": {"models": MODELS, "defaultModel": MODELS[0]["id"], "dock": [MODELS[0]["id"], MODELS[60]["id"], MODELS[90]["id"]], "openrouter": {"configured": True, "catalog": {"total": 406, "chat": 406, "syncedAt": "2026-11-19T14:19:12.804Z"}}},69    "/api/presets": {"presets": []}, "/api/projects": {"projects": []}, "/api/prompts": {"prompts": [], "tags": []},70    "/api/settings": {"settings": {"assistant": {"followUps": True}}, "tools": [{"name": "web_search", "category": "web"}], "integrations": {"tavily": True}},71    "/api/conversations": {"conversations": [CONV]}, "/api/drafts": {"draft": None},72    "/api/sync": {"events": [], "head": 1, "truncated": False, "runs": [], "serverTime": "2026-11-19T15:00:00.000Z"},73}747576class Handler(BaseHTTPRequestHandler):77    protocol_version = "HTTP/1.1"7879    def log_message(self, *args):80        pass8182    def send_json(self, payload, status=200):83        body = json.dumps(payload).encode()84        self.send_response(status)85        self.send_header("Content-Type", "application/json")86        self.send_header("Content-Length", str(len(body)))87        self.end_headers()88        self.wfile.write(body)8990    def do_GET(self):91        path = urlparse(self.path).path92        if path == "/api/events":            # the change feed: hello, then silence kept alive by comments93            self.send_response(200)94            self.send_header("Content-Type", "text/event-stream")95            self.send_header("Connection", "close")96            self.end_headers()97            try:98                self.wfile.write(b'event: hello\ndata: {"head":1,"runs":[]}\n\n')99                self.wfile.flush()100                while True:101                    time.sleep(15)102                    self.wfile.write(b": ping\n\n")103                    self.wfile.flush()104            except OSError:105                return106        if path.startswith("/api/conversations/conv_perf200"):107            return self.send_json(conversation())108        if path in ROUTES:109            return self.send_json(ROUTES[path])110        self.send_json({"error": "introuvable"}, 404)111112    def do_PUT(self):113        self.rfile.read(int(self.headers.get("Content-Length") or 0))114        self.send_json({"ok": True})115116    do_POST = do_PATCH = do_DELETE = do_PUT117118119if __name__ == "__main__":120    ThreadingHTTPServer(("127.0.0.1", PORT), Handler).serve_forever()121
Files main
Menu

Theme