Locust安装教程与使用
官网地址:https://github.com/locustio/locust

如果是python3+以上的环境,需要下载locust项目源码进行安装

因Centos7.2环境已默认安装python2.7版本,所以需要升级python到版本python3
安装python3参考如下:
1)安装python3.6可能使用的依赖
yum -y install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel

2)到python官网找到下载路径, 用wget下载
wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz

3)解压tgz包
tar -zxvf Python-3.6.4.tgz

4)把python移到/usr/local下面
mv Python-3.6.4 /usr/local/

5)删除旧版本的python依赖

ll /usr/bin | grep python
rm -rf /usr/bin/python

6)进入python目录
cd /usr/local/Python-3.6.4/

7)配置
./configure

8)编译,安装
make && make install

9)删除旧的软链接,创建新的软链接到最新的python
#rm -rf /usr/bin/python
ln -s /usr/local/bin/python3.6 /usr/bin/python

python -V


安装locust参考如下:

#进入安装包存放目录下
cd /usr/local/src

#下载locust
git clone https://github.com/locustio/locust.git

#将locust放到 /usr/local/ 目录下
cp -Rf locust/ /usr/local/

#进入locust目录开始安装
python setup.py install

locust压测步骤说明
1)编写压测脚本,为python脚本文件
2)启动locust服务,并设置启用压测的python脚本文件及hosts信息
3)设置locust访问本地网络及压测参数信息

压测脚本和启动脚本在同一目录
1-压测脚本: locust_test.py
from locust import HttpLocust,TaskSet,task

class UserBehavior(TaskSet):
@task()
def test(self):
self.client.get("/homepage/list_header.html")

class WebUserLocust(HttpLocust):
weight = 1
task_set = UserBehavior
min_wait = 300
max_wait = 700

2-启动脚本: start_locust.py
import os,re,sys,subprocess,webbrowser

def replace_url(url):
dir_path = os.path.dirname(os.path.abspath(__file__))
locust_file_path = os.path.join(dir_path,"locust_test.py")
with open(locust_file_path,"r") as locust_file:
content = locust_file.read()
searchObj = re.search( r'\"(.*)*\"', content, re.M|re.I)
content = content.replace(searchObj.group(),"\""+url+"\"")
open(locust_file_path,"w").write(content)

if __name__ == '__main__':
replace_url(sys.argv[2])

#启动压测时有界面
subprocess.Popen('locust -f .\locust_test.py --host='+sys.argv[1], shell=True)
webbrowser.open("http://localhost:8089/")

#启动压测时无界面
#subprocess.Popen('locust -f .\locust_test.py --host='+sys.argv[1]+' --no-web -c '+sys.argv[3]+' -r '+sys.argv[4]+' -n '+sys.argv[5], shell=True)

3-检测脚本: start.ps1

Write-Host "请输入压测网站host路径(例如:http://api.g.caipiao.163.com):"
$site = Read-Host

Write-Host "请输入压测网站url路径(例如:/homepage/list_header.html):"
$url = Read-Host

Write-Host "请输入模拟访问网站人数(例如:1000):"
$amount = Read-Host

Write-Host "请输入模拟每秒并发访问网站人数(不得小于模拟访问网站人数,例如:100):"
$conc = Read-Host

Write-Host "请输入模拟访问请求总数(例如:100000):"
$reqs = Read-Host

python .\start_locust_implicit.py $site $url $amount $conc $reqs
Read-Host

双击该脚本发起压测: start.ps1

posted on 2018-06-17 14:24  新美好时代  阅读(626)  评论(0编辑  收藏  举报