File size: 6,814 Bytes
f3bcb28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python3
"""
Simple benchmark script for quick performance testing.

Usage: python tests/load/benchmark.py [--requests N] [--concurrent N]
"""

import asyncio
import aiohttp
import argparse
import time
import statistics
from dataclasses import dataclass
from typing import Optional


@dataclass
class BenchmarkResult:
    total_requests: int
    successful_requests: int
    failed_requests: int
    total_time_seconds: float
    requests_per_second: float
    avg_latency_ms: float
    min_latency_ms: float
    max_latency_ms: float
    p50_latency_ms: float
    p95_latency_ms: float
    p99_latency_ms: float


async def make_request(
    session: aiohttp.ClientSession, url: str, payload: dict, headers: dict
) -> tuple[bool, float, Optional[str]]:
    """Make a single request and return (success, latency_ms, error)."""
    start = time.perf_counter()
    try:
        async with session.post(url, json=payload, headers=headers) as resp:
            latency = (time.perf_counter() - start) * 1000
            if resp.status in (200, 429):  # Success or rate limited
                return True, latency, None
            else:
                text = await resp.text()
                return False, latency, f"Status {resp.status}: {text[:100]}"
    except Exception as e:
        latency = (time.perf_counter() - start) * 1000
        return False, latency, str(e)


async def run_benchmark(
    base_url: str,
    total_requests: int,
    concurrent: int,
    api_key: str,
) -> BenchmarkResult:
    """Run the benchmark."""
    url = f"{base_url}/v1/chat/completions"
    headers = {
        "X-API-Key": api_key,
        "Content-Type": "application/json",
    }
    payload = {
        "model": "phi-2",
        "messages": [
            {"role": "user", "content": "Hello, benchmark test!"},
        ],
        "max_tokens": 20,
    }

    latencies = []
    successes = 0
    failures = 0
    errors = []

    print(f"\nRunning benchmark: {total_requests} requests, {concurrent} concurrent")
    print("-" * 50)

    connector = aiohttp.TCPConnector(limit=concurrent)
    timeout = aiohttp.ClientTimeout(total=30)

    start_time = time.perf_counter()

    async with aiohttp.ClientSession(connector=connector, timeout=timeout) as session:
        # Create semaphore for concurrency control
        semaphore = asyncio.Semaphore(concurrent)

        async def bounded_request():
            async with semaphore:
                return await make_request(session, url, payload, headers)

        # Create all tasks
        tasks = [bounded_request() for _ in range(total_requests)]

        # Progress tracking
        completed = 0
        for coro in asyncio.as_completed(tasks):
            success, latency, error = await coro
            latencies.append(latency)
            if success:
                successes += 1
            else:
                failures += 1
                if error and len(errors) < 5:
                    errors.append(error)

            completed += 1
            if completed % 100 == 0 or completed == total_requests:
                print(
                    f"  Progress: {completed}/{total_requests} ({completed / total_requests * 100:.1f}%)"
                )

    total_time = time.perf_counter() - start_time
    rps = total_requests / total_time

    # Calculate percentiles
    sorted_latencies = sorted(latencies)
    p50_idx = int(len(sorted_latencies) * 0.50)
    p95_idx = int(len(sorted_latencies) * 0.95)
    p99_idx = int(len(sorted_latencies) * 0.99)

    result = BenchmarkResult(
        total_requests=total_requests,
        successful_requests=successes,
        failed_requests=failures,
        total_time_seconds=total_time,
        requests_per_second=rps,
        avg_latency_ms=statistics.mean(latencies),
        min_latency_ms=min(latencies),
        max_latency_ms=max(latencies),
        p50_latency_ms=sorted_latencies[p50_idx],
        p95_latency_ms=sorted_latencies[p95_idx],
        p99_latency_ms=sorted_latencies[p99_idx],
    )

    return result, errors


def print_results(result: BenchmarkResult, errors: list[str]):
    """Print benchmark results."""
    print("\n" + "=" * 50)
    print("BENCHMARK RESULTS")
    print("=" * 50)

    print("\n📊 Throughput:")
    print(f"   Total requests:     {result.total_requests}")
    print(
        f"   Successful:         {result.successful_requests} ({result.successful_requests / result.total_requests * 100:.1f}%)"
    )
    print(
        f"   Failed:             {result.failed_requests} ({result.failed_requests / result.total_requests * 100:.1f}%)"
    )
    print(f"   Total time:         {result.total_time_seconds:.2f}s")
    print(f"   Requests/second:    {result.requests_per_second:.2f}")

    print("\n⏱️  Latency (ms):")
    print(f"   Average:            {result.avg_latency_ms:.2f}")
    print(f"   Min:                {result.min_latency_ms:.2f}")
    print(f"   Max:                {result.max_latency_ms:.2f}")
    print(f"   P50 (median):       {result.p50_latency_ms:.2f}")
    print(f"   P95:                {result.p95_latency_ms:.2f}")
    print(f"   P99:                {result.p99_latency_ms:.2f}")

    if errors:
        print(f"\n❌ Sample errors ({len(errors)} shown):")
        for err in errors:
            print(f"   - {err}")

    # Performance grade
    print("\n🎯 Performance Grade:")
    if result.p95_latency_ms < 100 and result.requests_per_second > 100:
        print("   ⭐⭐⭐⭐⭐ EXCELLENT")
    elif result.p95_latency_ms < 200 and result.requests_per_second > 50:
        print("   ⭐⭐⭐⭐ GOOD")
    elif result.p95_latency_ms < 500 and result.requests_per_second > 20:
        print("   ⭐⭐⭐ ACCEPTABLE")
    elif result.p95_latency_ms < 1000:
        print("   ⭐⭐ NEEDS IMPROVEMENT")
    else:
        print("   ⭐ POOR")

    print("=" * 50)


async def main():
    parser = argparse.ArgumentParser(description="TensorWall Benchmark")
    parser.add_argument("--url", default="http://localhost:8000", help="Base URL")
    parser.add_argument(
        "--requests", "-n", type=int, default=1000, help="Total requests"
    )
    parser.add_argument(
        "--concurrent", "-c", type=int, default=50, help="Concurrent requests"
    )
    parser.add_argument("--api-key", default=None, help="API key (required)")
    args = parser.parse_args()

    print("🚀 TensorWall Benchmark Tool")
    print(f"   Target: {args.url}")
    print(f"   Requests: {args.requests}")
    print(f"   Concurrency: {args.concurrent}")

    result, errors = await run_benchmark(
        base_url=args.url,
        total_requests=args.requests,
        concurrent=args.concurrent,
        api_key=args.api_key,
    )

    print_results(result, errors)


if __name__ == "__main__":
    asyncio.run(main())