Locust初学笔记2


Locust初探

Locust是一款类似于Jmeter开源负载测试工具,所不同的是它是用python实现,并支持python脚本。 locust提供web ui界面,能够方便用户实时监控脚本运行状态。

环境为:python3 版本;

安装:

pip install -U pyzmq

pip install -U locustio


实战

以公司的项目登录模块实战,直接上代码

复制代码
from locust import HttpLocust, TaskSet, task
import json


class UserBehavior(TaskSet):

    token = ''
    userId = ''
    headers = ''

    def login(self):
        data = {
            "email": "xxxxxxx",
            "passwd": "xxxxxxx",
        }
        headers = {
            'Content-Type': 'application/json',
            'Accept': 'application/json'}
        response = self.client.post(
            'xxxxxx/login',
            data=json.dumps(data),
            headers=headers)
        content = json.loads(response.content)
        self.token = {"token": content['data']['token']}
        self.userId = content['data']['userId']

    def logout(self):
        with self.client.get('xxxxxx/logout', params=self.token, catch_response=True) as response:
            if response.status_code != 200:
                response.failure()

    def user_details(self):
        data = {'userId': self.userId}
        with self.client.get('xxxxxxxx/view', params=data, headers=self.headers, catch_response=True) as response:
            if response.status_code != 200:
                response.failure()

    @task(10)
    def login_logout(self):
        self.login()
        self.user_details()
        self.logout()


class WebsiteUser(HttpLocust):
    host = 'http://10.1.51.221:7600/'
    task_set = UserBehavior
    min_wait = 5000
    max_wait = 9000

猜你喜欢

转载自blog.csdn.net/keny88888/article/details/81026986
今日推荐