2025年6月1日 星期日 乙巳(蛇)年 三月初五 夜 设为首页 加入收藏
rss

web网站怎么做压力测试

时间:10-01来源:作者:点击数:

Web网站性能体现在并发用户数已经网站的吞吐量和时延。

最简单的压力测试工具就是ab "Apache Benchmark"

下面将介绍ab的安装和使用:

1. ab的安装

ab的安装非常简单,安装了httpd,就自带ab。

CentOS下的安装:

  • yum install -y httpd

Ubuntu下的安装:

  • apt-get install apache2

安装好后,就可以使用ab了。

2. ab使用前的准备工作

由于ab是压力测试工具,我们需要把允许打开文件的数量调大。可以修改/etc/security/limits.conf

  • echo "* soft nofile 65535" >> /etc/security/limits.conf;echo "* hard nofile 65535" >> /etc/security/limits.conf

更改了这个配置后,需要重新启动VM。

或者采用临时命令:

  •  ulimit -n 65535

这样更改不需要重启机器。

3. 运行ab,或wrk

网上有很多ab和wrk使用的介绍,这里就不细讲了。一般打流量的命令是:

  •  ab -c 5000 -r -n 40000000 http://xx.xx.xx.xx/

其中-c表示并发连接数;-n表示总共的请求数量;-r表示出现错误不退出,这个参数可以保证测试的连续性。注意http链接最后一定要是"/",否则报错。

  • wrk -t1 -c6000 -d2000s http://x.x.x.x/

4. 脚本

为了保证测试的连续性,下面的小脚本可以检测ab测试程序是否在运行。如果有,表示正在测试,等待10s,继续检测;如果没有,表示ab已经测试结束,需要重新启动。具体脚本如下:

  • #!/bin/bash
  •   while true
  •   do
  •    flag=`ps -ef | grep "ab -c" | grep -v grep| wc -l`
  •    [ "$flag" = 1 ] && continue || ab -c 5000 -r -n 40000000 http://x.x.x.x/
  •    sleep 10
  •   done

wrk的脚本如下:

  • #!/bin/bash
  •   while true
  •   do
  •   flag=`ps -ef | grep "wrk -t" | grep -v grep| wc -l`
  •   [ "$flag" = 1 ] && continue || /root/wrk-master/wrk -t1 -c6000 -d2000s http://x.x.x.x/
  •   sleep 10
  •   done

把此脚本后台运行。

5. 时延测试

在服务器压力上去后,服务器的带宽和时延的检测成为检测服务器能力的指标之一。

带宽的检测,可以采用nload、Zabbix等工具。

时延的检测,也可以采用Zabbix等带外方式。本文采用的是比较简单的time工具。

