locust性能测试:安装与使用(二)

2021-09-26  本文已影响0人  HC2

安装成功会输出版本号

(pressure_env) lxdeMacBook-Pro-2:api_pressure lx$ locust -V
locust 2.2.1
from locust import HttpUser,TaskSet,between,task
import os

headers = {"Content-Type": "application/json", "app_id": "101"}

class UserBehavior(TaskSet):

    @task
    def test_baidu(self):
        '''查询全部'''

        url = '/widget?id=LocalNews&ajax=json&t=1632650885696'
        param = {
            "limit":8,
            "offset":0,
        }
        with self.client.get(url,params=param,headers={},catch_response = True) as response:
            print("=====", response.json())

class WebsiteUser(HttpUser):
    host = 'http://news.baidu.com'
    tasks = [UserBehavior]

    wait_time = between(1, 2)

if __name__ == '__main__':
    os.system("locust -f bb.py")
image.png image.png

参数解析:

执行结果:

image.png

脚本解析:

1、 @task装饰器装饰的函数表示是要被测试的一个任务
2、 wait_time = between(1, 3)
来模拟用户的真实操作,例如用户在实际操作中,从上一步到下一步的操作,中间可能存在思考时间 ,between(1, 3) 表示用户思考的时间,随机为1到3秒

host = 'http://news.baidu.com' 为要测试的域名

也可以写为:

class WebsiteUser(HttpUser):
    # host = 'http://news.baidu.com'
    tasks = [UserBehavior]

    wait_time = between(1, 2)

if __name__ == '__main__':
    os.system("locust -f bb.py --host=http://news.baidu.com")

当两处都写时,以#2为主

class WebsiteUser(HttpUser):
    host = 'http://news.baidu.com222'   #1
    tasks = [UserBehavior]

    wait_time = between(1, 2)

if __name__ == '__main__':
    os.system("locust -f bb.py --host=http://news.baidu.com")  #2

***旧版本写法有点不同:

from locust import HttpLocust, TaskSet, task,events
class UserBehavior(TaskSet):

    @task
    def test_baidu(self):
        '''查询全部'''

        url = '/widget?id=LocalNews&ajax=json&t=1632650885696'
        param = {
            "limit":8,
            "offset":0,
        }
        with self.client.get(url,params=param,headers={},catch_response = True) as response:
            print("=====", response.json())


class WebsiteUser(HttpLocust):
    host = 'http://news.baidu.com/'
    task_set = UserBehavior
    min_wait = 1000
    max_wait = 1000
上一篇下一篇

猜你喜欢

热点阅读