Python之路

Locust学习总结

2017-11-10  本文已影响61人  忆江南_1569

简介

Locust是一个使用Python编写的可扩展、分布式的开源性能测试工具。

优点

安装

pip install locust

Locust脚本编写

import queue
from locust import HttpLocust, TaskSet, task
from locust.clients import HttpSession
from sign import ParserData


class UserBehavior(TaskSet):

    parser_data = ParserData()  # 解析接口传参类
    _client = 20
    version = 119
    user_info = None

    @staticmethod
    def get_user_info(response):
        r = response.json().get('content')
        return {
            'market_id': r.get('marketId'),
            'token': r.get('token')
        }

    def on_start(self):
        try:
            user, password = self.locust.users.get_nowait()
        except queue.Empty:
            print('test data run out. test ended.')
            exit(0)
        client = HttpSession(base_url='http://login.xxxx.cn')
        data = self.parser_data(loginName=user,
                                password=password,
                                client=self._client,
                                version=self.version)
        response = client.post(url='/login', data=data)
        self.user_info = self.get_user_info(response)
        self.locust.users.put_nowait((user, password))

    @task(2)
    def index(self):
        url = '/index'
        data = self.parser_data(market_id=self.user_info['market_id'],
                                client=self._client,
                                version=self.version,
                                pnum='3')
        headers = {'Authorization': 'Barer:' + self.user_info['token'],
                   'Accept': 'application/vnd.hs-api.v1+json'}
        with self.client.post(url=url, data=data, headers=headers,
                              verify=False, catch_response=True) as response:
            if response.status_code == 200:
                response.success()
            else:
                response.failure('http error.')

    @task(1)
    def shop_car_list(self):
        url = '/shopCar/list'
        data = self.parser_data(market_id=self.user_info['market_id'],
                                ischaidan='1',
                                client=self._client,
                                version=self.version)
        headers = {'Authorization': 'Bearer:' + self.user_info['token'],
                   'Accept': 'application/vnd.hs-api.v1+json'}
        with self.client.post(name='ShopCar', url=url, data=data, headers=headers,
                              verify=False, catch_response=True) as response:
            if response.status_code != 200 or "失败" in response.text:
                response.failure('response error.')
            else:
                response.success()


class Stay(TaskSet):

    index = 0

    def on_start(self):
        self.index += 1

    @task
    def get_error(self):
        response = self.client.get('/1', name='error', allow_redirects=False,
                                   verify=False, catch_response=True)
        if response.status_code == 200:
            response.success()
        else:
            response.failure('http error.')

    @task
    def logout(self):
        self.interrupt()


class User(TaskSet):

    tasks = {Stay: 1}

    @task(1)
    def user(self):
        self.client.get('/', verify=False)


class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    host = 'https://xxxx.api.xxxx.cn'
    min_wait = 1000
    max_wait = 3000
    users = queue.Queue()
    users.put_nowait(('user1', '1232'))
    users.put_nowait(('user2', '1234'))
    users.put_nowait(('user3', '1321'))
    weight = 3
    stop_timeout = 20


class WebsiteU(HttpLocust):
    task_set = User
    host = 'https://www.baidu.com'
    min_wait = 0
    max_wait = 0
    weight = 1
    stop_timeout = 60

简单解释下:

Taskset

HttpLocust

运行模式:

no-web模式

no_web模式指在命令行中直接运行

locust -f load_test.py -c 1 -r 1 -n 1

单机单进程运行

locust -f load_test.py

分布式运行

分布式运行,有单机多进程运行和多机多进程运行两种

locust -f load_test.py --master       master模式下启动locust
locust -f load_test.py --slave         启动一个locust slave节点,单机多进程模式
locust -f load_test.py --slave --master-host=192.168.105.11    启动一个locust slave节点,多机模式下 
no_web模式下运行
image.png
web模式
image.png

Exceptions显示测试过程中出现的异常
Download Date提供测试结果csv文件的下载

测试数据:

queue数据结构

队列形式

q=queue.Queue(maxsize=3)       先进先出队列
q.put(1)      向队列中存数据
q.get()        向队列中取数据
q.put_nowait()       相当于q.put(1, block=False),当q队列满了之后put会触发queue.Full异常
q.get_nowait()       相当与q.get(block=False),当q队列为空之后get会触发queue.Empty异常
上一篇下一篇

猜你喜欢

热点阅读