由于time和一般的Shell命令不同,grep、awk等语句不能正常的抓取执行结果。我们一般采用把执行结果输出到文件,再检索文件的方式。并且time的重定向是stderr的定向。具体命令是:

  •  (time -p curl http://x.x.x.x > /dev/null) &>> out.txt

然后再采用grep real out.txt来获取时延。

6. 时延脚本

可以通过下面的脚本收集时延:

  •  #!/bin/bash
  •   while true
  •    do
  •    (time -p curl http://x.x.x.x > /dev/null) &> out.txt
  •    grep real out.txt | awk '{print $0 "\t" strftime()}' >> delay.txt
  •    sleep 10
  •   done

7. 服务器端查看同时在线人数的命令

netstat -n | awk '/^tcp/ {++state[$NF]} END {for(key in state) print key,"t",state[key]}'

结果如下:

  •  TIME_WAIT t 32502
  •   FIN_WAIT1 t 4171
  •   FIN_WAIT2 t 267
  •   ESTABLISHED t 993
  •   SYN_RECV t 610
  •   CLOSING t 274
  •   LAST_ACK t 1

或者用下面的命令:

  • netstat -s | grep "connections established"
  •   4288 connections established

8. 通过curl统计时延插入数据库

  •  #!/bin/bash
  •   while true
  •   do
  •    (time -p curl http://x.x.x.x > /dev/null) &> out.txt
  •    grep real out.txt | awk '{print $0 "\t" strftime()}' >> delay.txt
  •    time=$(TZ=Asia/Shanghai date "+%Y-%m-%d %H:%M:%S")
  •    webDelay=`grep real out.txt | awk '{print $2}'`
  •    hostn=`hostname`
  •    mysql -hxxxx -uxxxx -pxxxx -e "use delay; insert into delay2 (delay, time, host) values($webDelay,'$time','$hostn');"
  •   sleep 20
  •   done

 8.1 通过python脚本把时延插入数据库

  •  #!/usr/bin/env python
  •   import StringIO
  •   import pycurl
  •   import sys
  •   import os
  •   import time
  •   import MySQLdb
  •   import socket
  •   def getHTTPTime(url):
  •    crl = pycurl.Curl()
  •    #crl.setopt(pycurl.VERBOSE,1)
  •    crl.setopt(pycurl.FOLLOWLOCATION, 1)
  •    crl.setopt(pycurl.MAXREDIRS, 5)
  •    crl.fp = StringIO.StringIO()
  •    crl.setopt(pycurl.URL, url)
  •    crl.setopt(crl.WRITEFUNCTION, crl.fp.write)
  •    p = crl.perform()
  •    time = crl.getinfo(pycurl.TOTAL_TIME)
  •    return time
  •   url = "http://xx.xx.xx.xx/"
  •   myhost = socket.gethostname()
  •   host="xxx.mysqldb.chinacloudapi.cn"
  •   user="xxx"
  •   passwd="xxxxxx"
  •   db="delay"
  •   port=3306
  •   try:
  •    conn=MySQLdb.connect(host=host,user=user,passwd=passwd,port=port)
  •    conn.select_db(db)
  •    conn.select_db(db)
  •    cur=conn.cursor()
  •    while True:
  •    curtime=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
  •    mytime = float(getHTTPTime(url))
  •    T = []
  •    T.append(mytime)
  •    T.append(curtime)
  •    T.append(myhost)
  •    cur.execute('insert into delay10 (delay, time, host) values(%s,%s,%s)',T);
  •    conn.commit()
  •    #aa=cur.execute('select id,delay,time from delay10')
  •    #print aa
  •    #for data in cur.fetchall():
  •    # print data
  •    time.sleep(60)
  •   except MySQLdb.Error,e:
  •    print "Mysql Error %d: %s" % (e.args[0], e.args[1])

9. 通过paping检测时延,并插入数据库

  • #!/bin/bash
  •   while true
  •   do
  •    getDelay=`/root/paping -p 80 -c 1 x.x.x.x | grep time= | awk '{print $4}' | awk 'BEGIN{FS="="} {print $2}'`
  •    time=$(TZ=Asia/Shanghai date "+%Y-%m-%d %H:%M:%S")
  •    #webDelay=`echo ${a%ms*}`
  •    [ $getDelay ] && webDelay=$getDelay || webDelay=1000
  •    hostn=`hostname`
  •    mysql -hhwdelay.mysqldb.chinacloudapi.cn -uhwdelay%hengwei -pabc@123456 -e "use delay; insert into delay4 (delay, time, host) values('$webDelay','$time','$hostn');"
  •   sleep 20
  •   done

10. php页面反应实际时延情况:

  •  <?php
  •    echo "xxx时延测试</br>";
  •    echo "大于1秒钟时延的统计";
  •    $mysql_server_name="xxxxxx"; //数据库服务器名称
  •    $mysql_username="xxxxx"; // 连接数据库用户名
  •    $mysql_password="xxxxx"; // 连接数据库密码
  •    $mysql_database="delay"; // 数据库的名字
  •    // 连接到数据库
  •    $conn=mysql_connect($mysql_server_name, $mysql_username,
  •    $mysql_password);
  •    // 从表中提取信息的sql语句
  •    $strsql="select * from delay2 where delay > 1 order by id desc";
  •    // 执行sql查询
  •    $result=mysql_db_query($mysql_database, $strsql, $conn);
  •    // 获取查询结果
  •    $row=mysql_fetch_row($result);
  •    echo '<font face="verdana">';
  •    echo '<table border="1" cellpadding="1" cellspacing="2">';
  •    // 显示字段名称
  •    echo "</b><tr></b>";
  •    for ($i=0; $i<mysql_num_fields($result); $i++)
  •    {
  •    echo '<td bgcolor="#FF0000"><b>'.
  •    mysql_field_name($result, $i);
  •    echo "</b></td></b>";
  •    }
  •    echo "</tr></b>";
  •    // 定位到第一条记录
  •    mysql_data_seek($result, 0);
  •    // 循环取出记录
  •    while ($row=mysql_fetch_row($result))
  •    {
  •    echo "<tr></b>";
  •    for ($i=0; $i<mysql_num_fields($result); $i++ )
  •    {
  •    echo '<td bgcolor="#00FF00">';
  •    echo $row[$i];
  •    echo '</td>';
  •    }
  •    echo "</tr></b>";
  •    }
  •    echo "</table></b>";
  •    echo "</font>";
  •    // 释放资源
  •    mysql_free_result($result);
  •    // 关闭连接
  •    mysql_close($conn);
  •   ?>

11. 多监测点合并网页:

  • <!DOCTYPE html>
  •   <html>
  •    <head>
  •    <title>HWTEST.html</title>
  •   
  •    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"/>
  •    <meta http-equiv="description" content="this is my page"/>
  •    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
  •    <link rel="stylesheet" type="text/css" href="./test.css">
  •    </head>
  •    <body>
  •    <!-- Logo div -->
  •    <div class="logo">
  •    <h1>xxx时延测试比较页面</h1>
  •    </div>
  •    <!-- Adv div -->
  •    <div class="ad">
  •    <div class="stuad">
  •    <h4>Azure上海VM检测Azure北京VM时延</br></h4>
  •    <h4>大于1秒钟时延的统计</h4>
  •    <?php
  •    $mysql_server_name="xxx.chinacloudapi.cn"; //数据库服务器名称
  •    $mysql_username="xxx"; // 连接数据库用户名
  •    $mysql_password="xxx"; // 连接数据库密码
  •    $mysql_database="delay"; // 数据库的名字
  •    // 连接到数据库
  •    $conn=mysql_connect($mysql_server_name, $mysql_username,
  •    $mysql_password);
  •    // 从表中提取信息的sql语句
  •    $strsql="select id,delay,time from delay2 where delay > 1 order by id desc";
  •    //$strsql="select * from delay2 where delay > 1 order by id desc";
  •    // 执行sql查询
  •    $result=mysql_db_query($mysql_database, $strsql, $conn);
  •    // 获取查询结果
  •    $row=mysql_fetch_row($result);
  •    echo '<font face="verdana">';
  •   // echo '<table border="1" cellpadding="1" cellspacing="2">';
  •    echo '<table border="1" cellpadding="1" cellspacing="2" align="center">';
  •    // 显示字段名称
  •    echo "</b><tr></b>";
  •    for ($i=0; $i<mysql_num_fields($result); $i++)
  •    {
  •    echo '<td bgcolor="#FF0000"><b>'.
  •    mysql_field_name($result, $i);
  •    echo "</b></td></b>";
  •    }
  •    echo "</tr></b>";
  •    // 定位到第一条记录
  •    mysql_data_seek($result, 0);
  •    // 循环取出记录
  •    while ($row=mysql_fetch_row($result))
  •    {
  •    echo "<tr></b>";
  •    for ($i=0; $i<mysql_num_fields($result); $i++ )
  •    {
  •    echo '<td bgcolor="#00FF00">';
  •    echo $row[$i];
  •    echo '</td>';
  •    }
  •    echo "</tr></b>";
  •    }
  •    echo "</table></b>";
  •    echo "</font>";
  •    // 释放资源
  •    mysql_free_result($result);
  •    // 关闭连接
  •    mysql_close($conn);
  •    ?>
  •    </div>
  •    <div class="ad2">
  •    <h4>家中采用curl检测页面时延</br></h4>
  •    <h4>大于1秒钟时延的统计</h4>
  •    <?php
  •    $mysql_server_name="xxx.chinacloudapi.cn"; //数据库服务器名称
  •    $mysql_username="xxx"; // 连接数据库用户名
  •    $mysql_password="xxx"; // 连接数据库密码
  •    $mysql_database="delay"; // 数据库的名字
  •    // 连接到数据库
  •    $conn=mysql_connect($mysql_server_name, $mysql_username,
  •    $mysql_password);
  •   
  •    // 从表中提取信息的sql语句
  •    $strsql="select id,delay,time from delay3 where delay > 1 order by id desc";
  •    // 执行sql查询
  •    $result=mysql_db_query($mysql_database, $strsql, $conn);
  •    // 获取查询结果
  •    $row=mysql_fetch_row($result);
  •    echo '<font face="verdana">';
  •   // echo '<table border="1" cellpadding="1" cellspacing="2">';
  •    echo '<table border="1" cellpadding="1" cellspacing="2" align="center" >';
  •    // 显示字段名称
  •    echo "</b><tr></b>";
  •    for ($i=0; $i<mysql_num_fields($result); $i++)
  •    {
  •    echo '<td bgcolor="#FF0000"><b>'.
  •    mysql_field_name($result, $i);
  •    echo "</b></td></b>";
  •    }
  •    echo "</tr></b>";
  •    // 定位到第一条记录
  •    mysql_data_seek($result, 0);
  •    // 循环取出记录
  •    while ($row=mysql_fetch_row($result))
  •    {
  •    echo "<tr></b>";
  •    for ($i=0; $i<mysql_num_fields($result); $i++ )
  •    {
  •    echo '<td bgcolor="#00FF00">';
  •    echo $row[$i];
  •    echo '</td>';
  •    }
  •    echo "</tr></b>";
  •    }
  •    echo "</table></b>";
  •    echo "</font>";
  •    // 释放资源
  •    mysql_free_result($result);
  •    // 关闭连接
  •    mysql_close($conn);
  •    ?>
  •   
  •    </div>
  •    <div class="house">
  •    <h4>家中采用paping测试</br></h4>
  •    <h4>大于100ms时延的统计</h4>
  •    <?php
  •    $mysql_server_name="xxx.chinacloudapi.cn"; //数据库服务器名称
  •    $mysql_username="xxx"; // 连接数据库用户名
  •    $mysql_password="xxx"; // 连接数据库密码
  •    $mysql_database="delay"; // 数据库的名字
  •    // 连接到数据库
  •    $conn=mysql_connect($mysql_server_name, $mysql_username,
  •    $mysql_password);
  •   
  •    // 从表中提取信息的sql语句
  •    $strsql="select id,delay,time from delay4 where delay > 100 order by id desc";
  •    // 执行sql查询
  •    $result=mysql_db_query($mysql_database, $strsql, $conn);
  •    // 获取查询结果
  •    $row=mysql_fetch_row($result);
  •    echo '<font face="verdana">';
  •   // echo '<table border="1" cellpadding="1" cellspacing="2">';
  •    echo '<table border="1" cellpadding="1" cellspacing="2" align="center" >';
  •    // 显示字段名称
  •    echo "</b><tr></b>";
  •    for ($i=0; $i<mysql_num_fields($result); $i++)
  •    {
  •    echo '<td bgcolor="#FF0000"><b>'.
  •    mysql_field_name($result, $i);
  •    echo "</b></td></b>";
  •    }
  •    echo "</tr></b>";
  •    // 定位到第一条记录
  •    mysql_data_seek($result, 0);
  •    // 循环取出记录
  •    while ($row=mysql_fetch_row($result))
  •    {
  •    echo "<tr></b>";
  •    for ($i=0; $i<mysql_num_fields($result); $i++ )
  •    {
  •    echo '<td bgcolor="#00FF00">';
  •    echo $row[$i];
  •    echo '</td>';
  •    }
  •    echo "</tr></b>";
  •    }
  •    echo "</table></b>";
  •    echo "</font>";
  •    // 释放资源
  •    mysql_free_result($result);
  •    // 关闭连接
  •    mysql_close($conn);
  •    ?>
  •    </div>
  •    </div>
  •   
  •    </body>
  •   </html>

CSS文件:

  • body{
  •   width:950px;
  •   margin: 0 auto;
  •   background-color: white;
  •   border: 1px solid red;
  •   font-size: 16px;
  •   }
  •   .logo{
  •   width: 950px;
  •   height: 68px;
  •   background-color: yellow;
  •   float: left;
  •   margin-top: 5px;
  •   text-align: center;
  •   }
  •   .ad{
  •   width: 950px;
  •   /*height: 212px;*/
  •   /*background-color: pink;*/
  •   margin-top: 5px;
  •   float: left;
  •   }
  •   .stuad{
  •   width: 310px;
  •   /*height: 196px;*/
  •   float: left;
  •   background-color: #ADFEDC;
  •   margin: 4px 0 0 4px;
  •   text-align: center;
  •   }
  •   .ad2{
  •   width: 310px;
  •   /*height: 196px;*/
  •   float: left;
  •   background-color: #D2A2CC;
  •   margin: 4px 0 0 4px;
  •   text-align: center;
  •   }
  •   .house{
  •   width: 310px;
  •   /*height: 196px;*/
  •   float: left;
  •   background-color: #FC7E8C;
  •   margin: 4px 0 0 4px;
  •   text-align: center;
  •   }

12. 检测程序是否运行的脚本

  • #!/bin/bash
  •   while true
  •   do
  •   cflag=`ps -ef | grep delay.sh | grep -v grep | wc -l`
  •   pflag=`ps -ef | grep mypachk.sh | grep -v grep | wc -l`
  •   echo $cflag $pflag
  •   #[ "$flag" = 1 ] && continue || /root/delay.sh &
  •   [ "$cflag" = 0 ] && /root/delay.sh || echo "curl is ok" >> record.txt
  •   [ "$pflag" = 0 ] && /root/mypachk.sh || echo "paping is ok" >> record.txt
  •   sleep 600
  •   done
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