STEP1: http_load压测 (http_load的使用方法略)
如:http_load.exe -fetches 500 -parallel 50 urls.txt
STEP2: 压测结果入数据库
CREATE TABLE `t_p50f50qqcom` ( `id` int(11) NOT NULL AUTO_INCREMENT, `time` datetime NOT NULL, `mean` float NOT NULL, `max` float NOT NULL, `min` float NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=104 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; -- ---------------------------- -- Table structure for t_table_perporty -- ---------------------------- CREATE TABLE `t_table_perporty` ( `f_tid` int(11) NOT NULL, `f_status` tinyint(3) unsigned zerofill NOT NULL DEFAULT '000' COMMENT '0:valid 1:....', `f_table_name` varchar(64) COLLATE utf8_bin NOT NULL, `f_value_unit` varchar(16) COLLATE utf8_bin DEFAULT NULL, `f_table_note` varchar(128) COLLATE utf8_bin DEFAULT NULL, PRIMARY KEY (`f_tid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
以下代码获取一轮压测数据并提取出来每个请求平均时间,最大和最小时间并入库
#! D:\Python26\python.exe # -*- coding:utf8 -*- import os import re import sys import time import datetime import subprocess #http_load -p 100 -s 10 strTableName = "t_QQ_Req2Resp" def dumpValue2Sql(dInput): print dInput strTableName = dInput["table"] del dInput["table"] strSql = "INSERT INTO %s (%s,%s,%s,%s) VALUES('%s',%f,%f,%f); " % (strTableName,"time","max","min","mean",\ dInput["time"],dInput["max"],dInput["min"],dInput["mean"]) print strSql os.system('''mysql.exe -uroot perftest -s -e "%s"''' % strSql) def resolveHttpRet(strRet): ''' 50 fetches, 50 max parallel, 2.74322e+07 bytes, in 26.7117 seconds 548643 mean bytes/connection 1.87184 fetches/sec, 1.02697e+06 bytes/sec msecs/connect: 46.123 mean, 82.005 max, 9.001 min msecs/first-response: 129.828 mean, 3047.2 max, 14.001 min HTTP response codes: code 200 -- 50 ''' lRet = strRet.strip().replace(",", "").split("\n")[3].split() dRet = {lRet[2]:float(lRet[1]), lRet[4]:float(lRet[3]), lRet[6]:float(lRet[5])} dRet["table"] = "t_p50f50qqcom" dRet["time"] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") return dRet def doReq(dParam): strCommand = "http_load.exe " if dParam["rate"] != 0: strCommand += " -rate %d " % dParam["rate"] if dParam["fetches"] != 0: strCommand += " -fetches %d " % dParam["fetches"] if dParam["seconds"] != 0: strCommand += " -seconds %d " % dParam["seconds"] if dParam["parallel"] != 0: strCommand += " -parallel %d " % dParam["parallel"] strCommand += " urls.txt" print strCommand oP = subprocess.Popen(strCommand, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE) strRet = oP.stdout.read() return resolveHttpRet(strRet) if __name__ == "__main__": print "This is test web http by http_load, with mysql and python" #strCommand = "http_load -p 5 -f 1000 urls.txt" #os.system(strCommand) dParam = {"parallel":50, "fetches":50, "rate":0, "seconds":0, "table":"t_p50f50qqcom"} dRet = doReq(dParam) dumpValue2Sql(dRet) for i in range(0,100): dRet = doReq(dParam) dumpValue2Sql(dRet) time.sleep(20)
STEP3: php展现
<?php //content="text/plain; charset=utf-8" require_once ('src/jpgraph.php'); require_once ('src/jpgraph_line.php'); $hostname = "localhost"; $user = "perftest"; $password = "perftest"; mysql_connect($hostname, $user, $password) or die ("Database Failed"); mysql_select_db('perftest') or die("Database Error"); $sql = "select * from t_p50f50qqcom where time like '2014-05-03 %'"; $result = mysql_query($sql) or die("Select Error"); //输入的数据 $ydata = array(); while($row = mysql_fetch_row($result)) { array_push($ydata, $row[2]); } mysql_free_result($result); //创建图形 $graph = new Graph(800,500); $graph->SetScale('textlin'); //创建折线图 $lineplot=new LinePlot($ydata); $lineplot->SetColor('blue'); //在图上创建测量点 $graph->Add($lineplot); //显示图形 $graph->Stroke(); mysql_close(); ?>
效果:(如下图波动较大,是因为每轮测试时间只有50秒,可尝试加长时间,并在展现前清理一些异常数据)