运维测试 压力测试 - peter-xbs/CommonCodes GitHub Wiki

ab apache

ab压测工具,简单易用,主页参考:https://httpd.apache.org/docs/2.4/programs/ab.html 核心参数:

  • -n 请求总数
  • -c 并发数
  • -m http-method(post/get等)
  • -p post-file
  • -t timelimit
  • -T content-type,对于算法接口来说,可设置为application/json
  • -w 分析结果写入网页

实战命令:

 ab -p '/Users/peters/GitLabProjects/emr-parser-docker/tests/test4.json' -T application/json -c 100 -n 1000 -w http://120.27.245.66:8000/api/algorithm/process > txt_hosp_c100_w8_big.html

其中test4.json示例如下

{"args": {"content": "****", "hospital": "***", "tmpl_type": "txt", "record_type": null, "extract_title": true}}

注意postfile中仅可放一个入参,如果动态入参,请使用其它压测工具

locust

实战命令:

# _*_ coding:utf-8 _*_

"""
@Time: 2022/11/14 3:44 下午
@Author: jingcao
@Email: [email protected]
压测脚本
"""
import json
import random
import requests
from locust import HttpUser, between, task

with open('test2.json') as f:
    texts = [json.loads(line.strip()) for line in f]


class WebsiteUser(HttpUser):
    # wait_time = between(5, 15)
    host = "http://47.96.226.219:8001"
    # def on_start(self):
    #     self.client.post("/api/algorithm/process", req["args"])
    
    @task
    def index(self):
        req = random.choice(texts)
        req = req["args"]
        self.client.post("/api/algorithm/process", data=req)

上述代码编写为locustfile.py 在shell下运行

locust locustfile.py

设置user数量即可快速测试接口性能

关于locust统计报表解读:

  • statistics sheet:
1.Type:请求类型;
2.Name:请求路径;
3.requests:当前请求的数量;
4.fails:当前请求失败的数量;
5.Median:中间值,单位毫秒,一般服务器响应时间低于该值,而另一半高于该值;
6.Average:所有请求的平均响应时间,毫秒;
7.Min:请求的最小的服务器响应时间,毫秒;
8.Max:请求的最大服务器响应时间,毫秒;
9.Content Size:单个请求的大小,单位字节;
10.reqs/sec:每秒钟请求的个数。
  • charts

    • 1.吞吐量/每秒响应事务数(rps)实时统计
    • 2.平均响应时间/平均事务数实时统计
    • 3.虚拟用户数运行

补充

当缺少特定规格的服务器时,可以再运行docker时设置资源限制,来模拟特定服务器规格环境的压测 具体限制方式参考: https://docs.docker.com/config/containers/resource_constraints/