單機簡單壓測及調優

2019-04-14 11:50:00
CJL
原創
4593

安裝https://www.cnblogs.com/terryguan/p/6681055.html

其他概念蔘考:https://www.cnblogs.com/nulige/p/9370063.html

蔘數

-n  卽requests,用於指定壓力測試總共的執行次數。
-c  卽concurrency,用於指定的併髮數。
-t  卽timelimit,等待響應的最大時間(單位:秒)。
-b  卽windowsize,TCP髮送/接收的緩衝大小(單位:字節)。
-p  卽postfile,髮送POST請求時需要上傳的文件,此外還必鬚設置-T蔘數。
-u  卽putfile,髮送PUT請求時需要上傳的文件,此外還必鬚設置-T蔘數。
-T  卽content-type,用於設置Content-Type請求頭信息,例如:application/x-www-form-urlencoded,默認值爲text/plain。
-v  卽verbosity,指定打印幫助信息的冗餘級彆。
-w  以HTML錶格形式打印結果。
-i  使用HEAD請求代替GET請求。
-x  插入字符串作爲table標籤的屬性。
-y  插入字符串作爲tr標籤的屬性。
-z  插入字符串作爲td標籤的屬性。
-C  添加cookie信息,例如:"Apache=1234"(可以重覆該蔘數選項以添加多箇)。
-H  添加任意的請求頭,例如:"Accept-Encoding: gzip",請求頭將會添加在現有的多箇請求頭之後(可以重覆該蔘數選項以添加多箇)。
-A  添加一箇基本的網絡認證信息,用戶名和密碼之間用英文冒號隔開。
-P  添加一箇基本的代理認證信息,用戶名和密碼之間用英文冒號隔開。
-X  指定使用的和端口號,例如:"126.10.10.3:88"。
-V  打印版本號併退齣。
-k  使用HTTP的KeepAlive特性。
-d  不顯示百分比。
-S  不顯示預估和警告信息。
-g  輸齣結果信息到gnuplot格式的文件中。
-e  輸齣結果信息到CSV格式的文件中。
-r  指定接收到錯誤信息時不退齣程序。
-h  顯示用法信息,其實就是ab -help。

樣例

ab -n 50000 -c 1020 http://192.168.1.110/a.html

註意事項

被測機器與壓測機器要分開


錯誤:socket: Too many open files (24)

壓測機需要修改機器最大允許打開文件

ulimit -a

open files                      (-n) 1024 默認最大文件打開箇數

修改爲20W

ulimit -n 204800


錯誤:apr_socket_recv: Connection reset by peer (104)

當socket齣現錯誤時讓不退齣,加 -r 蔘數


錯誤:apr_pollset_poll: The timeout specified has expired (70007)

加-k 蔘數 保持連接


壓測要排除的要素

被測機硬件配置(CPU、內存、硬盤)

壓測機硬件配置(CPU、內存、硬盤)

網絡連接:帶寬、交換機性能、網卡

排除其他因素時需要使用相應的CPU、IO、網絡壓測工具


iperf安裝和使用:https://blog.csdn.net/shenzhen206/article/details/72820341

服務端:iperf -s -d 

客戶端:iperf -c 192.168.100.55 -t 20 -i 2 -d -m 

上圖分彆是本機和普通傢用無線網絡速率,可以看到差距明顯

上圖是5G無線網絡,可以看到基本翻倍,5G的內網速率還是有很多提陞的


壓測結果

如何查看linux硬件配置:https://www.cnblogs.com/xd502djj/p/4318289.html

被測機器配置server1:Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz 1核 內存8G

被測機器配置server2:Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz 2核 內存8G

壓測機client1:Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz 1核 內存4G

壓測機client2:Intel(R) Core(TM) i5-7360u CPU @ 2.30GHz 1核 內存16G

nginx+靜態頁麵

server1+client1: ab -n 200000 -c 500 -r http://192.168.1.110/a.html

server1+client2: ab -n 20000 -c 100 -r http://192.168.1.110/a.html 4000 QPS

不考慮網絡因素QPS最高5000

server1+client1: ab -n 10000 -c 100 -r http://192.168.1.110/aa phpinfo+url重寫 530 QPS URL重寫基本無影響

server1+client1: ab -n 10000 -c 500 -r http://192.168.1.110/index.php 直接die輸齣3箇字符 2400 QPS

server1+client1: ab -n 10000 -c 500 -r http://192.168.1.110/index.php 不執行任何php代碼 2600 QPS


蔘考:https://www.cnblogs.com/cocoliu/p/8566193.html

php-fpm調優嚐試

ulimit -n 204800 #內核打開文件數,機器配置較低時無效

php-fpm改爲socket方式(嚐試無效果,QPS降爲2300 )

修改php-fpm.conf

listen = /dev/shm/php-cgi.sock
listen.owner = www
listen.group = www

nginx配置

fastcgi_pass unix:/dev/shm/php-cgi.sock;

server1+client1: ab -n 10000 -c 500 -r http://192.168.1.110/index.php 直接die輸齣3箇字符 2300 QPS

修改nginx的backlog

backlog=1024(等待連接隊列的最大長度)修改後效果不大

server1+client1: ab -n 10000 -c 500 -r http://192.168.1.110/index.php 直接die輸齣3箇字符 2600 QPS

php-fpm進程數

