美文网首页
locust 压测入门 - 2

locust 压测入门 - 2

作者: 击石小记 | 来源:发表于2019-12-15 18:01 被阅读0次

分布式使用

如果要使用多进程/分布式的形式来进行压测,启动时需要增加以下参数:

主:locust -f test.py --master --host=http://example.com
从:locust -f test.py --slave --host=http://example.com

如果想在多台机器上运行Locust,在启动slaves时,我们应该指定master地址(当运行Locust分布式在同一台机器时,master的默认地址是127.0.0.1):

locust -f test.py --slave --master-host=ip --host=http://example.

locust 测试程序的编写

以前一篇中的示例程序 test.py 为例

from locust import HttpLocust, TaskSet, task, between

class UserBehavior(TaskSet):
    def on_start(self):
        """ on_start is called when a Locust start before any task is scheduled """
        self.login()

    def on_stop(self):
        """ on_stop is called when the TaskSet is stopping """
        self.logout()

    def login(self):
        self.client.post("/login", {"username":"ellen_key", "password":"education"})

    def logout(self):
        self.client.post("/logout", {"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
    wait_time = between(5, 9)

主要是两个类,HttpLocust 和 TaskSet

  1. Locust 表示每个虚拟的用户。
    HttpLocust 是继承自 Locust 类, 且 client 绑定了自己的实现,如果不是http/https协议,我们需要继承Locust后实现自己的client。

  2. TaskSet 描述了每个locust 实例要运行的任务集,并提供了调度,包括执行顺序、权重等等。
    @task 装饰器表示权重属性,即index()的执行频率是profile()的两倍

自定义客户端测试非http/https的系统

官网说明文档

只需要编写一个触发request_success和request_failure事件的自定义客户端即可。

以一个xml-rpc协议为例

import time
import xmlrpclib

from locust import Locust, TaskSet, events, task, between


class XmlRpcClient(xmlrpclib.ServerProxy):
    """
    Simple, sample XML RPC client implementation that wraps xmlrpclib.ServerProxy and 
    fires locust events on request_success and request_failure, so that all requests 
    gets tracked in locust's statistics.
    """
    def __getattr__(self, name):
        func = xmlrpclib.ServerProxy.__getattr__(self, name)
        def wrapper(*args, **kwargs):
            start_time = time.time()
            try:
                result = func(*args, **kwargs)
            except xmlrpclib.Fault as e:
                total_time = int((time.time() - start_time) * 1000)
                ###########################
                ## 1. 实现 request_failure 事件
                ###########################
                events.request_failure.fire(request_type="xmlrpc", name=name, response_time=total_time, exception=e)
            else:
                total_time = int((time.time() - start_time) * 1000)
                ###########################
                ## 2. 实现 request_success 事件
                ###########################
                events.request_success.fire(request_type="xmlrpc", name=name, response_time=total_time, response_length=0)
                # In this example, I've hardcoded response_length=0. If we would want the response length to be 
                # reported correctly in the statistics, we would probably need to hook in at a lower level
        
        return wrapper


class XmlRpcLocust(Locust):
    """
    This is the abstract Locust class which should be subclassed. It provides an XML-RPC client
    that can be used to make XML-RPC requests that will be tracked in Locust's statistics.
    """
    def __init__(self, *args, **kwargs):
        super(XmlRpcLocust, self).__init__(*args, **kwargs)
        self.client = XmlRpcClient(self.host)


class ApiUser(XmlRpcLocust):
    
    host = "http://127.0.0.1:8877/"
    wait_time = between(0.1, 1)
    
    class task_set(TaskSet):
        @task(10)
        def get_time(self):
            self.client.get_time()
        
        @task(5)
        def get_random_number(self):
            self.client.get_random_number(0, 100)

在上面的例子中,关键之处,即自定义客户端的实现中,当调用完成时,通过 events.request_failure 和 events.request_success 事件进行数据上报,从何locust可以完成数据统计。

如果需要运行起来以上例子,可以查看官方文档获得svr端代码。

相关文章

  • locust 压测入门 - 2

    分布式使用 如果要使用多进程/分布式的形式来进行压测,启动时需要增加以下参数: 主:locust -f test....

  • locust 压测入门 - 1

    简介与功能 Locust 是基于Gevent协程实现,可以轻量高效地单机模拟较高并发请求。 主要功能: 基于Web...

  • locust压测

    Locust 介绍 它采用纯 Python 实现,是一个分布式用户负载测试的工具。 使用基于 Requests 库...

  • go实现的压测工具【单台机器100w连接压测实战】

    本文介绍压测是什么,解释压测的专属名词,教大家如何压测。介绍市面上的常见压测工具(ab、locust、Jmeter...

  • go 实现的压测工具,ab、locust、Jmeter压测工具介

    本文介绍压测是什么,解释压测的专属名词,教大家如何压测。介绍市面上的常见压测工具(ab、locust、Jmeter...

  • locust的核心类

    locust主要有两个特性:1.模拟用户操作locust支持多种协议,可以压测任意协议类型的系统 2.并发机制lo...

  • 基于web界面的locust性能测试

    前言 做为一个压测工具(库),[locust](http://locust.io/)其实解决这么一个问题:AB 之...

  • Locust + python + influxdb + gra

    需求 在使用locust压测的时候,如果使用web则可以查看到QPS压测过程的曲线图。而如果使用no web模式启...

  • locust(一) - 简介, 安装与运行

    简介 Locust 是一个开源压力测试工具. 与其他类似的压测工具相比, locust有几大优势: 使用代码定义用...

  • 【测试】Locust 并发压测

    由于最近产品用户量突增,爆发了很多技术上存在的问题,sql索引没建好,存在很多慢sql,缓存没做好等等。虽然都有解...

网友评论

      本文标题:locust 压测入门 - 2

      本文链接:https://www.haomeiwen.com/subject/lmxlnctx.html