服务器web性能测试之Locust工具使用

本文介绍了一款基于Python的开源负载测试工具Locust。它允许用户通过编写简单的Python脚本来模拟大量用户并发访问,从而测试系统的负载能力。Locust支持分布式部署,能够轻松扩展到成千上万个并发用户。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自:https://blog.youkuaiyun.com/wik_123/article/details/53180431

参考:http://blog.timd.cn/python-locust/

分布式:  https://blog.youkuaiyun.com/zhengshaolong8125/article/details/78111999


简介

Locust 是一个开源负载测试工具。使用 Python 代码定义用户行为,也可以仿真百万个用户。

Locust 是非常简单易用,分布式,用户负载测试工具。Locust主要为网站或者其他系统进行负载测试,能测试出一个系统可以并发处理多少用户。

Locust 是完全基于时间的,因此单个机器支持几千个并发用户。相比其他许多事件驱动的应用,Locust 不使用回调,而是使用对协程支持比较完善的gevent(基于IO切换)。

特点

  • 完全使用纯Python代码编写用户测试场景,不需要任何配置文件。
  • 分布式&可伸缩 - 支持成千上万的用户
  • 基于webui(自带)
  • 可以测试任意系统

安装

pip install locustio

或者

easy_install locustio
  • 1
  • 2
  • 3
  • 4
  • 5

检查是否安装成功;可以通过“locust –help”来检测。

文档说明

官方文档:http://docs.locust.io/en/latest/installation.html

简单示例

test.py

from locust import web, HttpLocust, TaskSet, events,task
import random, traceback

# 增加新的web界面
@web.app.route("/hi")
def my_added_page():
        return "hello locust..."


# 钩子函数
def on_request_success(request_type, name, response_time, response_length):
    print 'Type: %s, Name: %s, Time: %fms, Response Length: %d' % \
            (request_type, name, response_time, response_length)

def on_request_failure(request_type, name, response_time, exception):
    print 'Type: %s, Name: %s, Time: %fms, Reason: %r' % \
            (request_type, name, response_time, exception)

events.request_success += on_request_success
events.request_failure += on_request_failure

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):
        with self.client.post("/login", {"username":"ellen_key", "password":"education"}, catch_response = True) as r:
        if random.choice([0, 1]):
                r.success()
        else:
            r.failure('0')

    @task(2)
    def index(self):
        self.client.get("/")

    @task(1)
    def profile(self):
        self.client.get("/profile")

class WebsiteUser(HttpLocust):
    # 指向一个TaskSet类,TaskSet类定义了每个用户的行为
    task_set = UserBehavior
    # 请求等待最小时间min_wait和请求等待最大时间max_wait
    min_wait = 5000
    max_wait = 9000
    host = http://127.0.0.1
  • 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
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

单机

终端输入:

locust -f test.py
  • 1

webui访问地址:localhost:8089

ui界面需要填写两个参数:

number of users to simulate:模拟用户的数量

Hatch rate (users spawned/second):表示产生模拟用户的速度,每秒启动多少个用户

分布式

master机终端输入:

locust -f test.py --master --master-port=8888(默认的是8080端口)
  • 1

salve机终端输入:

locust -f test.py --slave --master-host=<master-IP> --master-bind-host=8888


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值