博主昨天优化了接口框架想着再添加些功能
想到对接口的性能压力测试
在工作过程中之前都是使用的工具 如:loadrunner、jmeter
想着这次准备用python实现对接口的性能压力测试
首先要实现这个功能就要运用到python的threading模块
下面是自己学习摸索出来的代码:
#!/usr/bin/env python
# -*- coding: utf_8 -*- import threading
import requests
from time import ctime def api(url, data):
try:
r = requests.post(url, data)
print "接口访问返回状态码: ", r.status_code
print "接口请求时间: ", ctime()
print "接口访问返回地址: ", r.url
except Exception.__bases__:
print "接口访问失败 ", "接口请求时间: ", ctime() case_1 = {'username': 'admin', 'password': ''}
url1 = 'http://localhost:8081/swcw/back/sysLogin.action' threads = []
for i in range(10):
t_i = threading.Thread(target=api(url1, case_1))
threads.append(t_i) if __name__ == '__main__':
for t in threads:
t.setDaemon(True)
t.start()
t.join() print "all over %s" % ctime()
这个是10个进程同时登录的场景
之后要改成类的形式 增加一些性能运行的结果数据
最后我推荐一位关于多线程的博客 通俗易懂
博主自己也是从上面学习的