基于python的性能测试框架LOCUST系列一
简介
首先,这个框架是做性能测试的,然后,基于python编写!!!所以,不可能不研究研究啊。
LOCUST英文意思是“蝗虫”,感受感受使用locust进行性能测试,并发请求就像铺天盖地的蝗虫一样攻击你的系统,嗯,想想都可怕。
在Locust测试框架中,测试场景是由纯python脚本编写,对于http以及https协议,可以使用python的requests库作为客户端。对于其他协议,locust也提供有接口。也就是说,只要我们使用python编写对应的请求,就能方便的用locust进行压力测试。
安装
安装locust很简单,日常操作:1pip install locustio
demo示例
官方demo:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#!/usr/bin/env python3
# -*- coding: utf-8 -*-
“””
@author: kyle shi
@time: 2018/3/30/030 16:59
“””
from locust import HttpLocust, TaskSet, task
class UserBehavior(TaskSet):
def on_start(self):
“”” on_start is called when a Locust start before any task is scheduled “””
self.login()
def login(self):
self.client.post(“/login”, {“username”: “ellen_key”, “password”: “education”})
@task(2)
def index(self):
self.client.get(“/”)
@task(1)
def profile(self):
self.client.get(“/profile”)
class WebsiteUser(HttpLocust):
task_set = UserBehavior
host = ‘http://example.com’
min_wait = 5000
max_wait = 9000
cmd进入该文件所在路径下,执行locust命令,即可开启locust web服务,默认端口80891
2
3
4
5
6
7
8E:py_workspaceLocustTestcodingsTestScripts {git}
{lamb} ls
__init__.py __pycache__ locustfile.py
E:py_workspaceLocustTestcodingsTestScripts {git}
{lamb} locust
[2018-03-30 17:22:44,919] 3HET0MVY93LITXF/INFO/locust.main: Starting web monitor at *:8089
[2018-03-30 17:22:44,920] 3HET0MVY93LITXF/INFO/locust.main: Starting Locust 0.8.1
打开浏览器,输入url:http://localhost:8089即可访问locust web。
tips:
使用locust打开服务前提是脚本名称是locustfile.py;否则需要执行指定脚本的命令:
locust -f mylocustFile.py -P 7070指定文件和端口
demo简单分析
官方的demo对于http://example.com进行压测,随机访问首页(/)以及页面(/profile)比例为2:1,每次请求间隔为5~9s。
后记
locust现在还是刚刚开始接触,看着官方介绍的,功能相当齐全,尤其是我还是一个只有python编码能力的菜鸡。。。太对胃口了。。。好好研究!