python多线程http压力测试脚本
作者:feihuadao 发布时间:2022-12-31 16:48:37
标签:python,多线程,压力测试
本文实例为大家分享了python多线程http压力测试的具体代码,供大家参考,具体内容如下
#coding=utf-8
import sys
import time
import thread
import httplib, urllib
import random
import uuid
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='测试脚本日志.log',
filemode='w')
def log_uncaught_exceptions(exception_type, exception, tb):
logging.critical(''.join(traceback.format_tb(tb)))
logging.critical('{0}: {1}'.format(exception_type, exception))
sys.excepthook = log_uncaught_exceptions
#网关地址
addr="172.18.2.4"
port=8080
thread_count = 15 #单次并发数量
requst_interval = 10 #请求间隔(秒)
test_count = sys.maxsize #sys.maxsize # 指定测试次数
#字段说明,必须一一对应
#login为空表示使用随机用户名
param_list=[
{"login":"user1","password":"qweqwe12"},
]
now_count = 0
lock_obj = thread.allocate()
def send_http():
global now_count
httpClient = None
try:
for user in user_list:
tmp_user = user["login"]
if tmp_user.strip() =='':
tmp_user = str(uuid.uuid1()) + str(random.random())
print tmp_user
params = urllib.urlencode({"operationData":
[{"login": tmp_user,"password":user["password"]}]})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
httpClient = httplib.HTTPConnection(addr, port, timeout=5)
httpClient.request("POST", "/simple/spider.task.distribute", params, headers)
response = httpClient.getresponse()
print '发送数据: ' + params
print '返回码: ' + str(response.status)
print '返回数据: ' + response.read()
logging.info('发送数据: ' + params)
logging.info('返回码: ' + str(response.status))
logging.info('返回数据: ' + response.read())
#print response.getheaders() #获取头信息
sys.stdout.flush()
now_count+=1
except Exception, e:
print e
logging.info(e)
finally:
if httpClient:
httpClient.close()
def test_func(run_count):
global now_count
global requst_interval
global lock_obj
cnt = 0
while cnt < run_count:
lock_obj.acquire()
print ''
print '***************************请求次数:' + str(now_count) + '*******************************'
print 'Thread:(%d) Time:%s\n'%(thread.get_ident(), time.ctime())
logging.info(' ')
logging.info('***************************请求次数:' + str(now_count) + '*******************************')
logging.info('Thread:(%d) Time:%s\n'%(thread.get_ident(), time.ctime()))
cnt+=1
send_http()
sys.stdout.flush()
lock_obj.release()
time.sleep(requst_interval)
def test(ct):
global thread_count
for i in range(thread_count):
thread.start_new_thread(test_func,(ct,))
if __name__=='__main__':
global test_count
test(test_count)
while True:
time.sleep(100)
来源:https://blog.csdn.net/feihuadao/article/details/48785003
0
投稿
猜你喜欢
- 表查询: 合并查询:使用union关键字,可将满足条件的重复行去掉。 select ename,sal,job from emp where
- 本文实例为大家分享了python实现学生管理系统的具体代码,供大家参考,具体内容如下一.面向过程版import osstu_list = [
- 概述可以获取的数据包括:video-视频模块user-用户模块dynamic-动态模块这次用“Running Man”十周年特辑的视频,来做
- 问:如何给导入文件加上时间戳标记?答:请参考下文中介绍的两种方法:1.在DOS下从系统获得时间戳利用Dos命令取得时间戳:C:\>ec
- 下载编译器protoc两种方式:1、使用google官方protoc下载地址:https://github.com/google/proto
- 对python中的控制条件、循环和跳出详解代码缩进(代码块):python用缩进表示代码块,没有其他语言的大括号缩进是强制检查,整个代码缩进
- 我就废话不多说了,大家还是直接看代码吧~import kerasimport numpy as npimport matplotlib.py
- 在近几年Python的呼声越来越高,很多刚开始起步想要学习编程的朋友都会犹豫要不要选择学习Python,毕竟作为人工智能时代的首选语言这个诱
- ORCLE数据库备份策略 1.通过使用exp和imp命令实现数据库导出和导入。 有三种模式: a. 用户模式: 导出(导入)用户所有对象以及
- 这方面我还是一个freshman,不过看了一些文章,经过一些实践后也算是有了一些想法。希望如果有这方面的前辈路过的话,能不吝指教。首先,作为
- from keras.utils.np_utils import to_categorical注意:当使用categorical_cross
- 日期和时间类型MySQL有多个表示各种日期和时间值的数据类型, 比如YEAR和DATE. MySQL存储时间的最精确粒度是秒。 然而, 能做
- 本文实例为大家分享了Bootstrap导航栏和登陆框的具体代码,供大家参考,有不足的地方请大家谅解,大家共同学习进步。<!DOCTYP
- 01、正则表达式学习正则表达式操作字符串,re模块是用C语言写的没匹配速度非常快,其中compile函数根据一个模式字符串和可选的标志参数生
- © 版权符号显示不清楚,就是那个圈C,在某些网站上就是显示太小,看不清楚。一开始还以为是字体大小的原因,调大以后,还是一样
- subprocess官方中文文档介绍参考文档,我的直观感受和实际用法是:subprocess可以开启一个子进程来运行cmd命令。那就意味着可
- ajax开发中, 常遇到下面的几种情况: 1 服务端需要返回一段普通文本给客户端 2 服务端需要返回一段HTML代码给客户端 3 服务端需要
- 工作中偶尔需要做客流分析,用pyplot 库绘图。一般情况下, x 轴刻度默认显示为数字。例如:我希望x 轴刻度显示为星期日期。查询pypl
- 随着网站访问量的加大,每次从数据库读取都是以效率作为代价的,很多用ACCESS作数据库的更会深有体会,静态页加在搜索时,也会被优先考虑。互联
- 由于工作中涉及到生日编辑资料编辑,然后自己改了一下代码:<html><head> <meta charset=