python多線程性能測試之快速mock數(shù)據(jù)
背景
在我們測試工作中,性能測試也是避免不了的,因此在性能測試前期準(zhǔn)備工作中,需要 mock 足夠批量的數(shù)據(jù)進(jìn)行壓測。那么怎么能在短時(shí)間內(nèi)快速 mock 出想要的格式數(shù)據(jù)和足夠量的數(shù)據(jù)進(jìn)行壓測?那么往下看。
安裝相關(guān)類包
- pip install kafka
- pip install appmetrics
- pip install faker
- pip install pykafka
快速 mock kafka 批量測試數(shù)據(jù)
# -* coding:utf8 *-
from pykafka import KafkaClient
import uuid
import time
import threading
from appmetrics import metrics
from faker import Faker
import os
fake = Faker("zh-cn")
PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(__file__), p)
)
meter = metrics.new_meter("meter_test")
host_producer = 'host地址'
def data_info():
uid = str(uuid.uuid4())
suid = ''.join(uid.split('-'))
return suid
def data_result():
#數(shù)據(jù)格式可自行定義
data = f"{data_info()},{fake.phone_number()},111111111111,LOL-UZI"
return data
def mock_request():
client_producer = KafkaClient(hosts=host_producer)
topicdocu = client_producer.topics['XXXXXXX-TOPIC']
producer = topicdocu.get_producer(sync=False) # sync=False 關(guān)閉同步,使用異步
while True:
data_uni = data_result()
producer.produce(bytes(data_uni, encoding='utf-8'))
meter.notify(1) # 請求一次 記錄器打點(diǎn)一次
# i = i - 1
producer.stop()
def print_meter():
while True:
print(meter.get())
time.sleep(1)
def thread_request(nums):
t1 = []
for i in range(nums):
if i == 0:
#該線程是為了記錄每秒打點(diǎn)作用
t = threading.Thread(target=print_meter, name="T" + str(i))
else:
t = threading.Thread(target=mock_request, name="T" + str(i))
t.setDaemon(True)
t1.append(t)
for t in t1:
t.start()
for t in t1:
t.join()
#
#
if __name__ == '__main__':
thread_request(101)
appmetrics 使用方法
Meters
Meters,度量一系列事件發(fā)生的速率 (rate),例如 TPS。Meters 會(huì)統(tǒng)計(jì)最近 1 分鐘,5 分鐘,15 分鐘,還有全部時(shí)間的速率。
meter = metrics.new_meter(“meter_test”) meter.notify(1) meter.notify(1) meter.notify(3) meter.get()
返回結(jié)果
{'count': 5, 'kind': 'meter', 'five': 0.0066114184713530035, 'mean': 0.27743058841197027, 'fifteen': 0.0022160607980413085, 'day': 2.3147478365093123e-05, 'one': 0.031982234148270686}
以上就是python批量測試多線程之快速mock數(shù)據(jù)的詳細(xì)內(nèi)容,更多關(guān)于python測試多線程mock數(shù)據(jù)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python解決pip?install?numpy過慢問題的幾種方法
在進(jìn)行Python科學(xué)計(jì)算、數(shù)據(jù)分析或機(jī)器學(xué)習(xí)時(shí),numpy是最基礎(chǔ)且最常用的庫之一,然而,許多用戶在安裝numpy時(shí),可能會(huì)遇到下載速度極慢甚至失敗的情況,本文將從問題分析、解決方案、優(yōu)化建議等多個(gè)角度,詳細(xì)介紹pip?install?numpy過慢問題的解決,需要的朋友可以參考下2025-03-03
使用Python快樂學(xué)數(shù)學(xué)Github萬星神器Manim簡介
這篇文章主要介紹了使用Python快樂學(xué)數(shù)學(xué)Github萬星神器Manim簡介,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
Python中range()與np.arange()的具體使用
本文主要介紹了Python中range()與np.arange()的具體使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
Python爬蟲urllib和requests的區(qū)別詳解
這篇文章主要介紹了Python爬蟲urllib和requests的區(qū)別詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
python 邊緣擴(kuò)充方式的實(shí)現(xiàn)示例
本文主要介紹了python 邊緣擴(kuò)充方式的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Python OpenCV機(jī)器學(xué)習(xí)之圖像識(shí)別詳解
OpenCV中也提供了一些機(jī)器學(xué)習(xí)的方法,例如DNN等。本文將為大家詳細(xì)介紹一下OpenCV中利用機(jī)器學(xué)習(xí)實(shí)現(xiàn)的一些圖片識(shí)別功能:人臉識(shí)別、車牌識(shí)別等,感興趣的可以了解一下2022-01-01
Python實(shí)現(xiàn)讀取json文件到excel表
這篇文章主要介紹了Python實(shí)現(xiàn)讀取json文件到excel表,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11

