性能测试Locust--(9)使用定制的客户端测试其他系统

使用定制的客户端测试其他系统

以HTTP为主要目标构建Locust。但是,通过编写触发request_successrequest_failure事件的自定义客户端,可以很容易的将其扩展,用来对任何基于request/response的系统进行负载测试。

Locust client示例–XML-RPC

以下是Locust类XmlRpcLocust的示例,该类提供XML-RPC客户端XmlRpcClient并跟踪所有发出的请求:

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)
                events.request_failure.fire(request_type="xmlrpc", name=name, response_time=total_time, exception=e)
            else:
                total_time = int((time.time() - start_time) * 1000)
                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)

如果你以前编写过 Locust的测试,你应该知道一个名为 ApiUser 的类,它是一个普通的 Locust 类,它的 task_set属性是一个 TaskSet 类的子类,而这个子类带有多个 task

然而,ApiUser 继承自 XmlRpcLocust,您可以在 ApiUser 的正上方看到它。XmlRpcLocust 类在 client 属性下提供 XmlRpcClient 的实例。XmlRpcClient 是标准库的 xmlrclib. serverproxy的装饰器。它基本上只是代理函数调用,但是添加了触发用于将所有调用报告给 Locust 统计数据的 locust.events.request_successlocust.events.request_failure的重要功能。

下面是 XML-RPC 服务器的实现,它可以作为上述代码的服务器:

import random
import time
from SimpleXMLRPCServer import SimpleXMLRPCServer


def get_time():
    time.sleep(random.random())
    return time.time()

def get_random_number(low, high):
    time.sleep(random.random())
    return random.randint(low, high)

server = SimpleXMLRPCServer(("localhost", 8877))
print("Listening on port 8877...")
server.register_function(get_time, "get_time")
server.register_function(get_random_number, "get_random_number")

你可能感兴趣的:(Locust)