SoFunction
Updated on 2024-11-13

python write a performance testing tool (I)

National Day relearning a bit about go's gin high-performance testing framework.

Using JMeter to test the performance of the gin vs. flask interface makes a big difference.

Why don't I try to write a performance tool myself, which at its core is all about concurrency and requests.

Requests can be selected from Python's requests library.

Concurrency can be simulated by python processes, threads, and concurrency.

When you think about it that way, it's not that hard to get on and jack one.

dependency library (computing)

requests==2.22.0
gevent==20.9.0
numpy==1.19.2

requests Everyone is no stranger to the HTTP request library.

gevent is a python concurrent library, simulating concurrency through concurrency is more resource-efficient and can simulate more concurrency in the same configuration.

numpy is a data computation library for python that provides a large number of group and matrix operations, which are used here to find the average of a list.

Implementation Script

Okay, next it's time to get your hands on the code.

from __future__ import print_function
import time
import gevent
from gevent import monkey
monkey.patch_all()

import requests
from numpy import mean


users = 10 # of users
numbers = 100 # of requests
req_url = "http://127.0.0.1:8080/user/tom" # Request URL

print("requestingURL: {url}".format(url=req_url))

print("number of users:{},Number of cycles: {}".format(users, numbers))

print("============== Running ===================")

pass_number = 0
fail_number = 0

run_time_list = []

def running(url):
  global fail_number
  global pass_number
  for _ in range(numbers):
    start_time = ()
    r = (url)
    if r.status_code == 200:
      pass_number = pass_number + 1
      print(".", end="")
    else:
      fail_number = fail_number + 1
      print("F", end="")

    end_time = ()
    run_time = round(end_time - start_time, 4)
    run_time_list.append(run_time)


jobs = [(running, req_url) for _url in range(users)]
(jobs)

print("\n============== Results ===================")
print("greatest:    {} s".format(str(max(run_time_list))))
print("minimal:    {} s".format(str(min(run_time_list))))
print("on average:    {} s".format(str(round(mean(run_time_list), 4))))
print("Request successful.", pass_number)
print("Request failed.", fail_number)
print("============== end ===================")

Design Ideas

Creating a thread group in JMeter has two parameters number of threads and number of loops, i.e. number of users and number of requests, set how many users and how many times each user user runs, the number of users is simulated by a concatenation and the number of times each user runs is realized by a for loop.

As for the request is relatively simple, directly through the requests to send requests. By determining whether the impact of the status code is 200 to determine the success or not, by counting the number of successful and failed requests respectively.

Regarding the request time statistics, the current timestamp is obtained before and after each request, and then the time difference is calculated to be the call time of a single interface. Maximum, minimum and average can be easily obtained by calculation.

> python3 

requestingURL: http://127.0.0.1:8080/user/tom
number of users:10,Number of cycles: 100
============== Running ===================
...............................................................................................................
...............................................................................................................
...............................................................................................................
...............................................................................................................
...............................................................................................................
...............................................................................................................
...............................................................................................................
...............................................................................................................
...............................................................................................................
.
============== Results ===================
greatest:    0.0352 s
minimal:    0.0036 s
on average:    0.0204 s
requesting成功 1000
requesting失败 0
============== end ===================

(dialect) remarry

Make the script an ab command line tool.

Support for more request types (get/post/put/delete) and parameters.

More statistical dimensions, throughput, throughput rate

Increase start-up time, thinking time, etc.

...

Above is python write a performance test tool (a) of the details, more information about python performance test tool please pay attention to my other related articles!