使用wrk对api接口进行性能测试
安装
yum install -y openssl git
git clone https://github.com/wg/wrk.git wrk
cd wrk
make
cp wrk /usr/local/bin/
测试
测试服务:fastapi(pip3 install fastapi uvicorn -i https://mirrors.aliyun.com/pypi/simple)
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
    return {"Hello": "world"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
    return {"item_id": item_id, "q": q}
启动:
uvicorn hello:app --host 192.168.0.10 --port 8000 --reload
测试:
wrk -t8 -c400 -d30s http://192.168.0.10:8000
# t:线程数。关于线程数,并不是设置的越大,压测效果越好,线程设置过大,反而会导致线程切换过于频繁,效果降低,一般来说,推荐设置成压测机器 CPU 核心数的 2 倍到 4 倍就行了。
# c:模拟400个并发请求
# d:持续时间
分析测试结果
Running 30s test @ http://192.168.0.10:8000
  8 threads and 400 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   628.72ms   47.76ms 811.86ms   67.54%
    Req/Sec   107.60     88.35   440.00     70.42%
  18898 requests in 30.10s, 2.56MB read
Requests/sec:    627.89
Transfer/sec:     87.14KB
- 
列: - Avg:平均值
- Stdev:标准差
- Max:最大值
- +/- Stdev:正负一个标准差所占比例
 
- 
行: - Latency:延迟
- Req/Sec:每秒请求数
 
- 
其它: - Requests/sec:即QPS,平均每秒处理请求数
- Transfer/sec:平均每秒流量
 
本文来自博客园,作者:花酒锄作田,转载请注明原文链接:https://www.cnblogs.com/XY-Heruo/p/15931767.html
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号