前提
在进行性能测试时,使用mock接口是一种非常有效的方法,可以帮助您模拟真实服务的行为,从而避免对外部系统的依赖。使用Nginx作为mock接口的返回源不仅可以快速配置,还能确保mock接口本身不会成为性能瓶颈。这是因为Nginx是一个高性能的Web服务器和反向代理服务器,非常适合处理高并发请求。
一、nginx的安装
1、使用包管理器安装
【Debian/Ubuntu系统】
(1) apt-get install nginx -y
(2) nginx -s reload
【centos系统】
sudo yum install nginx
2、使用 Docker 安装
sudo docker pull nginx
sudo docker run --name my-nginx -p 80:80 -d nginx
二、nginx的启动
启动和停止 Nginx安装完成后,可以通过以下命令启动、停止或重启 Nginx:
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl status nginx
三、配置mock接口
proxy_pass:用于将请求代理到后端服务器。 root 和 alias:用于指定文件系统的根目录或别名。 try_files:用于尝试按顺序访问不同的文件路径。 return:用于直接返回固定的响应代码和内容。 rewrite:用于重写 URL,实现 URL 重定向。 if:用于基于条件执行不同的配置。 |
3.1 设置返回某个固定json格式的值
(1) cd /etc/nginx
(2) vi nginx.conf 然后增加以下location块,add_header ,return 一个固定的JSON响应
注意:nginx中的location 块主要用于配置如何处理特定URL的请求。可以设置响应头、响应数据
(以下是在nginx配置文件中配置的是,响应头是json格式并包含 charset=UTF-8 信息。这将确保JMeter在请求时能够正确解析中文字符;响应数据是直接返回一个固定的JSON响应)
server {
listen 8001;
server_name localhost;
location /hy/procenter/data/v2/tables/list {
add_header Content-Type "application/json; charset=UTF-8";
return 200 '{
"code": "00000",
"msg": "请求处理成功",
"timestamp": "1724115811614",
"result": {
"total": "2",
"data": [{
"id": "1792792659119714304", "name": "索引自动创建关联关系", "code": "auto_index_config_relation", "type": "TABLE","moduleId": "1567836685759295488", "moduleName": null,
"species": "BIS","description": null, "gmtCreate": 1716270053043,"modifiedBy": "1295915065878388737", "gmtModified": 1716428782592, "hadAuxTable": null, "auxTable": null,
"treeTable": null, "middleTable": null, "superTable": null, "columns": [], "dynamicClips": [], "references": [], "foreignKeys": [],"indexes": [], "comboUniqueIndexes": [],
"relationTables": [], "triggers": [], "createdUserName": "hy", "modifiedUserName": "hy", "superRelations": [], "sourceId": null, "sourceType": "本地数据源", sourceName": "本地数据源",
"version": "1.3", "dataValue": 10000.00, "tableMappingId": null, "lessCode": "hy", "appCode": "procenter", "isFullTextSearch": null, "domainType": 1
}]
}
}'; }}
四、访问mock接口
1、直接在浏览器中输入:
192.168.8.42:8001/hy/procenter/data/v2/tables/list
2、使用jmeter请求并压测,tps在1800左右(实际不止,已经够用,所以没有测下去了)