python多线程http压力测试脚本

释放双眼,带上耳机,听听看~!

本文实例为大家分享了python多线程http压力测试的具体代码,供大家参考,具体内容如下


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
1#coding=utf-8
2
3import sys
4import time
5import thread
6import httplib, urllib
7import random
8import uuid
9import logging
10logging.basicConfig(level=logging.DEBUG,
11    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
12    datefmt='%a, %d %b %Y %H:%M:%S',
13    filename='测试脚本日志.log',
14    filemode='w')
15
16def log_uncaught_exceptions(exception_type, exception, tb):
17 logging.critical(''.join(traceback.format_tb(tb)))
18 logging.critical('{0}: {1}'.format(exception_type, exception))
19sys.excepthook = log_uncaught_exceptions
20
21#网关地址
22addr="172.18.2.4"
23port=8080
24thread_count = 15 #单次并发数量
25requst_interval = 10 #请求间隔(秒)
26test_count = sys.maxsize #sys.maxsize # 指定测试次数
27
28
29#字段说明,必须一一对应
30#login为空表示使用随机用户名
31
32param_list=[
33{"login":"user1","password":"qweqwe12"},
34]
35
36now_count = 0
37lock_obj = thread.allocate()
38def send_http():
39 global now_count
40 httpClient = None
41 try:
42  for user in user_list:
43   tmp_user = user["login"]
44   if tmp_user.strip() =='':
45    tmp_user = str(uuid.uuid1()) + str(random.random())
46   print tmp_user
47   params = urllib.urlencode({"operationData":
48      [{"login": tmp_user,"password":user["password"]}]})
49   headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
50
51   httpClient = httplib.HTTPConnection(addr, port, timeout=5)
52   httpClient.request("POST", "/simple/spider.task.distribute", params, headers)
53
54   response = httpClient.getresponse()
55   print '发送数据: ' + params
56   print '返回码: ' + str(response.status)
57   print '返回数据: ' + response.read()
58
59   logging.info('发送数据: ' + params)
60   logging.info('返回码: ' + str(response.status))
61   logging.info('返回数据: ' + response.read())
62   #print response.getheaders() #获取头信息
63   sys.stdout.flush()
64   now_count+=1
65 except Exception, e:
66  print e
67  logging.info(e)
68 finally:
69  if httpClient:
70   httpClient.close()
71
72def test_func(run_count):
73 global now_count
74 global requst_interval
75 global lock_obj
76 cnt = 0
77 while cnt < run_count:
78  lock_obj.acquire()
79  print ''
80  print '***************************请求次数:' + str(now_count) + '*******************************'
81  print 'Thread:(%d) Time:%s\n'%(thread.get_ident(), time.ctime())
82
83  logging.info(' ')
84  logging.info('***************************请求次数:' + str(now_count) + '*******************************')
85  logging.info('Thread:(%d) Time:%s\n'%(thread.get_ident(), time.ctime()))
86  cnt+=1
87  send_http()
88  sys.stdout.flush()
89  lock_obj.release()
90  time.sleep(requst_interval)
91
92def test(ct):
93 global thread_count
94 for i in range(thread_count):
95  thread.start_new_thread(test_func,(ct,))
96
97if __name__=='__main__':
98 global test_count
99 test(test_count)
100 while True:
101  time.sleep(100)
102

给TA打赏
共{{data.count}}人
人已打赏
安全技术

java千万级别数据生成文件思路和优化

2022-1-11 12:36:11

安全技术

共享内存linux C/C++代码实战------顺便玩下ipcs, ipcrm, shmget, shmat, shmdt, shmctl

2022-1-12 12:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索