如何在Linux系统上使用Postman进行性能测试
Postman虽为API调试工具,但通过其内置的Runner功能及Newman命令行工具,可实现基础的Linux环境下的性能测试。以下是具体操作步骤:
.tar.gz格式)下载。/opt目录(系统级软件存放路径):tar -xzf Postman-linux-x64-*.tar.gz -C /opt
postman和pm命令的软链接:sudo ln -s /opt/Postman/Postman /usr/local/bin/postman
sudo ln -s /usr/local/bin/postman /usr/local/bin/pm
安装完成后,终端输入postman即可启动应用。Linux API Performance Tests),保存。Content-Type: application/json)、Body(如JSON参数{"key": "value"}),保存至集合。my-performance-tests.json),保存至Linux服务器本地目录(如~/postman-collections)。pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Response contains expected data", function () {
const jsonData = pm.response.json();
pm.expect(jsonData.key).to.eql("expected_value");
});
500 Internal Server Error)。测试完成后,Postman会生成包含以下关键指标的报告:
若需将性能测试集成至CI/CD流程(如Jenkins),可通过Newman(Postman的命令行工具)实现自动化:
npm install -g newman
run-performance.js文件,内容如下:const newman = require('newman');
newman.run({
collection: require('./postman-collections/my-performance-tests.json'), // 集合路径
reporters: 'cli', // 控制台输出报告
iterationCount: 100, // 迭代次数(可选,覆盖Runner设置)
concurrency: 10 // 并发数(可选,覆盖Runner设置)
}, function (err, summary) {
if (err) { throw err; }
console.log(`测试完成,失败请求数:${summary.run.failures.length}`);
});
node run-performance.js,Newman将自动运行性能测试,并在控制台输出结果。可将此命令添加至CI/CD pipeline,实现每次代码提交后自动测试。top、htop、vmstat)监控服务器资源使用情况,分析性能瓶颈(如CPU过高导致的响应变慢)。