又是新的一周

延续上周的进度 关于多进程的学习

今天实践下

初步设计的接口性能压力测试代码如下:

 #!/usr/bin/env python
# -*- coding: utf_8 -*- import threading
import requests
import time
import re
from time import sleep # -------接口性能测试配置-------
method = "post"
# 接口类型
url = "http://localhost:8081/swcw/back/sysLogin.action"
# 接口地址
data = {"username": "admin", "password": ""}
# 接口参数
thread_num = 20
# 线程数
one_work_num = 2
# 每个线程循环次数
loop_sleep = 1
# 每次请求时间间隔
response_time = []
# 平均响应时间列表
error = []
# 错误信息列表 class CreateThread:
def __init__(self):
pass @classmethod
def thread_api(cls):
global results
try:
if method == "post":
results = requests.post(url, data)
if method == "get":
results = requests.get(url, data)
return results
except requests.ConnectionError:
return results
# 接口函数 @classmethod
def thread_response(cls):
responsetime = float(CreateThread.thread_api().elapsed.microseconds) / 1000
return responsetime
# 获取响应时间 单位ms @classmethod
def thread_response_avg(cls):
avg = 0.000
l = len(response_time)
for num in response_time:
avg += 1.000 * num / l
return avg
# 获取平均相应时间 单位ms @classmethod
def thread_time(cls):
return time.asctime(time.localtime(time.time()))
# 获取当前时间格式 @classmethod
def thread_error(cls):
try:
pa = u"个人信息"
pattern = re.compile(pa)
match = pattern.search(CreateThread.thread_api().text)
if CreateThread.thread_api().status_code == 200:
pass
if match.group() == pa:
pass
else:
error.append(CreateThread.thread_api().status_code)
except AttributeError:
error.append("登录失败")
# 获取错误的返回状态码 @classmethod
def thread_work(cls):
threadname = threading.currentThread().getName()
print "[", threadname, "] Sub Thread Begin"
for i in range(one_work_num):
CreateThread.thread_api()
print "接口请求时间: ", CreateThread.thread_time()
response_time.append(CreateThread.thread_response())
CreateThread.thread_error()
sleep(loop_sleep)
print "[", threadname, "] Sub Thread End"
# 工作线程循环 @classmethod
def thread_main(cls):
start = time.time()
threads = []
for i in range(thread_num):
t = threading.Thread(target=CreateThread.thread_work())
t.setDaemon(True)
threads.append(t)
for t in threads:
t.start()
# 启动所有线程
for t in threads:
t.join()
# 主线程中等待所有子线程退出
end = time.time() print "========================================================================"
print "接口性能测试开始时间:", time.asctime(time.localtime(start))
print "接口性能测试结束时间:", time.asctime(time.localtime(end))
print "接口地址:", url
print "接口类型:", method
print "线程数:", thread_num
print "每个线程循环次数:", one_work_num
print "每次请求时间间隔:", loop_sleep
print "总请求数:", thread_num * one_work_num
print "错误请求数:", len(error)
print "总耗时(秒):", end - start
print "每次请求耗时(秒):", (end - start) / (thread_num * one_work_num)
print "每秒承载请求数(TPS):", (thread_num * one_work_num) / (end - start)
print "平均响应时间(毫秒):", CreateThread.thread_response_avg() if __name__ == '__main__':
CreateThread.thread_main()

