首先需要在CentOS系统上安装Postman客户端。推荐通过以下步骤安装:
.tar.gz格式);tar -xvf Postman-linux-x64-<version>.tar.gz(替换<version>为实际版本号);Postman文件夹移动至/opt目录:sudo mv Postman /opt;sudo ln -s /opt/Postman/Postman /usr/local/bin/postman。为确保性能测试的准确性,需先优化CentOS系统的配置:
sudo yum update -y,确保系统和所有软件包为最新版本;/etc/sysctl.conf文件,添加或修改以下参数以优化网络性能:net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.ip_local_port_range = "1024 65535"
net.core.somaxconn = 1024
net.core.netdev_max_backlog = 2000
net.ipv4.tcp_max_orphans = 32768
net.ipv4.tcp_syncookies = 1
执行sudo sysctl -p使配置生效;systemctl stop firewalld && systemctl disable firewalld),减少系统资源占用(注意:测试环境可使用,生产环境需谨慎);/etc/security/limits.conf,添加* soft nofile 65535和* hard nofile 65535,提升系统处理并发请求的能力。Content-Type: application/json)和请求体(如JSON参数);在集合内每个请求的“Tests”选项卡中,编写JavaScript脚本验证响应是否符合预期,例如:
// 断言响应状态码为200
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).to.have.property("code", 200); // 假设响应体中有"code"字段且值为200
pm.expect(jsonData.data).to.be.an("array"); // 假设"data"字段为数组
});
脚本会在每次请求后自动执行,若断言失败,Postman会标记该请求为失败。
测试完成后,重点关注以下关键指标:
run-tests.js文件:const newman = require('newman');
newman.run({
collection: '/path/to/your/collection.json',
reporters: 'cli', // 控制台输出报告
iterationCount: 100, // 迭代次数
concurrency: 10 // 并发数
}, function (err, summary) {
if (err) throw err;
console.log(summary);
});
运行命令:node run-tests.js;top、htop、vmstat等工具监控测试期间CentOS服务器的CPU、内存、磁盘I/O使用情况,判断系统瓶颈;通过以上步骤,可在CentOS系统上使用Postman完成API性能测试,评估接口的响应时间、吞吐量及稳定性。需注意的是,Postman的Runner功能适合简单性能测试,复杂场景(如高并发、分布式测试)建议使用JMeter等专业工具。