当前位置:网站首页>Self writing performance testing tool (2)

Self writing performance testing tool (2)

2020-11-09 23:48:00 fnng

Last week I taught you how to pass Python Implement performance testing tools , Finally, I left a question , Today we continue to implement the command line tool .

Dependency Library

requests==2.22.0
gevent==20.9.0
numpy==1.19.2
click==7.1.2

click library

Today's main character is click library .

Chinese document :https://www.osgeo.cn/click/index.html

First example (hello.py):

import click

@click.command()
@click.argument('name')
@click.option('-c', default=1, help='number of greetings')
def hello(name, c):
    for x in range(c):
        click.echo('Hello %s!' % name)


if __name__ == "__main__":
    hello()

view help :

> python3 hello.py --help
Usage: hello.py [OPTIONS] NAME

Options:
  -c INTEGER  number of greetings
  --help      Show this message and exit.

Use :

> python3 hello.py  Insect division  -c 3
Hello  Insect division !
Hello  Insect division !
Hello  Insect division !

Now I have mastered click The basic usage of .

Implement command line performance testing tool

Next , take click Introduced to the kb.py In the performance test script .

from __future__ import print_function
import gevent
from gevent import monkey
monkey.patch_all()
import time
import click
import requests
from numpy import mean


class statistical:
    pass_number = 0
    fail_number = 0
    run_time_list = []


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

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


@click.command()
@click.argument('url')
@click.option('-u', default=1, help=' Number of users running , Default  1', type=int)
@click.option('-q', default=1, help=' Number of individual user requests , Default  1', type=int)
def main(url, u, q):
    print(" request URL: {url}".format(url=url))
    print(" The number of users :{}, cycles : {}".format(u, q))
    print("============== Running ===================")

    jobs = [gevent.spawn(running, url, q) for _url in range(u)]
    gevent.wait(jobs)

    print("\n============== Results ===================")
    print(" Maximum :       {} s".format(str(max(statistical.run_time_list))))
    print(" Minimum :       {} s".format(str(min(statistical.run_time_list))))
    print(" Average :       {} s".format(str(round(mean(statistical.run_time_list), 4))))
    print(" The request is successful ", statistical.pass_number)
    print(" request was aborted ", statistical.fail_number)
    print("============== end ===================")


if __name__ == "__main__":
    main()

view help :


python3 kb.py --help
Usage: kb.py [OPTIONS] URL

Options:
  -u INTEGER   Number of users running , Default  1
  -q INTEGER   Number of individual user requests , Default  1
  --help      Show this message and exit.

Usage method :

> python3 kb.py https://wwww.baidu.com -u 10 -q 10

 request URL: https://wwww.baidu.com
 The number of users :10, cycles : 10
============== Running ===================
....................................................................................................
============== Results ===================
 Maximum :       0.955 s
 Minimum :       0.2573 s
 Average :       0.4585 s
 The request is successful  100
 request was aborted  0
============== end ===================

Project code :https://github.com/SeldomQA/kb

版权声明
本文为[fnng]所创,转载请带上原文链接,感谢