python多线程http压力测试脚本


Posted in Python onJune 25, 2019

本文实例为大家分享了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)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python2中的raw_input() 与 input()
Jun 12 Python
小议Python中自定义函数的可变参数的使用及注意点
Jun 21 Python
浅谈Python类里的__init__方法函数,Python类的构造函数
Dec 10 Python
利用Python2下载单张图片与爬取网页图片实例代码
Dec 25 Python
Python中字典的浅拷贝与深拷贝用法实例分析
Jan 02 Python
Python for循环与range函数的使用详解
Mar 23 Python
python实现月食效果实例代码
Jun 18 Python
Python企业编码生成系统总体系统设计概述
Jul 26 Python
python数字类型math库原理解析
Mar 02 Python
python 利用Pyinstaller打包Web项目
Oct 23 Python
利用Python实现学生信息管理系统的完整实例
Dec 30 Python
教你怎么用Python监控愉客行车程
Apr 29 Python
Pyqt5 基本界面组件之inputDialog的使用
Jun 25 #Python
对PyQt5的输入对话框使用(QInputDialog)详解
Jun 25 #Python
如何使用Python标准库进行性能测试
Jun 25 #Python
python绘制评估优化算法性能的测试函数
Jun 25 #Python
Python基于机器学习方法实现的电影推荐系统实例详解
Jun 25 #Python
Python 中的参数传递、返回值、浅拷贝、深拷贝
Jun 25 #Python
pyqt5 删除layout中的所有widget方法
Jun 25 #Python
You might like
PHP开发中csrf攻击的简单演示和防范
2017/05/07 PHP
PHP获取日期对应星期、一周日期、星期开始与结束日期的方法
2018/06/22 PHP
Javascript动态引用CSS文件的2种方法介绍
2014/06/06 Javascript
火狐下input焦点无法重复获取问题的解决方法
2014/06/16 Javascript
将HTML格式的String转化为HTMLElement的实现方法
2014/08/07 Javascript
适用于手机端的jQuery图片滑块动画
2016/12/09 Javascript
CodeMirror js代码加亮使用总结
2017/03/25 Javascript
详解操作虚拟dom模拟react视图渲染
2018/07/25 Javascript
electron 如何将任意资源打包的方法步骤
2020/04/16 Javascript
浅析JavaScript 函数防抖和节流
2020/07/13 Javascript
[55:47]DOTA2上海特级锦标赛C组小组赛#2 LGD VS Newbee第三局
2016/02/27 DOTA
[20:39]DOTA2-DPC中国联赛 正赛开幕式 1月18日
2021/03/11 DOTA
解析Python中的二进制位运算符
2015/05/13 Python
在Python中操作字典之clear()方法的使用
2015/05/21 Python
详解Python中的序列化与反序列化的使用
2015/06/30 Python
浅谈python字典多键值及重复键值的使用
2016/11/04 Python
详解Python中的Numpy、SciPy、MatPlotLib安装与配置
2017/11/17 Python
Python中sort和sorted函数代码解析
2018/01/25 Python
运用TensorFlow进行简单实现线性回归、梯度下降示例
2018/03/05 Python
Python实现的文轩网爬虫完整示例
2019/05/16 Python
python自动化测试之异常及日志操作实例分析
2019/11/09 Python
python+opencv实现移动侦测(帧差法)
2020/03/20 Python
基于python模拟TCP3次握手连接及发送数据
2020/11/06 Python
Pytorch如何切换 cpu和gpu的使用详解
2021/03/01 Python
全球领先的在线cosplay服装商店:RoleCosplay
2020/01/18 全球购物
ruby如何进行集成操作?Ruby能进行多重继承吗?
2013/10/16 面试题
经济管理专业毕业生自荐信范文
2014/01/02 职场文书
应届大学生求职信
2014/07/20 职场文书
老干部工作先进事迹
2014/08/17 职场文书
责任书范本
2014/08/25 职场文书
社区灵活就业证明
2014/11/03 职场文书
平遥古城导游词
2015/02/03 职场文书
合理化建议书
2015/02/04 职场文书
大学生学生会工作总结2015
2015/05/26 职场文书
大学学生会竞选稿
2015/11/19 职场文书
go语言中GOPATH GOROOT的作用和设置方式
2021/05/05 Golang