High-performance MoonBit vision routing proxy for New API (caption user images with xing-vl2.0 before forwarding to Xing4.1-Preview)
| 场景(16 并发) | 直连模拟上游 | 原版 proxy.py(Python + SQLite WAL) | 本代理(MoonBit Native Release) | 提升幅度 |
|---|---|---|---|---|
| 小包 JSON 透传(61 B) | 34,085 RPS · p50 0.43ms | 1,748–3,002 RPS · p50 4.19ms · 端口耗尽报 502 | 30,266 RPS · p50 0.49ms · p99 1.01ms · 0 错 | 10×–17× 吞吐,p50 低 8.6×,零报错 |
| 64 KiB + 30 段 SSE 透传 | 17,406 RPS · p50 0.85ms | 936–1,182 RPS · p50 13.3–17.4ms · 首包 P95 4.1–5.3ms | 2,703 RPS · p50 5.86ms · 首包 P95 1.22ms · 0 错 | 2.3×–2.9× 吞吐,首包快 3.4×–3.8× |
| 256 KiB Base64 带图命中 + 30 段 SSE | — | 524–544 RPS · p50 28.1–28.8ms · p99 58.4ms | 357 RPS · p50 44.32ms · p99 53.70ms · 0 错 | 纯 MoonBit 软件 SHA-256,p99 更稳且无端口/锁报错 |
node test/bench/mock-vision-upstream.mjs 18080 &
./_build/native/release/build/cmd/main/main.exe --listen 127.0.0.1:18081 --upstream 127.0.0.1:18080 &
node test/bench/loadgen.mjs 18081 16 3 small
node test/bench/loadgen.mjs 18081 16 4 sse64k
node test/bench/loadgen.mjs 18081 16 4 vision256knew-api-vision-proxy/
├── moon.mod # 模块声明与依赖 (moonbitlang/async, spectacleCase/dotenv)
├── facade.mbt # 顶层公共 API 导出门面
├── config/ # CLI 参数、环境变量与默认值解析
├── internal/
│ ├── dotenv/ # .env 加载器(进程已有环境变量优先)
│ ├── httpparse/ # 单遍零分配 HTTP/1.1 请求/响应头解析与上游序列化
│ └── sha256/ # 纯 MoonBit SHA-256 缓存键计算(基于官方 moonbitlang/x/crypto)
├── cache/ # O(1) 内存缓存 + @cond_var 并发合并 + 异步双段日志持久化
├── vision/ # 零 DOM 字节级扫描拼接 + @json.parse 自动回退改写器
├── proxy/ # 双端 Keep-Alive 连接池转发、SSE 流式透传与健康检查
├── cmd/main/ # 原生可执行入口与 Linux SO_REUSEPORT 多进程监管器
└── test/bench/ # 模拟上游与多场景固定并发压测脚本moon check --target native
moon test --target native
moon build cmd/main --target native --release
# 产物: _build/native/release/build/cmd/main/main.exe (Linux 下亦为 ELF 可执行文件,可重命名为 new-api-vision-proxy)cmd.exe /d /c 'call "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars64.bat" >nul && set "CC=cl" && moon test --target native && moon build cmd/main --target native --release'# 默认启动: 127.0.0.1:3001 -> 127.0.0.1:3000, 缓存写入当前目录 captions.{0,1}.log
./new-api-vision-proxy
# 自定义监听、上游、缓存目录与 Linux 多 Worker
./new-api-vision-proxy \
--listen 127.0.0.1:3001 \
--upstream 127.0.0.1:3000 \
--cache-dir /opt/new-api-vision-proxy \
--workers 2
# 查看内置运行统计
curl http://127.0.0.1:3001/_vision_proxy/healthz[Service]
Type=simple
WorkingDirectory=/opt/new-api-vision-proxy
ExecStart=/opt/new-api-vision-proxy/new-api-vision-proxy --cache-dir /opt/new-api-vision-proxy
Restart=always
RestartSec=2upstream vision_proxy_backend {
server 127.0.0.1:3001;
keepalive 32;
}
location = /v1/chat/completions {
proxy_pass http://vision_proxy_backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_buffering off;
proxy_request_buffering off;
proxy_read_timeout 900s;
}let default_vision_prompt : Stringfn format_ms_as_sec_2dp(elapsed_ms : UInt64) -> Stringasync fn load_env(path? : String) -> Unitasync fn maybe_rewrite_vision_chat(body : Bytes, cfg : Config, authorization : String?, store : CaptionStore, fetch_vision : async (Bytes) -> Result[Bytes, VisionFailure]) -> AssistOutcomeInstall
Download zipHigh-performance MoonBit vision routing proxy for New API (caption user images with xing-vl2.0 before forwarding to Xing4.1-Preview)