python学习笔记(threading接口性能压力测试)的更多相关文章

  1. JMeter工具接口性能压力测试分析与优化

    最近公司做的项目,要求对相关接口做性能压力测试,在这里记录一下分析解决过程. 压力测试过程中,如果因为资源使用瓶颈等问题引发最直接性能问题是业务交易响应时间偏大,TPS逐渐降低等.而问题定位分析通常情 ...

  2. Python学习笔记--threading线程

    通过线程来实现多任务并发.提高性能.先看看例子. #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2020-03-02 21:10:39 ...

  3. python学习笔记(十三)接口开发

    一.开发接口的作用 1.mock接口,模拟一些接口,在别的接口没有开发好的时候,需要开发一些模拟接口进行调试和测试. 2.查看数据,比如,获取所有学员信息的接口,就不需要提供数据库的查看信息. 二.接 ...

  4. python学习笔记(接口自动化框架 V2.0)

    这个是根据上次框架版本进行的优化 用python获取excel文件中测试用例数据 通过requets测试接口.并使用正则表达式验证响应信息内容 生成xml文件测试报告 版本更新内容: 1. 整理了Cr ...

  5. python学习笔记(接口自动化框架 V1.0)

    之前是利用python自带的unittest测试框架 这次自己设计一个 之后再一点点往里面加功能 (ps:当然这个框架真的是很简单..很简单...很简单...) excel文件格式: #!/usr/b ...

  6. python学习笔记:接口开发——flask Demo实例

    举例1,返回当前时间接口 ''' 初始化:所有的Flask都必须创建程序实例, web服务器使用wsgi协议,把客户端所有的请求都转发给这个程序实例 程序实例是Flask的对象,一般情况下用如下方法实 ...

  7. python学习笔记:接口开发——PythonWEB框架之Flask

    Flask是一个使用 Python 编写的轻量级 Web 应用框架,安装命令如下 pip install flask 一.服务端接口是怎么开发的? 1.启动一个服务 2.接收到客户端传过来的数据3.登 ...

  8. python学习笔记-调用接口

    requests模块 1.get请求: url = 'http://api.nnzhp.cn/api/user/stu_info' data = {'stu_name':'小黑'} #请求数据 req ...

  9. python学习笔记(threading多线程)

    博主昨天优化了接口框架想着再添加些功能 想到对接口的性能压力测试 在工作过程中之前都是使用的工具 如:loadrunner.jmeter 想着这次准备用python实现对接口的性能压力测试 首先要实现 ...

随机推荐

  1. iPhone设备分辨率一览

    地址:https://developer.apple.com/design/human-interface-guidelines/ios/icons-and-images/launch-screen/

  2. JavaScript 学习(1)--window对象

    JavaScript 学习--Window对象 window对象方法 1.1 创建新窗体 //窗体打开 var newWindow=Window.open("default.html&quo ...

  3. img标签使用onload进行src更改时出现的内存溢出问题

    最近在开发时需要在img标签加载完成后修改src属性,使用了onload方法. 但是在方法体中最后没有把onload事件指向null, 导致了循环调用onload方法,CPU占用一直居高不下,最后只要 ...

  4. Eclipse 介绍

    设置背景的插件: Darkest Dark Theme 添加 properties 插件: Properties Editor Git 插件: Egit 常用快捷键 command + 1 : 代码提 ...

  5. Log4j:log4j.properties 配置解析

    Log4j 三个主要组件 Loggers(记录器):记录日志的工具,程序中就是用它来记录我们想要的日志信息. Appenders (输出源):日志输出到什么地方,可以是控制台.文件.流位置.数据库,等 ...

  6. Django页面重定向

    重定向分为永久性重定向和暂时性重定向,在页面上体现的操作就是浏览器会从一个页面自动跳转到另外一个页面.比如用户访问了一个需要权限的页面,但是该用户当前并没有登录,因此我们应该给他重定向到登录页面. 永 ...

  7. 【我的Android进阶之旅】如何隐藏Android中EditText控件的默认下划线

    Android EditText控件是经常使用的控件,但是有时候我们并不需要它的一些默认的属性,比如说下划线,因为有时候这样的默认下划线看起来特别怪异,和其他控件在一起搭配的时候不协调,因此有时候就需 ...

  8. RT-Thread内核之线程调度(三)

    4.RT-Thread中的线程? /**  * 线程结构  */ struct rt_thread {     /** Object对象 */     char        name[RT_NAME ...

  9. String StringBuffer StringBuilder 老生常谈

    1.String 与 StringBuffer . StringBuilder的区别 String 字符串常量 而 (StringBuffer 和 StringBuilder 字符串变量) 执行速度上 ...

  10. springboot整合JPA创建数据库表失败

    org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table t_s ...