性能测试框架Locust初学笔记


Locust初探

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

Locust安装

这个框架基于python,故而需要先安装python,这里不再赘述。

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

执行压力测试

重新打开cmd窗口进入脚本文件的目录之中,执行以下命令开启压力测试

locust -f locustfile.py

执行完上面命令后用浏览打开http://127.0.0.1:8089,出现以下界面:

  我这里尝试模拟100个虚拟用户,每秒发送30次请求,然后点击Start swarming开始进行压力测试

测试结果

locust的运行界面,可以下载当前的运行结果到本地,但是这两份csv中的测试结果不够详细,不利于分析系统的瓶颈,这也是locust的不足之处。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM