Locust 使用(续更新)

warning: 这篇文章距离上次修改已过465天,其中的内容可能已经有所变动。

本文以Centos 7为例

先对准系统时间:ntpdate ntp.aliyun.com

1、部署Python环境

Python3.6.5 下载

上传至操作系统,并解压

1371745-20220225112836645-465646129.png1371745-20220225112836645-465646129.png

安装一些依赖包:

yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
yum -y install zlib*

切到解压目录进行编译安装到/usr/local/python36下:

mkdir /usr/local/python36 -p
./configure --prefix=/usr/local/python36  --with-ssl
make
make install

安装完成之后,执行环境变量:

[root@locust bin]# export PATH=/usr/local/python36/bin/:$PATH
[root@locust bin]# python3 -V
Python 3.6.5
[root@locust bin]# pip3 -V
pip 9.0.3 from /usr/local/python36/lib/python3.6/site-packages (python 3.6)

至此,Python环境安装完成。
2、安装Locust
在目录下创建一个目录workspace

[root@locust local]# pwd
/usr/local
[root@locust local]# mkdir workspace

进入目录,创建虚拟空间:

[root@locust local]# cd workspace/
[root@locust workspace]# python3 -m venv ceshi

进入虚拟环境ceshi:

[root@locust bin]# pwd
/usr/local/workspace/ceshi/bin
[root@locust bin]# source activate
(ceshi) [root@locust bin]# 

安装locust

(ceshi) [root@locust bin]# pip install locust

安装完成后,新建一个discuz_index.py脚本,如下:

from locust import HttpUser, TaskSet, task


class UserBehavior(TaskSet):

    @task(1)
    def baidu(self):
        self.client.get("/ip")    


class Discuz_Locust(HttpUser):
    tasks = [UserBehavior] #
    host = "http://httpbin.org"    //目标主机服务
    min_wait = 1000
    max_wait = 2000
[root@locust ceshi]# pwd
/usr/local/workspace/ceshi
[root@locust ceshi]# ll
total 8
drwxr-xr-x. 2 root root 204 May 25 15:32 bin
-rw-r--r--. 1 root root 303 May 25 15:33 discuz_index.py   //赋予执行权限 chmod +x discuz_index.py
drwxr-xr-x. 3 root root  18 May 25 15:32 include
drwxr-xr-x. 3 root root  23 May 25 15:24 lib
lrwxrwxrwx. 1 root root   3 May 25 15:24 lib64 -> lib
-rw-r--r--. 1 root root  84 May 25 15:24 pyvenv.cfg
[root@locust ceshi]# 

查看locust是否安装完成:

1371745-20220225112836645-465646129.png1371745-20220225112836645-465646129.png

运行:

(ceshi) [root@locust ceshi]# pwd
/usr/local/workspace/ceshi
(ceshi) [root@locust ceshi]# ll
total 12
drwxr-xr-x. 2 root root 218 May 25 15:42 bin
-rwxrwxrwx. 1 root root 303 May 25 15:33 discuz_index.py
drwxr-xr-x. 3 root root  18 May 25 15:32 include
drwxr-xr-x. 3 root root  23 May 25 15:24 lib
lrwxrwxrwx. 1 root root   3 May 25 15:24 lib64 -> lib
-rw-r--r--. 1 root root  61 May 25 15:38 pip-selfcheck.json
-rw-r--r--. 1 root root  84 May 25 15:24 pyvenv.cfg
(ceshi) [root@locust ceshi]# locust -f discuz_index.py

1371745-20220225112836645-465646129.png1371745-20220225112836645-465646129.png

添加新评论

选择表情