在linux安装locust才发现,locust已经更新到1.*了;导致以前用0.*版本写的脚本运行不了,要对应修改代码
locust更新版本导致运行脚本时提示类名称已变化,打开官方文档:https://docs.locust.io/en/latest/changelog.html#locust-class-renamed-to-user
非常明显的区别是:
HttpLocust类->HttpUser类
Locust类->User类
Locust的参数-c/-clients->-u /-users
没有task_set属性了,现在叫tasks,且用法为tasks=[MyTasks]
在没有Web UI的情况下运行Locust的选项已从更改--no-web
为--headless
在安装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.*版本脚本模版
#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
1.*脚本模版
#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