项目-压测工具wrk/性能测试pprof

328 阅读2分钟

go-wrk

在项目正式上线之前,我们通常需要通过压测来评估当前系统能够支撑的请求量、排查可能存在的隐藏bug, 同时了解了程序的实际处理能力能够帮我们更好的匹配项目的实际需求,节约资源成本。

常用参数:

-H="User-Agent: go-wrk 0.1 bechmark\nContent-Type: text/html;": 由'\n'分隔的请求头
-c=100: 使用的最大连接数
-k=true: 是否禁用keep-alives
-i=false: if TLS security checks are disabled
-m="GET": HTTP请求方法
-n=1000: 请求总数
-t=1: 使用的线程数
-b="" HTTP请求体
-s="" 如果指定,它将计算响应中包含搜索到的字符串s的频率
D:\golang\project\bluebell>go-wrk -t=8 -c=100 -n=10000 "http://10.249.44.49:8080/api/v1/posts?size=10"
==========================BENCHMARK==========================
URL:                            http://10.249.44.49:8080/api/v1/posts?size=10

Used Connections:               100
Used Threads:                   8
Total number of calls:          10000

===========================TIMINGS===========================
Total time passed:              15.65s
Avg time per request:           154.74ms
Requests per second:            639.18
Median time per request:        148.46ms
99th percentile time:           354.99ms
Slowest time for request:       443.00ms

=============================DATA=============================
Total response body sizes:              339932
Avg response body per request:          33.99 Byte
Transfer rate per second:               21727.71 Byte/s (0.02 MByte/s)
==========================RESPONSES==========================
20X Responses:          9998    (99.98%)
30X Responses:          0       (0.00%)
40X Responses:          0       (0.00%)
50X Responses:          0       (0.00%)
Errors:                 2       (0.02%)
  • 响应时间(RT) :指系统对请求作出响应的时间.
  • 吞吐量(Throughput) :指系统在单位时间内处理请求的数量
  • QPS每秒查询率(Query Per Second) :“每秒查询率”,是一台服务器每秒能够响应的查询次数,是对一个特定的查询服务器在规定时间内所处理流量多少的衡量标准。
  • TPS(TransactionPerSecond):每秒钟系统能够处理的交易或事务的数量
  • 并发连接数:某个时刻服务器所接受的请求总数

pprof

profiling 是指对应用程序的画像,画像就是应用程序使用 CPU 和内存的情况

Go语言项目中的性能优化主要有以下几个方面:

  • CPU profile:报告程序的 CPU 使用情况,按照一定频率去采集应用程序在 CPU 和寄存器上面的数据
  • Memory Profile(Heap Profile):报告程序的内存使用情况
  • Block Profiling:报告 goroutines 不在运行状态的情况,可以用来分析和查找死锁等性能瓶颈
  • Goroutine Profiling:报告 goroutines 的使用情况,有哪些 goroutine,它们的调用关系是怎样的

pprof开启后,每隔一段时间(10ms)就会收集下当前的堆栈信息,获取各个函数占用的CPU以及内存资源

web应用可以使用net/http/pprof库

本项目使用的gin框架,所以使用github.com/gin-contrib…

在路由导入库,并注册路由 pprof.Register(router)

image.png

image.png

go tool pprof -inuse_space http://10.249.44.49:8080/debug/pprof/heap

查看内存使用情况 安装graphviz

web

image.png

image.png