mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-23 03:19:41 +00:00
feat(performance-tester): add device protocol E2E simulator
Add a device-side protocol simulator under performance_tester: it performs the hello handshake, sends Opus audio frames over listen/start-stop and reports STT/LLM/TTS messages, so that 'device says something but gets no response' issues can be reproduced end to end. - supports raw PCM or 16k mono wav input - token via --token/--secret (auth enabled servers) - exits 0 only when non-empty STT and TTS audio are observed - bilingual docs under docs/device-protocol-simulator.md
This commit is contained in:
parent
8c5330f742
commit
d3c669d1f9
3 changed files with 412 additions and 0 deletions
120
docs/device-protocol-simulator.md
Normal file
120
docs/device-protocol-simulator.md
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
# 设备协议 E2E 模拟器(Device Protocol Simulator)
|
||||
|
||||
`performance_tester/performance_tester_device.py` 用真实设备协议(`hello` → `listen` → `opus` 音频帧 → `listen stop`)连接 xiaozhi-server,用于回归验证「设备说话后无反应」这类端到端问题:服务端到底有没有完成 ASR、有没有触发工具/大模型、有没有下发 TTS。
|
||||
|
||||
它不替代固件测试:不涉及麦克风/扬声器声学链路,也不验证固件看门狗与重连逻辑。
|
||||
|
||||
## 快速开始
|
||||
|
||||
```bash
|
||||
cd main/xiaozhi-server
|
||||
pip install -r requirements.txt
|
||||
|
||||
# 1) 准备一段 16kHz / 单声道 / 16bit 的语音
|
||||
ffmpeg -i question.mp3 -f s16le -ac 1 -ar 16000 /tmp/question.pcm
|
||||
# 或直接使用 wav:--wav question.wav
|
||||
|
||||
# 2) 直接运行
|
||||
python performance_tester/performance_tester_device.py \
|
||||
--url ws://127.0.0.1:8000/xiaozhi/v1/ \
|
||||
--device-id AA:BB:CC:DD:EE:FF \
|
||||
--pcm /tmp/question.pcm
|
||||
|
||||
# 或从菜单进入
|
||||
python performance_tester.py
|
||||
```
|
||||
|
||||
`--device-id` 必须是服务端已登记的设备(否则会进入绑定流程或拿不到智能体配置)。
|
||||
|
||||
## 认证
|
||||
|
||||
服务端 `server.auth.enabled=true` 时,必须携带 token,二选一:
|
||||
|
||||
| 方式 | 说明 |
|
||||
|---|---|
|
||||
| `--token <token>` | 直接使用设备 token(固件 NVS 里的值) |
|
||||
| `--secret <auth_key>` | 用服务端 `auth_key` 现签 token,适合在服务器本机调试 |
|
||||
|
||||
也支持环境变量 `DEVICE_SIM_TOKEN` / `DEVICE_SIM_SECRET`。
|
||||
注意:运行态配置可能来自管理端(`server:config` 缓存),与本地 `config.yaml` 不一致;以服务端实际返回的认证要求为准。
|
||||
|
||||
## 常用参数
|
||||
|
||||
| 参数 | 默认 | 说明 |
|
||||
|---|---|---|
|
||||
| `--url` | `ws://127.0.0.1:8000/xiaozhi/v1/` | 服务端 WebSocket 地址 |
|
||||
| `--device-id` | 无(必填) | 设备 ID |
|
||||
| `--client-id` | 随机 UUID | 客户端 ID(与 token 签名绑定) |
|
||||
| `--pcm` / `--wav` | 无 | 输入音频(16k/mono/16bit) |
|
||||
| `--tail-silence` | `1.0` | 语音结束后的静音秒数,用于触发服务端断句 |
|
||||
| `--timeout` | `40` | 等待 TTS 结束的最长秒数 |
|
||||
| `--json-out` | 无 | 结果写入 JSON 文件 |
|
||||
| `--verbose` | 关 | 打印服务端其余消息 |
|
||||
|
||||
## 输出与退出码
|
||||
|
||||
运行结束打印一行汇总:
|
||||
|
||||
```json
|
||||
{"stt": ["今天深圳的天气怎么样?"], "llm_msgs": 1, "tts_states": ["start", "sentence_start", "stop"], "tts_audio_frames": 395, "session": "..."}
|
||||
```
|
||||
|
||||
- 退出码 `0`:收到非空识别结果,且收到 TTS 音频(PASS)
|
||||
- 退出码 `2`:超时/认证失败/无识别或无音频(FAIL)
|
||||
|
||||
## 限制
|
||||
|
||||
- 只验证服务端链路;TTS 音频不解码、不校验音质。
|
||||
- MCP 工具列表返回空(设备侧工具不可用),服务端插件(天气等)不受影响。
|
||||
- 不是压测工具:单连接、单轮对话,帧按 60ms 实时发送。
|
||||
|
||||
---
|
||||
|
||||
# Device Protocol Simulator (English)
|
||||
|
||||
`performance_tester/performance_tester_device.py` connects to xiaozhi-server using the real device protocol (`hello` → `listen` → Opus frames → `listen stop`) to reproduce and verify end-to-end "device says something, server never responds" issues: did ASR produce text, did the tool/LLM run, did TTS audio come back?
|
||||
|
||||
It is **not** a firmware or acoustic test: microphone/speaker paths, watchdog and reconnect behavior are out of scope.
|
||||
|
||||
## Quick start
|
||||
|
||||
```bash
|
||||
cd main/xiaozhi-server
|
||||
pip install -r requirements.txt
|
||||
|
||||
# 16 kHz / mono / 16-bit audio
|
||||
ffmpeg -i question.mp3 -f s16le -ac 1 -ar 16000 /tmp/question.pcm
|
||||
|
||||
python performance_tester/performance_tester_device.py \
|
||||
--url ws://127.0.0.1:8000/xiaozhi/v1/ \
|
||||
--device-id AA:BB:CC:DD:EE:FF \
|
||||
--pcm /tmp/question.pcm
|
||||
```
|
||||
|
||||
`--device-id` must be a device already registered on the server.
|
||||
|
||||
## Authentication
|
||||
|
||||
When `server.auth.enabled=true`, supply a token with either `--token <token>` (the device token from firmware NVS) or `--secret <auth_key>` (sign a token locally, handy on the server host). Environment variables `DEVICE_SIM_TOKEN` / `DEVICE_SIM_SECRET` are also supported.
|
||||
|
||||
Note: the effective server config may come from the manager API (`server:config` cache) and differ from the local `config.yaml`; always follow what the running server enforces.
|
||||
|
||||
## Options
|
||||
|
||||
`--url`, `--device-id`, `--client-id`, `--pcm`/`--wav`, `--tail-silence` (default 1.0 s), `--timeout` (default 40 s), `--json-out`, `--verbose`.
|
||||
|
||||
## Result
|
||||
|
||||
A single JSON summary line is printed, e.g.:
|
||||
|
||||
```json
|
||||
{"stt": ["What's the weather in Shenzhen today?"], "llm_msgs": 1, "tts_states": ["start", "sentence_start", "stop"], "tts_audio_frames": 395}
|
||||
```
|
||||
|
||||
Exit code `0` = non-empty STT plus TTS audio; `2` = timeout/auth failure/no STT or no audio.
|
||||
|
||||
## Limitations
|
||||
|
||||
- Server-side path only; TTS audio is counted, not decoded or quality-checked.
|
||||
- MCP device tools are reported as empty; server-side plugins (weather, etc.) still work.
|
||||
- Single connection, single turn, 60 ms real-time pacing — not a load generator.
|
||||
|
|
@ -70,3 +70,7 @@ python performance_tester.py
|
|||
`AliBLTTS` 和 `XunFeiTTS` 这两项默认带的 key 是占位符,不在 `.config.yaml` 里换成自己的 key 就会报失败;想直接跳过,把这两项在 `.config.yaml` 里写成空的(键名下面不填内容)就行。
|
||||
|
||||
标了「无条件调用」的这几个(`AliyunStreamTTS`、`HuoshanDoubleStreamTTS`、`PaddleSpeechTTS`、`IndexStreamTTS`),供应器缺失或被清空时,那一项会报错,错误被测试方法内部的异常处理吃掉,这一项被记成失败 —— 结果表里会多出一行失败记录,但不影响其它项。带守卫的那几家(`AliBLTTS`、`XunFeiTTS`、`LinkeraiTTS`)是整项跳过:没配就不报错、结果表里也不会有这一行。
|
||||
|
||||
## 设备协议 E2E 模拟器
|
||||
|
||||
`performance_tester_device.py` 不测供应器延迟,而是按真实设备协议(`hello` → `listen` → opus 帧 → `listen stop`)连接服务端,验证 ASR → 工具/大模型 → TTS 全链路是否响应,适合复现「设备说话后无反应」类问题。用法、认证与限制见 [device-protocol-simulator.md](device-protocol-simulator.md)。
|
||||
|
|
|
|||
|
|
@ -0,0 +1,288 @@
|
|||
"""设备协议 E2E 测试(hello / listen / opus 音频注入)
|
||||
|
||||
模拟 ESP32 设备走完整 WebSocket 协议链路:
|
||||
hello 握手 -> listen start -> opus 音频帧 -> listen stop
|
||||
-> 等待 stt / llm / tts 消息,输出端到端结果
|
||||
|
||||
用于回归验证「设备说话后无反应」类问题:可直接确认服务端是否完成
|
||||
ASR 识别、是否调用工具/大模型、是否下发 TTS 音频。
|
||||
|
||||
用法(在 main/xiaozhi-server 目录下):
|
||||
python performance_tester.py # 按菜单选择
|
||||
python performance_tester/performance_tester_device.py \
|
||||
--url ws://127.0.0.1:8000/xiaozhi/v1/ \
|
||||
--device-id AA:BB:CC:DD:EE:FF \
|
||||
--pcm /tmp/question.pcm
|
||||
|
||||
认证(server.auth.enabled=true 时二选一):
|
||||
--token <token> 直接使用设备 token(固件 NVS 中的值)
|
||||
--secret <auth_key> 用服务端 auth_key 现签 token(服务端本机调试方便)
|
||||
也可用环境变量 DEVICE_SIM_TOKEN / DEVICE_SIM_SECRET
|
||||
|
||||
音频准备(16kHz / 单声道 / 16bit):
|
||||
ffmpeg -i question.mp3 -f s16le -ac 1 -ar 16000 question.pcm
|
||||
或直接传 16k 单声道 wav:--wav question.wav
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import uuid
|
||||
import wave
|
||||
|
||||
import numpy as np
|
||||
import opuslib_next
|
||||
import websockets
|
||||
|
||||
# 直接运行本文件时(python performance_tester/performance_tester_device.py),
|
||||
# 把 main/xiaozhi-server 加入 sys.path,保证 core.auth / config 可导入
|
||||
_SERVER_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
if _SERVER_ROOT not in sys.path:
|
||||
sys.path.insert(0, _SERVER_ROOT)
|
||||
|
||||
description = "设备协议 E2E 测试(hello/listen/opus 注入,验证 ASR->LLM->TTS)"
|
||||
|
||||
SAMPLE_RATE = 16000
|
||||
FRAME_SAMPLES = 960 # 60ms @16k
|
||||
DEFAULT_URL = "ws://127.0.0.1:8000/xiaozhi/v1/"
|
||||
|
||||
|
||||
def _load_pcm(args) -> np.ndarray:
|
||||
if args.pcm:
|
||||
data = np.fromfile(args.pcm, dtype=np.int16)
|
||||
source = args.pcm
|
||||
elif args.wav:
|
||||
with wave.open(args.wav, "rb") as w:
|
||||
if w.getframerate() != SAMPLE_RATE or w.getnchannels() != 1 or w.getsampwidth() != 2:
|
||||
raise SystemExit(
|
||||
f"{args.wav}: 需要 16kHz / 单声道 / 16bit wav,"
|
||||
f"实际 {w.getframerate()}Hz/{w.getnchannels()}ch/{w.getsampwidth()*8}bit;"
|
||||
"可先用 ffmpeg 转换"
|
||||
)
|
||||
data = np.frombuffer(w.readframes(w.getnframes()), dtype=np.int16)
|
||||
source = args.wav
|
||||
elif os.environ.get("DEVICE_SIM_PCM"):
|
||||
data = np.fromfile(os.environ["DEVICE_SIM_PCM"], dtype=np.int16)
|
||||
source = os.environ["DEVICE_SIM_PCM"]
|
||||
else:
|
||||
data = np.array([], dtype=np.int16)
|
||||
source = ""
|
||||
if data.size < FRAME_SAMPLES:
|
||||
raise SystemExit(f"音频不足一帧({data.size} samples):{source or '未提供 --pcm/--wav'}")
|
||||
return data
|
||||
|
||||
|
||||
def _resolve_token(args) -> str:
|
||||
if args.token:
|
||||
return args.token
|
||||
if os.environ.get("DEVICE_SIM_TOKEN"):
|
||||
return os.environ["DEVICE_SIM_TOKEN"]
|
||||
secret = args.secret or os.environ.get("DEVICE_SIM_SECRET")
|
||||
if secret:
|
||||
from core.auth import AuthManager
|
||||
|
||||
token = AuthManager(secret).generate_token(args.client_id, args.device_id)
|
||||
print("[device-sim] token=generated from auth_key")
|
||||
return token
|
||||
return ""
|
||||
|
||||
|
||||
async def run(args) -> int:
|
||||
pcm = _load_pcm(args)
|
||||
token = _resolve_token(args)
|
||||
enc = opuslib_next.Encoder(SAMPLE_RATE, 1, "audio")
|
||||
dec = opuslib_next.Decoder(SAMPLE_RATE, 1)
|
||||
probe = enc.encode(pcm[:FRAME_SAMPLES].tobytes(), FRAME_SAMPLES)
|
||||
if len(np.frombuffer(dec.decode(probe, FRAME_SAMPLES), dtype=np.int16)) != FRAME_SAMPLES:
|
||||
raise SystemExit("opus 编解码自检失败(检查 opuslib_next 安装)")
|
||||
print(
|
||||
f"[device-sim] pcm={len(pcm)} samples / {len(pcm)/SAMPLE_RATE:.2f}s / "
|
||||
f"rms={int(np.sqrt(np.mean(pcm.astype(np.int64) ** 2)))}"
|
||||
)
|
||||
|
||||
state = {"session": None, "stt": [], "llm": 0, "tts": [], "audio": 0, "closed": None}
|
||||
t0 = time.monotonic()
|
||||
|
||||
def log(kind, **kw):
|
||||
detail = f" {json.dumps(kw, ensure_ascii=False)}" if kw else ""
|
||||
print(f"[device-sim] {time.monotonic()-t0:6.2f}s {kind}{detail}", flush=True)
|
||||
|
||||
headers = {
|
||||
"Device-Id": args.device_id,
|
||||
"Client-Id": args.client_id,
|
||||
"Protocol-Version": "1",
|
||||
}
|
||||
if token:
|
||||
headers["Authorization"] = "Bearer " + token
|
||||
|
||||
try:
|
||||
async with websockets.connect(args.url, additional_headers=headers, max_size=None) as ws:
|
||||
async def rx():
|
||||
async for raw in ws:
|
||||
if isinstance(raw, bytes):
|
||||
state["audio"] += 1
|
||||
continue
|
||||
try:
|
||||
msg = json.loads(raw)
|
||||
except Exception:
|
||||
if str(raw).strip():
|
||||
print(f"[device-sim] server says: {str(raw)[:120]}")
|
||||
continue
|
||||
t = msg.get("type")
|
||||
if t == "hello":
|
||||
state["session"] = msg.get("session_id")
|
||||
log("hello", session=state["session"])
|
||||
elif t == "mcp":
|
||||
payload = msg.get("payload") or {}
|
||||
rid = payload.get("id")
|
||||
if rid is not None:
|
||||
if payload.get("method") == "initialize":
|
||||
result = {
|
||||
"protocolVersion": "2024-11-05",
|
||||
"capabilities": {"tools": {}},
|
||||
"serverInfo": {"name": "device-simulator", "version": "1.0"},
|
||||
}
|
||||
else:
|
||||
result = {"tools": []}
|
||||
await ws.send(
|
||||
json.dumps(
|
||||
{
|
||||
"session_id": state["session"],
|
||||
"type": "mcp",
|
||||
"payload": {"jsonrpc": "2.0", "id": rid, "result": result},
|
||||
},
|
||||
ensure_ascii=False,
|
||||
)
|
||||
)
|
||||
elif t == "stt":
|
||||
state["stt"].append(msg.get("text") or "")
|
||||
log("stt", text=msg.get("text"))
|
||||
elif t == "llm":
|
||||
state["llm"] += 1
|
||||
log("llm", emotion=msg.get("emotion"), text=(msg.get("text") or "")[:80])
|
||||
elif t == "tts":
|
||||
state["tts"].append(msg.get("state"))
|
||||
log("tts", state=msg.get("state"), text=(msg.get("text") or "")[:60])
|
||||
elif args.verbose:
|
||||
log("msg", type=t, raw=json.dumps(msg, ensure_ascii=False)[:160])
|
||||
|
||||
rx_task = asyncio.create_task(rx())
|
||||
await ws.send(
|
||||
json.dumps(
|
||||
{
|
||||
"type": "hello",
|
||||
"version": 1,
|
||||
"features": {"mcp": True},
|
||||
"transport": "websocket",
|
||||
"audio_params": {
|
||||
"format": "opus",
|
||||
"sample_rate": SAMPLE_RATE,
|
||||
"channels": 1,
|
||||
"frame_duration": 60,
|
||||
},
|
||||
},
|
||||
ensure_ascii=False,
|
||||
)
|
||||
)
|
||||
log("hello_sent")
|
||||
await asyncio.sleep(0.6)
|
||||
await ws.send(
|
||||
json.dumps(
|
||||
{"session_id": state["session"], "type": "listen", "state": "start", "mode": "auto"},
|
||||
ensure_ascii=False,
|
||||
)
|
||||
)
|
||||
log("listen_start")
|
||||
|
||||
next_at = time.monotonic()
|
||||
sent = 0
|
||||
for i in range(0, len(pcm) - FRAME_SAMPLES + 1, FRAME_SAMPLES):
|
||||
await ws.send(enc.encode(pcm[i:i + FRAME_SAMPLES].tobytes(), FRAME_SAMPLES))
|
||||
sent += 1
|
||||
next_at += 0.06
|
||||
delay = next_at - time.monotonic()
|
||||
if delay > 0:
|
||||
await asyncio.sleep(delay)
|
||||
log("frames_sent", n=sent)
|
||||
|
||||
await asyncio.sleep(args.tail_silence)
|
||||
await ws.send(
|
||||
json.dumps(
|
||||
{"session_id": state["session"], "type": "listen", "state": "stop", "mode": "auto"},
|
||||
ensure_ascii=False,
|
||||
)
|
||||
)
|
||||
log("listen_stop")
|
||||
|
||||
deadline = time.monotonic() + args.timeout
|
||||
while time.monotonic() < deadline:
|
||||
if state["tts"] and state["tts"][-1] == "stop":
|
||||
break
|
||||
await asyncio.sleep(0.25)
|
||||
rx_task.cancel()
|
||||
except websockets.exceptions.InvalidStatus as e:
|
||||
print(f"[device-sim] 握手失败:{e}(检查 url / 反向代理)")
|
||||
return 2
|
||||
except websockets.exceptions.ConnectionClosed as e:
|
||||
print(f"[device-sim] 连接被关闭:{e}(若服务端开启认证,请用 --token/--secret)")
|
||||
if not state["stt"]:
|
||||
return 2
|
||||
|
||||
summary = {
|
||||
"stt": state["stt"],
|
||||
"llm_msgs": state["llm"],
|
||||
"tts_states": state["tts"],
|
||||
"tts_audio_frames": state["audio"],
|
||||
"session": state["session"],
|
||||
}
|
||||
print("[device-sim] SUMMARY " + json.dumps(summary, ensure_ascii=False))
|
||||
if args.json_out:
|
||||
with open(args.json_out, "w", encoding="utf-8") as f:
|
||||
json.dump(summary, f, ensure_ascii=False, indent=2)
|
||||
ok = bool(state["stt"]) and state["audio"] > 0
|
||||
print("[device-sim] " + ("PASS" if ok else "FAIL: 未收到识别结果或 TTS 音频"))
|
||||
return 0 if ok else 2
|
||||
|
||||
|
||||
def _interactive_defaults(args):
|
||||
if not args.device_id:
|
||||
args.device_id = os.environ.get("DEVICE_SIM_ID", "")
|
||||
if not args.device_id and sys.stdin.isatty():
|
||||
args.device_id = input("设备 Device-Id(如 AA:BB:CC:DD:EE:FF):").strip()
|
||||
if not args.device_id:
|
||||
raise SystemExit("缺少 --device-id(或环境变量 DEVICE_SIM_ID)")
|
||||
if not args.pcm and not args.wav and sys.stdin.isatty() and not os.environ.get("DEVICE_SIM_PCM"):
|
||||
path = input("PCM 文件路径(16k/mono/s16le,留空则退出):").strip()
|
||||
if not path:
|
||||
raise SystemExit("未提供音频文件")
|
||||
args.pcm = path
|
||||
|
||||
|
||||
def build_arg_parser() -> argparse.ArgumentParser:
|
||||
p = argparse.ArgumentParser(description=description)
|
||||
p.add_argument("--url", default=os.environ.get("DEVICE_SIM_URL", DEFAULT_URL))
|
||||
p.add_argument("--device-id", default="")
|
||||
p.add_argument("--client-id", default=os.environ.get("DEVICE_SIM_CLIENT_ID") or str(uuid.uuid4()))
|
||||
p.add_argument("--pcm", help="16kHz/mono/s16le 裸 PCM 文件")
|
||||
p.add_argument("--wav", help="16kHz/mono/16bit wav 文件")
|
||||
p.add_argument("--token", default="", help="设备 token(auth.enabled=true 时)")
|
||||
p.add_argument("--secret", default="", help="服务端 auth_key,用于现签 token")
|
||||
p.add_argument("--tail-silence", type=float, default=1.0, help="语音后静音秒数(默认 1.0)")
|
||||
p.add_argument("--timeout", type=float, default=40.0, help="等待 TTS 结束的秒数(默认 40)")
|
||||
p.add_argument("--json-out", default="", help="把结果写入 JSON 文件")
|
||||
p.add_argument("--verbose", action="store_true")
|
||||
return p
|
||||
|
||||
|
||||
async def main():
|
||||
args = build_arg_parser().parse_args()
|
||||
_interactive_defaults(args)
|
||||
return await run(args)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(asyncio.run(main()))
|
||||
Loading…
Reference in a new issue