locust 压测入门 - 2

分布式使用

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

主: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)