袁与张 - 网站压测
type: channel
link: https://ytyzhn.top/tag/%E7%BD%91%E7%AB%99%E5%8E%8B%E6%B5%8B/
items: Locust 使用(续更新)

Locust 使用(续更新)
type: item
date: 2023-02-09T16:54:00+08:00
description: 本文以Centos 7为例先对准系统时间：ntpdate ntp.aliyun.com1、部署Python环境Python3.6.5 下载上传至操作系统,并解压安装一些依赖包： 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是否安装完成：运行： (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…