在工业自动化控制领域,Tango作为成熟的设备控制系统框架,其REST API接口的测试验证是系统集成中的关键环节。最近我在部署一套实验室设备控制系统时,需要对Tango 9.3.3版本的REST API进行功能验证,这个过程涉及到接口连通性、指令响应、数据格式转换等多个技术要点。
传统Tango系统通常使用CORBA协议进行通信,而REST API的引入使得移动端应用和Web服务能够更便捷地与控制系统交互。但这也带来了新的测试挑战:需要验证二进制数据与JSON格式的转换效率、检查跨平台调用的兼容性,以及评估接口在高并发场景下的稳定性。
测试环境采用Docker容器部署的Tango Test环境:
bash复制docker run -it -p 10000:10000 \
-e TANGO_HOST=localhost:10000 \
tangocs/tango-cpp:9.3.3
配套使用Postman 10.14作为主要测试工具,同时准备Python requests库脚本用于自动化验证。环境搭建时需特别注意:
通过Swagger UI访问http://<tango_host>/tango/api/v1/docs获取API文档,重点关注:
/devices 设备列表查询/device/{device}/command/{command} 指令执行/attribute/{attribute} 属性读写首先验证API服务可用性:
python复制import requests
response = requests.get("http://localhost:10000/tango/api/v1/devices")
assert response.status_code == 200
print(response.json()) # 应返回设备列表
常见问题处理:
docker logs <container_id>测试sys/tg_test/1设备的DevString命令:
bash复制curl -X POST "http://localhost:10000/tango/api/v1/device/sys%2Ftg_test%2F1/command/DevString" \
-H "Content-Type: application/json" \
-d '{"argin":"test"}'
预期返回应包含:
json复制{
"value": "test",
"type": "string",
"quality": "VALID"
}
使用Locust进行压力测试:
python复制from locust import HttpUser, task
class TangoUser(HttpUser):
@task
def read_attribute(self):
self.client.get("/tango/api/v1/attribute/sys/tg_test/1/double_scalar")
@task
def execute_command(self):
self.client.post(
"/tango/api/v1/device/sys%2Ftg_test%2F1/command/DevDouble",
json={"argin": 3.14}
)
测试指标重点关注:
| 错误码 | 可能原因 | 解决方案 |
|---|---|---|
| 500 | 设备未启动 | 检查Device Server状态 |
| 400 | 参数格式错误 | 验证JSON数据结构 |
| 404 | 接口路径错误 | 核对Swagger文档 |
ini复制# log4j.properties
log4j.logger.tango.rest=DEBUG
bash复制tcpdump -i any port 10000 -w tango_api.pcap
在实际测试中,我总结出三点关键经验:
python复制@pytest.mark.parametrize("command", ["DevBoolean", "DevLong"])
def test_command_execution(command):
response = tango_api.post_command(command)
assert response.status == "VALID"
通过这次测试,我们发现Tango REST API在9.3.3版本中对数组类型的支持仍有改进空间,特别是在处理二维光谱数据时会出现序列化性能下降的问题。建议对高频访问的数值型属性配置缓存机制,这在后续的压力测试中证实可提升约40%的响应速度。