1. 背景:互聯網發達的今天,大大小小的網站如雨後春筍,不斷出現,可是想要作出一個網站很簡單,可是想要作好一個網站,很是很是難,首先:網站作好以後的功能怎麼樣這都是次要的,主要的是你的網站能承受怎麼樣的訪問量,一個在高壓訪問下,能承受很高峯值的訪問併發才能稱得上是一個好的網站,那麼做爲一個程序員,當你搭建好你的網站以後,你應該怎麼測試你的網站併發訪問量呢?php
接下來要介紹的就是apache的ab命令壓測:mysql
2.在學習使用ab命令以前,首先要了解壓力測試的幾個概念:(本身能夠上網查下具體的概念)nginx
吞吐率(Requests per second)
概念:服務器併發處理能力的量化描述,單位是reqs/s,指的是某個併發用戶數下單位時間內處理的請求數。某個併發用戶數下單位時間內能處理的最大請求數,稱之爲最大吞吐率。
計算公式:總請求數 / 處理完成這些請求數所花費的時間,即
Request per second = Complete requests / Time taken for tests程序員
併發鏈接數(The number of concurrent connections)
概念:某個時刻服務器所接受的請求數目,簡單的講,就是一個會話。sql
併發用戶數(The number of concurrent users,Concurrency Level)
概念:要注意區分這個概念和併發鏈接數之間的區別,一個用戶可能同時會產生多個會話,也即鏈接數。數據庫
用戶平均請求等待時間(Time per request)
計算公式:處理完成全部請求數所花費的時間/ (總請求數 / 併發用戶數),即
Time per request = Time taken for tests /( Complete requests / Concurrency Level)apache
服務器平均請求等待時間(Time per request: across all concurrent requests)
計算公式:處理完成全部請求數所花費的時間 / 總請求數,即
Time taken for / testsComplete requests
能夠看到,它是吞吐率的倒數。
同時,它也=用戶平均請求等待時間/併發用戶數,即
Time per request / Concurrency Levelwindows
-T
參數。
-T
參數。
application/x-www-form-urlencoded
,默認值爲text/plain
。
ab -help
。
5.分析上面的壓測結果:api
Server Software: Apache/2.2.25 (服務器軟件名稱及版本信息)tomcat
Server Hostname: www.xxx.com(服務器主機名)
Server Port: 80 (服務器端口)
Document Path: /lol (供測試的URL路徑)
Document Length: 0 bytes (供測試的URL返回的文檔大小)
Concurrency Level: 100 (併發數)
Time taken for tests: 0.800 seconds (壓力測試消耗的總時間)
Complete requests: 100 (壓力測試的的總次數)
Failed requests: 0 (失敗的請求數)
Total transferred: 16342 bytes (傳輸的總數據量)
HTML transferred: 0 bytes (HTML文檔的總數據量)
Requests per second: 125.03 [#/sec] (mean) (平均每秒的請求數)
Time per request: 799.805 [ms] (mean) (全部併發用戶(這裏是100)都請求一次的平均時間)
Time per request: 7.998 [ms] (mean, across all concurrent requests) (單個用戶請求一次的平均時間)
Transfer rate: 19.95 [Kbytes/sec] received (傳輸速率,單位:KB/s)
在上面的測試中,咱們設置的壓力測試總次數以及併發數並無讓服務器感受到什麼「壓力」,如今咱們再來看一個「壓力山大」的執行命令:
ab -n 1000 -c 100 localhost/index.php
,這個時候apache就直接罷工——拒絕訪問了:
D:\study\Apache\bin>ab -n 100 -c100 http://www.xxx.com;
This is ApacheBench, Version 2.3 <$Revision: 1748469 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/
在上面的壓力測試中,Apache使用的是默認配置,並無通過任何優化措施處理。實際上,Apache在通過配置優化
後,只要服務器硬件夠用,Apache服務器是可以撐起1000的併發量的。
6.ab進行app接口的壓測:
ab -n 400 -c20 "http://www.xxx.com/api.php?sig=......";
將須要壓測的接口,用 " " ;
7.ab進行post傳參的壓測
ab -n 400 -c20 -p parm.txt -T "application/x-www-form-urlencoded" http://localhost:3000/login
將 parm.txt放在和ab.exe相同的文件夾中,parm.txt中存放的是須要post格式傳遞的參數。
-T :post請求的head頭。