又是新的一周

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

今天实践下

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

 #!/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. <2014 03 18> Term BreakPoint

  2. Android之网络----使用HttpClient发送HTTP请求(通过get方法获取数据)

    [正文] 一.HTTP协议初探: HTTP(Hypertext Transfer Protocol)中文 "超文本传输协议",是一种为分布式,合作式,多媒体信息系统服务,面向应用层 ...

  3. sql server 中having 的使用注意事项

    1.having 中不能使用未参与分列的组,having 不能替代where 作用不一样,having是对组进行过滤,where是每条记录进行过滤. 2.having 是对Group By 的条件分组 ...

  4. boost智能指针总结

    智能指针是一种具备指针类似行为的对象,当不在需要它的时候自动删除其引用的c++对象.直接点说就是自动析构C++对象. boost提供了6种智能指针,如下所示: scoped_ptr <boost ...

  5. java反射基础知识(五)反射应用实践

    详解Java反射各种应用   Java除了给我们提供在编译期得到类的各种信息之外,还通过反射让我们可以在运行期间得到类的各种信息.通过反射获取类的信息,得到类的信息之后,就可以获取以下相关内容: Cl ...

  6. Python:virtualenv介绍

    virtualenv 在开发Python应用程序的时候,系统安装的Python3只有一个版本:3.4.所有第三方的包都会被pip安装到Python3的site-packages目录下. 如果我们要同时 ...

  7. iOS消息推送原理

    推送相关概念,如下图1-1: 1.Provider:就是为指定IOS设备应用程序提供Push的服务器,(如果IOS设备的应用程序是客户端的话,那么Provider可以理解为服务端[消息的发起者]): ...

  8. 日志体系——loging

    import loggingclass log: def __init__(self): # 文件的命名 self.logname=os.path.join(os.path.abspath(os.pa ...

  9. gitlab + jenkins + docker + k8s

    总体流程: 在开发机开发代码后提交到gitlab 之后通过webhook插件触发jenkins进行构建,jenkins将代码打成docker镜像,push到docker-registry 之后将在k8 ...

  10. with as (cte common table expression) 公共表表达式

    SQL中 with as 的用法——使用公用表表达式(CTE)  公用表表达式 (CTE) 可以认为是在单个 SELECT.INSERT.UPDATE.DELETE 或 CREATE VIEW 语句的 ...