pm = dynamic; 
pm.max_children = 300; 靜態方式下開啟的php-fpm進程數量
pm.start_servers = 20; 動態方式下的起始php-fpm進程數量
pm.min_spare_servers = 5; 動態方式下的最小php-fpm進程數量
pm.max_spare_servers = 35; 動態方式下的最大php-fpm進程數量

max_spare_servers 調整到100性能反而下降瞭

server1+client1: ab -n 10000 -c 500 -r http://192.168.1.110/index.php 直接die輸齣3箇字符 900 QPS

pm.max_requests = 500; #1024 有部分提陞

server1+client1: ab -n 10000 -c 500 -r http://192.168.1.110/index.php 直接die輸齣3箇字符 2700 QPS

慢日誌

request_slowlog_timeout = 2; 開啟慢日誌

slowlog = log/$pool.log.slow; 慢日誌路徑

開啟opcache

http://www.cnblogs.com/JohnABC/p/4531029.html

https://www.cnblogs.com/chenguoli/p/7607208.html


查看php-fpm狀態

nginx配置

location ~ ^/status$ {
    include fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
}

php-fpm.conf配置

pm.status_path = /status

  pool:php-fpm池的名稱,一般都是應該是www
  process manage:進程的管理方法,php-fpm支持三種管理方法,分彆是static,dynamic和ondemand,一般情況下都是dynamic
  start time:php-fpm啟動時候的時間,不管是restart或者reload都會更新這裡的時間
  start since:php-fpm自啟動起來經過的時間,默認爲秒
  accepted conn:當前接收的連接數
  listen queue:在隊列中等待連接的請求箇數,如果這箇數字爲非0,那麽最好增加進程的fpm箇數
  max listen queue:從fpm啟動以來,在隊列中等待連接請求的最大值
  listen queue len:等待連接的套接字隊列大小
  idle processes:空閒的進程箇數
  active processes:活動的進程箇數
  total processes:總共的進程箇數
  max active processes:從fpm啟動以來,活動進程的最大箇數,如果這箇值小於當前的max_children,可以調小此值
  max children reached:當pm嚐試啟動更多的進程,卻因爲max_children的限製,沒有啟動更多進程的次數。如果這箇值非0,那麽可以適當增加fpm的進程數
  slow requests:慢請求的次數,一般如果這箇值未非0,那麽可能會有慢的php進程,一般一箇不好的mysql查詢是最大的禍首。

關閉nginx日誌  效果不明顯不到 2700 QPS

access_log off;

nginx工作進程數

worker_processes  auto #或者與核數相衕


更換測試工具wrk

蔘考:https://blog.csdn.net/Diligent_ten/article/details/79081433

server1+client1: wrk -t 2 -c 50 -d 20 --latency http://192.168.1.110/a.html 12056 QPS

server1+client1: wrk -t 2 -c 50 -d 20 --latency http://192.168.1.110/index.php 4868 QPS

wrk工具性能更高

server2+client1: wrk -t 5 -c 100 -d 20 --latency http://192.168.1.110/a.html 19590 QPS

server2+client1: wrk -t 5 -c 100 -d 20 --latency http://192.168.1.110/index.php 8548 QPS

基本CPU一箇核可以達到 1W QPS HTML/ 5k QPS PHP


更換web server爲swoole

swoole安裝: https://blog.csdn.net/u010474681/article/details/82463779

yum install glibc-headers

yum install gcc-c++ 

Web Server代碼:https://wiki.swoole.com/wiki/page/327.html

server2+client1: wrk -t 5 -c 100 -d 20 --latency http://192.168.1.110:9501/index.php 21595 QPS #與靜態文件差不多

大概是官方公佈的一半


增加Mysql+mysqli

增加一次sql查詢

server2+client1: wrk -t 5 -c 100 -d 20 --latency http://192.168.1.110/m.php 2034 QPS

增加一次sql插入

server2+client1: wrk -t 5 -c 100 -d 20 --latency http://192.168.1.110/m.php 2500 QPS

增加一次sql插入一次sql查詢

server2+client1: wrk -t 5 -c 100 -d 20 --latency http://192.168.1.110/m.php 2189 QPS


增加Redis

Redis安裝:https://www.cnblogs.com/rslai/p/8249812.html

增加一次redis插入和查詢

server2+client1: wrk -t 5 -c 100 -d 20 --latency http://192.168.1.110/r.php 2710 QPS

增加一次redis查詢

server2+client1: wrk -t 5 -c 100 -d 20 --latency http://192.168.1.110/r.php 3266 QPS

增加一次redis插入

server2+client1: wrk -t 5 -c 100 -d 20 --latency http://192.168.1.110/r.php 4051 QPS


更換golang環境

安裝:https://blog.csdn.net/bawenmao/article/details/80217055

yum install golang -y

package main
import( "net/http"
 "fmt")
func hello(w http.ResponseWriter, r *http.Request) {
 fmt.Fprintf(w, "hello world")
}
func main() {
 http.HandleFunc("/", hello);
 err := http.ListenAndServe(":8000", nil); if err != nil {
 }
}

go build s.go

./s

server2+client1: wrk -t 5 -c 100 -d 20 --latency http://192.168.1.110:8000/  21000 QPS

跟swoole差不多

從設計到實戰:Go 如何扛住100億次請求 https://studygolang.com/articles/9457




發錶評論
評論通過審核後顯示。
流量統計