relay-bench 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,75 @@
1
+ Metadata-Version: 2.4
2
+ Name: relay-bench
3
+ Version: 0.1.0
4
+ Summary: 测 AI / API 中转站的可用率与延迟:并发请求 /v1/models,输出状态码与中位延迟表格,可存 JSON 接监控
5
+ Author: kuku-momo
6
+ License: MIT
7
+ Project-URL: Homepage, https://kuku-momo.github.io/api-relay-benchmark/
8
+ Project-URL: Documentation, https://kuku-momo.github.io/ai-relay/
9
+ Project-URL: Source, https://github.com/kuku-momo/api-relay-benchmark
10
+ Keywords: api relay,benchmark,latency,uptime,llm,monitoring
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Topic :: Software Development :: Libraries
14
+ Requires-Python: >=3.8
15
+ Description-Content-Type: text/markdown
16
+ License-File: LICENSE
17
+ Dynamic: license-file
18
+
19
+ # AI 中转站测速与可用率自检(api-relay-benchmark)
20
+
21
+ 一个不依赖第三方的小脚本:给你自己常用的中转站端点测**可用率、状态码、延迟中位数**,输出 Markdown 表格贴进 issue、周报或者监控告警里。
22
+
23
+ > 中转站评测/中转站测速/API 中转站可用率 · 不需要装包,只用 Python 标准库。
24
+
25
+ ## 为什么需要它
26
+
27
+ 中转站的"能不能用"和"快不快"是两件事,而且都会随时间变化。别人写的排行榜是别人那一刻的测量结果;你自己跑一次,才知道**此刻**哪条线路是通的。
28
+
29
+ ## 安装
30
+
31
+ 不需要安装。只要 Python 3.8+:
32
+
33
+ ```bash
34
+ git clone https://github.com/kuku-momo/api-relay-benchmark
35
+ cd api-relay-benchmark
36
+ ```
37
+
38
+ ## 用法
39
+
40
+ ```bash
41
+ # 测两个端点,各跑 3 次
42
+ python3 bench.py https://ai.tt2066.top/v1 https://api.openai.com/v1 --key $RELAY_KEY
43
+
44
+ # 端点写在文件里,跑 5 次,顺便存 JSON
45
+ python3 bench.py --file endpoints.txt --rounds 5 --json report.json
46
+ ```
47
+
48
+ 输出:
49
+
50
+ ```
51
+ | 端点 | 成功/次数 | 状态码 | 中位延迟 | 最快 | 最慢 |
52
+ |---|---|---|---|---|---|
53
+ | https://ai.tt2066.top/v1 | 3/3 | 200 | 412.3ms | 388.1ms | 470.9ms |
54
+ ```
55
+
56
+ ## 怎么读结果
57
+
58
+ - `4xx` 全部失败:Key 不对或模型未开通,不是网络问题;
59
+ - `5xx` 抖动:上游通道不稳,看是否连续;
60
+ - 中位延迟高但最快值正常:链路拥塞或限流,不是端点本身慢;
61
+ - 只有 `error` 列有内容:DNS / TLS / 超时,先查本地网络。
62
+
63
+ ## 端点填什么
64
+
65
+ 填到 `/v1`(OpenAI 兼容)。脚本会自动拼 `/models`。Anthropic 兼容的端点用 `/v1/messages`,本脚本只测 OpenAI 侧连通性。
66
+
67
+ ## 相关文档
68
+
69
+ - [AI 中转站 / API 中转站接入总览](https://kuku-momo.github.io/ai-relay/)
70
+ - [base_url / Key / model 三字段配置](https://kuku-momo.github.io/ai-relay/setup/)
71
+ - 服务入口:[ai.tt2066.top](https://ai.tt2066.top/)
72
+
73
+ ## License
74
+
75
+ MIT
@@ -0,0 +1,7 @@
1
+ relay_bench.py,sha256=1xpd710cGaOXITwZNt7S3Ypm_iBfQ_1PddnKM_Mxkp4,3569
2
+ relay_bench-0.1.0.dist-info/licenses/LICENSE,sha256=ZoUOoFaoXBc7Al-BzqfIrYkS66CkbBh8Moeij6LEpO4,42
3
+ relay_bench-0.1.0.dist-info/METADATA,sha256=PUgbi0p-cSA1IjGcHxtlEqhkquXi-Ccijsab-D38zLY,2774
4
+ relay_bench-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
5
+ relay_bench-0.1.0.dist-info/entry_points.txt,sha256=GXAREq1HWCI0uk1vmjTMLgPdNXmnNgaPWg1ZWS1YCZ4,49
6
+ relay_bench-0.1.0.dist-info/top_level.txt,sha256=Zy1L0OiOiy1k128JiacNyIdhQN4cT6g5k0Od-Jw9sC8,12
7
+ relay_bench-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ relay-bench = relay_bench:main
@@ -0,0 +1,3 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 kuku-momo
@@ -0,0 +1 @@
1
+ relay_bench
relay_bench.py ADDED
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """API 中转站可用率 / 延迟自检。
4
+
5
+ 对一组 base_url 并发打 /v1/models,记录 DNS/TLS/首字节/总耗时与状态码,
6
+ 输出表格 + Markdown 报告,方便贴到 issue 或周报里对比。
7
+
8
+ 用法:
9
+ python3 bench.py https://ai.tt2066.top/v1 https://api.openai.com/v1 --key sk-xxx
10
+ python3 bench.py --file endpoints.txt --rounds 5 --json report.json
11
+ """
12
+ import argparse, json, ssl, statistics, sys, time, urllib.request, urllib.error
13
+ from concurrent.futures import ThreadPoolExecutor
14
+
15
+
16
+ def one(endpoint, key=None, timeout=15):
17
+ url = endpoint.rstrip('/') + '/models'
18
+ t0 = time.perf_counter()
19
+ req = urllib.request.Request(url, headers={'User-Agent': 'relay-bench/1.0'})
20
+ if key:
21
+ req.add_header('Authorization', 'Bearer ' + key)
22
+ req.add_header('x-api-key', key)
23
+ status, size, err = None, 0, None
24
+ try:
25
+ ctx = ssl.create_default_context()
26
+ with urllib.request.urlopen(req, timeout=timeout, context=ctx) as r:
27
+ status = r.status
28
+ body = r.read()
29
+ size = len(body)
30
+ except urllib.error.HTTPError as e:
31
+ status = e.code
32
+ except Exception as e: # 网络 / TLS / 超时
33
+ err = type(e).__name__ + ': ' + str(e)[:80]
34
+ ms = (time.perf_counter() - t0) * 1000
35
+ return {'endpoint': endpoint, 'status': status, 'ms': round(ms, 1), 'bytes': size, 'error': err}
36
+
37
+
38
+ def main():
39
+ ap = argparse.ArgumentParser()
40
+ ap.add_argument('endpoints', nargs='*')
41
+ ap.add_argument('--file', help='每行一个 base_url 的文件')
42
+ ap.add_argument('--key', help='API Key(可选,不带则只测连通性)')
43
+ ap.add_argument('--rounds', type=int, default=3, help='每个端点重复次数,取中位数')
44
+ ap.add_argument('--timeout', type=float, default=15)
45
+ ap.add_argument('--json', help='把结果写成 JSON')
46
+ a = ap.parse_args()
47
+
48
+ eps = list(a.endpoints)
49
+ if a.file:
50
+ with open(a.file, encoding='utf-8') as f:
51
+ eps += [l.strip() for l in f if l.strip() and not l.startswith('#')]
52
+ if not eps:
53
+ ap.error('至少给一个 base_url,例如 https://ai.tt2066.top/v1')
54
+
55
+ results = []
56
+ with ThreadPoolExecutor(max_workers=8) as pool:
57
+ for ep in eps:
58
+ runs = list(pool.map(lambda _: one(ep, a.key, a.timeout), range(a.rounds)))
59
+ ok = [r for r in runs if r['status'] and r['status'] < 400]
60
+ results.append({
61
+ 'endpoint': ep,
62
+ 'ok': len(ok),
63
+ 'rounds': a.rounds,
64
+ 'status': runs[0]['status'],
65
+ 'median_ms': round(statistics.median([r['ms'] for r in runs]), 1),
66
+ 'min_ms': min(r['ms'] for r in runs),
67
+ 'max_ms': max(r['ms'] for r in runs),
68
+ 'error': next((r['error'] for r in runs if r['error']), None),
69
+ })
70
+
71
+ print('\n| 端点 | 成功/次数 | 状态码 | 中位延迟 | 最快 | 最慢 |')
72
+ print('|---|---|---|---|---|---|')
73
+ for r in results:
74
+ print('| %s | %d/%d | %s | %sms | %sms | %sms |' % (
75
+ r['endpoint'], r['ok'], r['rounds'], r['status'], r['median_ms'], r['min_ms'], r['max_ms']))
76
+ if a.json:
77
+ with open(a.json, 'w', encoding='utf-8') as f:
78
+ json.dump(results, f, ensure_ascii=False, indent=2)
79
+ print('\n写入', a.json)
80
+
81
+ # 全部失败时返回非 0,方便接 CI / 定时任务
82
+ return 0 if any(r['ok'] for r in results) else 1
83
+
84
+
85
+ if __name__ == '__main__':
86
+ sys.exit(main())