对测试的api引入locust后,不在使用requests
库进行http请求了,而是通过client属性发送请求,实质是使用HttpSession。
Gevent 兼容
兼容模式: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):
self.client.get("/")
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(UserRun)