性能測試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")
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章