先把nginx和memcached环境开启
/usr/local/nginx/sbin/nginx /usr/local/fastphp/sbin/php-fpm /usr/local/memcached/bin/memcached -u nobody -m 512 -d //后台运行
创建测试文件
vim /usr/local/nginx/html/bench1/index.php
内容如下
<?php $id = mt_rand(0 , 400); $sql = 'select page_id,page_post_time from pal_error_book_page where page_id='.$id; $mem = new memcache(); $mem->pconnect('localhost'); if (($com = $mem->get($sql)) === false) { $conn = mysql_connect('127.0.0.1' , 'root' , ''); mysql_query('use pal_studio' , $conn); mysql_query('set names utf8' , $conn); $rs = mysql_query($sql , $conn); $com = mysql_fetch_assoc($rs); $mem->add($sql , $com , false , 100); echo 'from mysql'; } print_r($com); ?>
在开始压力测试前,先创建文件记录mysql状态
cd /usr/local/nginx/html/bench1 vim tjstatus.sh //写脚本 touch status.txt //记录状态
tjstatus.sh内容为
while true do mysqladmin -uroot ext|awk '/Queries/{q=$4}/Threads_connected/{c=$4}/Threads_running/{r=$4}END{printf("%d % d %d ",q,c,r)}' >> status.txt sleep 1 done
现在开始进行压力测试
/usr/local/nginx/html/bench1/sh tjstatus.sh
/usr/local/apache/bin/ab -c 50 -n 20000 http://192.168.187.131/bench1/index.php
测完后记得关闭tjstatus.sh文件
然后即可在status.txt中看到状态值了
可以使用
awk '{q=$1-last;last=$1}END{printf("%d %d %d ",q,$2,$3)}'
将数据放入表格中绘制成图表观察周期