SoFunction
Updated on 2025-03-02

Fast mock data for Python multithreaded performance test

background

In our testing work, performance testing is also inevitable. Therefore, in the preliminary preparations for performance testing, mock sufficient batches of data are needed for stress testing. So how can we quickly mock the desired format data and sufficient amount of data for pressure measurement in a short period of time? Then look down.

Install related packages

  • pip install kafka
  • pip install appmetrics
  • pip install faker
  • pip install pykafka

Fast mock kafka batch test data

# -* 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: (
    ((__file__), p)
)
meter = metrics.new_meter("meter_test")
host_producer = 'host address'
def data_info():
    uid = str(uuid.uuid4())
    suid = ''.join(('-'))
    return suid
def data_result():
    #Data format can be defined by yourself    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 Turn off synchronization and use asynchronous    while True:
        data_uni = data_result()
        (bytes(data_uni, encoding='utf-8'))
        (1) # Request once the recorder is clicked once        # i = i - 1
    ()
def print_meter():
    while True:
        print(())
        (1)
def thread_request(nums):
    t1 = []
    for i in range(nums):
        if i == 0:
            #This thread is used to record the function of every second            t = (target=print_meter, name="T" + str(i))
        else:
            t = (target=mock_request, name="T" + str(i))
        (True)
        (t)
    for t in t1:
        ()
    for t in t1:
        ()
#
#
if __name__ == '__main__':
    thread_request(101)

How to use appmetrics

Meters

Meters, measure the rate (rate) of a series of events, such as TPS. Meters will count the last 1 minute, 5 minutes, 15 minutes, and the rate of all time.

meter = metrics.new_meter(“meter_test”)
(1)
(1)
(3)
()

Return result

{'count': 5, 'kind': 'meter', 'five': 0.0066114184713530035, 'mean': 0.27743058841197027, 'fifteen': 0.0022160607980413085, 'day': 2.3147478365093123e-05, 'one': 0.031982234148270686}

The above is the detailed content of python batch testing of multi-threaded fast mock data. For more information about python testing of multi-threaded mock data, please pay attention to my other related articles!