代码改变世界

自己上手写性能测试工具(二)

  虫师  阅读(2195)  评论(1编辑  收藏  举报

上周教大家如何通过Python实现性能测试工具,最后留了一下问题,今天我们继续来实现命令行工具。

依赖库

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

click 库

今天的主角是click库。

中文文档:https://www.osgeo.cn/click/index.html

第一个例子(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()

查看帮助:

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

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

使用:

> python3 hello.py 虫师 -c 3
Hello 虫师!
Hello 虫师!
Hello 虫师!

现在已经掌握了click 的基本用法。

实现命令行性能测试工具

接下来,将click引入到kb.py性能测试脚本中。

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='运行用户的数量,默认 1', type=int)
@click.option('-q', default=1, help='单个用户请求数,默认 1', type=int)
def main(url, u, q):
    print("请求URL: {url}".format(url=url))
    print("用户数:{},循环次数: {}".format(u, q))
    print("============== Running ===================")

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

    print("\n============== Results ===================")
    print("最大:       {} s".format(str(max(statistical.run_time_list))))
    print("最小:       {} s".format(str(min(statistical.run_time_list))))
    print("平均:       {} s".format(str(round(mean(statistical.run_time_list), 4))))
    print("请求成功", statistical.pass_number)
    print("请求失败", statistical.fail_number)
    print("============== end ===================")


if __name__ == "__main__":
    main()

查看帮助:


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

Options:
  -u INTEGER  运行用户的数量,默认 1
  -q INTEGER  单个用户请求数,默认 1
  --help      Show this message and exit.

使用方法:

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

请求URL: https://wwww.baidu.com
用户数:10,循环次数: 10
============== Running ===================
....................................................................................................
============== Results ===================
最大:       0.955 s
最小:       0.2573 s
平均:       0.4585 s
请求成功 100
请求失败 0
============== end ===================

项目代码:https://github.com/SeldomQA/kb

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2013-11-09 python BDD 框架之lettuce
Web Page Counters
Computer Desks
点击右上角即可分享
微信分享提示