locust 快速入门--多种启动方式

背景

  使用 locust进行压测时,通常使用带界面和不带界面两种启动方式。为了更直观的查看压测结果,常用带界面的方式。

  但每次运行要在控制台命令启动,比较麻烦。且pycharm没有类似flask、Django一样的配置一键启动。

多种启动方式尝试:

方式一:控制台命令启动

locust -f current_limiting_test.py --web-host 0.0.0.0 -P 8089

方式二:单用户模式

import os
from logging import getLogger

from locust import HttpUser
from locust import TaskSet
from locust import events
from locust import task
from locust.log import setup_logging

setup_logging("INFO", None)
logger = getLogger(os.path.basename(__file__))


class Behavior(TaskSet):
    def on_start(self):
        logger.info('开始访问活动')

    @task
    def index(self):
        with self.client.get("/", catch_response=True) as response:
            json_data = response.json()
            if json_data['code'] != 1:
                response.failure(f'自定义响应失败: code != 1')

            assert json_data['code'], f'响应异常信息: code != 1'

    def on_stop(self):
        logger.info('结束访问活动')


class MyUser(HttpUser):
    host = "https://docs.locust.io"
    tasks = [Behavior]

    def on_start(self):
        logger.info('用户开始行动')

    def on_stop(self):
        logger.info('用户结束行动')
        
if __name__ == '__main__':
    # 调试模式
    from locust import run_single_user
    run_single_user(MyUser)

方式三:使用os库

import os
from logging import getLogger

from locust import HttpUser
from locust import TaskSet
from locust import events
from locust import task
from locust.log import setup_logging

setup_logging("INFO", None)
logger = getLogger(os.path.basename(__file__))


class Behavior(TaskSet):
    def on_start(self):
        logger.info('开始访问活动')

    @task
    def index(self):
        with self.client.get("/", catch_response=True) as response:
            json_data = response.json()
            if json_data['code'] != 1:
                response.failure(f'自定义响应失败: code != 1')

            assert json_data['code'], f'响应异常信息: code != 1'

    def on_stop(self):
        logger.info('结束访问活动')


class MyUser(HttpUser):
    host = "https://docs.locust.io"
    tasks = [Behavior]

    def on_start(self):
        logger.info('用户开始行动')

    def on_stop(self):
        logger.info('用户结束行动')
        
if __name__ == '__main__':
    os.system(f"locust -f {os.path.basename(__file__)} ")

方式四:通过locust库方法 推荐

import os
from logging import getLogger

from locust import HttpUser
from locust import TaskSet
from locust import events
from locust import task
from locust.log import setup_logging

setup_logging("INFO", None)
logger = getLogger(os.path.basename(__file__))


class Behavior(TaskSet):
    def on_start(self):
        logger.info('开始访问活动')

    @task
    def index(self):
        with self.client.get("/", catch_response=True) as response:
            json_data = response.json()
            if json_data['code'] != 1:
                response.failure(f'自定义响应失败: code != 1')

            assert json_data['code'], f'响应异常信息: code != 1'

    def on_stop(self):
        logger.info('结束访问活动')


class MyUser(HttpUser):
    host = "https://docs.locust.io"
    tasks = [Behavior]

    def on_start(self):
        logger.info('用户开始行动')

    def on_stop(self):
        logger.info('用户结束行动')
        
if __name__ == '__main__':
	from locust import HttpUser, events
    from locust.env import Environment

    # setup Environment and Runner
    env = Environment(user_classes=[UserRun], events=events)
    runner = env.create_local_runner()
    # start a WebUI instance
    web_ui = env.create_web_ui("0.0.0.0", 8089)

    # execute init event handlers (only really needed if you have registered any)
    env.events.init.fire(environment=env, runner=runner, web_ui=web_ui)

    web_ui.greenlet.join()

你可能感兴趣的:(locust,测试,python)