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


猜你喜欢
- window对文件夹的操作主要包括移动/剪切/复制,本篇文章主要用jQuery来实现,下面一起来了解一下把。1.先看下效果吧!2.在添加一个
- 一、回顾一下前面《Oracle开发之窗口函数》中关于全统计一节,我们使用了Oracle提供的:sum(sum(tot_sales)) ove
- MySQL有6种日志,监控数据库系统的时候必须知道select日志slow select日志变更日志二进制变更日志(binlog)
- 一、安装软件包并创建项目$sudo pip install django$sudo python -c "import djang
- 这篇文章主要介绍了如何给Python代码进行加密,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以
- 在本文中,小编将与大家重点探讨SQL执行进展优化的方法。聚集索引扫描SELECT * FROM C_SY_Ownedstorm 聚集索引扫描
- 一个简单的实现class NaiveFilter():'''Filter Messages from keyword
- 1、not关键词可以反转一个布尔值。>>> not TrueFalse>>>>>> n
- 这几天花了点时间,将把django开发好的web项目部署到Apache上,参考了官方的一些文档和互联网上的文档,还是花了比较多的时间,这里把
- 本文实例讲述了Python socket套接字实现C/S模式远程命令执行功能。分享给大家供大家参考,具体如下:一. 前言要求: 使用pyth
- 如果我们的web应用有大量的异步请求,而这些异步请求是在web服务器认证的情况下,那当我们请求发生在服务器认证失效下,服务器自动302到登录
- 从今天开始起,基督山将和大家一起进入ASP.net 诸多程序的学习中,老实说,.net到底是法宝还是垃圾,我们拭目以待。有任何问题,联络基督
- 这是《python基础教程》后面的实践,照着写写,一方面是来熟悉python的代码方式,另一方面是练习使用python中的基本的以及非基本的
- 对于中小型个人、企业网站来说,MySQL数据库或许是目前数据库的最完美实施解决方案了。在不变更服务器硬件的前提下,一个经过良好架构,优化后的
- python 字典操作提取key,value dictionaryName[key] = value1.为字典增加一项 2.访问字典中的值
- 《页面表达常用方式》是整个“web交互设计方法”中的一部分:设计师在设计页面时,应该在页面上建立许多视觉层次,引导用户的视觉焦点。把用户的注
- 前言实现类似SQL的join操作,通过pd.merge()方法可以自由灵活地操作各种逻辑的数据连接、合并等操作可以将两个DataFrame或
- 出自:【孟宪会之精彩世界】 发布日期:2005年1月27日 8点48分0秒 [有删改] 由于某些原
- 所有数据库mysqlcheck --auto-repair -A -o -uroot -pyigeorg单一数据库mysqlcheck --
- 学生管理系统相信大家学各种语言的时候,练习总是会写各种管理系统吧,管理系统主要有对数据的增删查改操作,原理不难,适合作为练手的小程序数据的结