自定義客戶端測驗其他系統
- 1、引言
- 2、實體展示
- 2.1 定義
- 2.2 客戶端代碼展示
- 2.3 服務器端代碼展示
1、引言
十一期間,看著朋友圈的各個大佬都是北京游,上海游,云南游,海南游等等,
我就不一樣了,github游,CSDN游,B站游,知乎游,掘金游等等…
反正,十一假期,我是沒閑著(開沒開車,不確定)…
我們今天來看看Locust如何自定義客戶端測驗系統的 ,
由于基礎內容,在第一章節,已經介紹完,
所以本章內容,就是實體展示!!
2、實體展示
2.1 定義
Locust以HTTP為主要目標構建,但是,通過撰寫觸發request_success 和request_failure事件的自定義客戶端,可以輕松擴展對任何基于請求/回應的系統進行負載測驗,
2.2 客戶端代碼展示
我們使用User類XmlRpcUser的示例,來看看XmlRpcUser如何發送請求,
老規矩,上代碼
# -*- coding: utf-8 -*-
"""
@ auth : carl_DJ
@ time : 2020-10-05
"""
import time
from xmlrpc.client import ServerProxy, Fault
from locust import User, task, between
class XmlRpcClient(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.
"""
_locust_environment = None
def __getattr__(self, name):
func = ServerProxy.__getattr__(self, name)
def wrapper(*args, **kwargs):
start_time = time.time()
try:
result = func(*args, **kwargs)
except Fault as e:
total_time = int((time.time() - start_time) * 1000)
self._locust_environment.events.request_failure.fire(
request_type="xmlrpc", name=name, response_time=total_time, exception=e
)
else:
total_time = int((time.time() - start_time) * 1000)
self._locust_environment.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 XmlRpcUser(User):
"""
This is the abstract User 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.
"""
abstract = True
def __init__(self, *args, **kwargs):
super(XmlRpcUser, self).__init__(*args, **kwargs)
self.client = XmlRpcClient(self.host)
self.client._locust_environment = self.environment
class ApiUser(XmlRpcUser):
host = "http://127.0.0.1:8877/"
wait_time = between(0.1, 1)
@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的類,這是一個普通的類,只是宣告了幾個任務;
②ApiUser繼承自XmlRpcUser ,這個可以在ApiUser類上看到;
③abstract = True,則標記為Locust不會嘗試從該類創建模擬用戶(僅擴展該類的類),
④ XmlRpcUser在客戶端屬性下提供XmlRpcClient的實體,
2.3 服務器端代碼展示
這是XML-RPC服務器的實作,該服務器可用作客戶端代碼的服務器,
代碼展示:
# -*- coding: utf-8 -*-
"""
@ auth : carl_DJ
@ time : 2020-10-05
"""
import time
from xmlrpc.server 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")
server.serve_forever()
今天的代碼,就分享到這里,
今天的代碼,也很簡單,
小魚不寫這么多,是因為怕耽誤各位十一假期出游啊 ~ ~
同樣,小魚再嘮叨一次,可能是習慣性,畢竟寫的是系列博文,
①、《深聊性能測驗,從入門到放棄之:Locust性能自動化(一)初識Locust》
帶你認識 locust,從此不再僅限于 Loadrunner、Jmeter性能功能,
②、《深聊性能測驗,從入門到放棄之:Locust性能自動化(二)代碼實戰》
讓你了解locust的內涵,自己也可以動手寫性能測驗腳本,
③、《深聊性能測驗,從入門到放棄之:Locust性能自動化(三)提高locust性能》
讓你提高性能,遨游性能的海洋,
④、《深聊性能測驗,從入門到放棄之:Locust性能自動化(四)自定義客戶端測驗》
帶你揭開,客戶端測驗其他系統的方法,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/159721.html
標籤:其他
上一篇:python人臉識別
下一篇:【詳細】Python基礎(三)
