Locust1.*版本主要变化以及脚本使用模版
程序员文章站
2022-06-03 15:50:20
...
已经有两个多月没有使用locust,今天拿出来发现在虚拟环境重新安装了一下,因为使用的是pip install locust 默认安装的是最新(1.1.1)的版本,但之前的脚本是基于0.*版本使用的,导致提示类名称已变化,打开官方文档https://docs.locust.io/en/latest/changelog.html#locust-class-renamed-to-user 查看在1.0,1.0.1中间有重大变化
在安装1.*版本后使用原先脚本启动会报错 ImportError: The HttpLocust class has been renamed to HttpUser in version 1.0. For more info see: https://docs.locust.io/en/latest/changelog.html#changelog-1-0
修改导入名称后启动报错:
if issubclass(task_set, TaskSet) and not hasattr(task_set, “locust_task_weight”):
TypeError: issubclass() arg 1 must be a class
后发现是因为没有修改task_set为tasks(太粗心了????)
故修改后,记录下:
0.*版本脚本模版
// An highlighted block
#locust 0.*版本使用
from locust import HttpLocust, TaskSet, task
class WebsiteTasks(TaskSet):
def on_start(self):
self.header = {
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, '
'like Gecko) Version/11.0 Mobile/15A372 Safari/604.1'}
self.cookies_test = 'cookies'
return self.on_start
@task(100)
def Course_detial(self):
url = 'pack
datas = {"params": 111}
r = self.client.get(url, params=self.locust.check_sing(datas), cookies=self.cookies_test, name='测试')
x = r.content.decode()
return x
class WebsiteUser(HttpLocust):
host = 'https://www.baidu.com'
task_set = WebsiteTasks
min_wait = 5000
max_wait = 15000
def check_sing(self):
pass
1.*脚本模版
// An highlighted block
#1.*版本使用,重点是将HttpLocust替换为Httpuser,task_set任务集需要数据类型为列表类型,且task_set需要修改为tasks
from locust import HttpUser, TaskSet, task
class UserTasks(TaskSet):
def on_start(self):
self.header = {
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, '
'like Gecko) Version/11.0 Mobile/15A372 Safari/604.1'}
self.cookies_test = 'cookies'
return self.on_start
@task(100)
def Course_detial(self):
url = 'pack
datas = {"params": 111}
r = self.client.get(url, params=self.locust.check_sing(datas), cookies=self.cookies_test, name='测试')
x = r.content.decode()
return x
class WebUser(HttpUser):
host = 'https://www.baidu.com'
tasks = [UserTasks]
min_wait = 5000
max_wait = 15000
def check_sing(self):
pass