import requests
import json
import threading
import time
import uuid
import random
import datetime
import redis
import logging
import numpy as np
import math
pool = redis.ConnectionPool(host='localhost', port=6379)
logger = logging.getLogger('requset_log')
logger.setLevel(logging.DEBUG)
log_file = logging.FileHandler('test.log', encoding='utf-8')
log_file.setLevel(logging.DEBUG)
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
log_file.setFormatter(formatter)
logger.addHandler(log_file)
url = 'http://ip:port/ctrl/api/get?app_id=8d25bb3617814922&server_name=Utilities.Gas&method=Utilities.Gas.ApiProxy.UpdateOrderRecordsStatus¶meters='
token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkYXRhIjoiRENqeTBNRG9yendDWkJiY0VtZDRma0JXcFNiVzBycnVOdlZEcSttNEh4cHNISkhlNjZHakVRZGwvbnJVZkhwVVJsRnBQdVVZZklaamFJb3lrNmpqcVVNaUwzc3poQjlUajg1N1BvU3NpS0ovQ0xKTFRDOVYycWpCdTBCbVVUSjQrT1VLMktRZUxObUpndEN4ZXE1VTZNR3dWT2ZmSEZiZ1QzRVRCdkxTRjBwRENpaFdla2FPcHZXeHFOYjFnb282In0.hUvlZ0mVSqoZzNeLUSsfUWT3XJ7KYbl0teh3XRyo_2A'
parameters = [
'{"outTradeNo":"909f6972ba3126e1c02f9c6c3a39b69d","statusCode":803}']
time_list = []
class counter():
def set_flag(self, flag):
redis_conn = redis.Redis(connection_pool=pool)
self.flag = flag
redis_conn.set('counter', flag)
return 'success'
def get_flag(self, key):
redis_conn = redis.Redis(connection_pool=pool)
self.key = key
_new_flag = ord(redis_conn.get(self.key))
print(_new_flag)
return _new_flag
def incr_flag(self, key):
redis_conn = redis.Redis(connection_pool=pool)
self.key = key
_new_flag = redis_conn.incr(self.key)
print('第', _new_flag, '個線程組')
return _new_flag
class getRequests():
def __init__(self, url, token, params):
self.token = token
self.params = params
self.url = url+random.choice(params)
self.headers = {'content-type': 'application/json',
'Authorization': 'Bearer '+self.token}
def get(self):
try:
r = requests.get(self.url, timeout=5, headers=self.headers)
data = r.text
logger.info(data)
response_time = r.elapsed.total_seconds()*1000
time_list.append(response_time)
print('data:', data)
time_list.sort(reverse=True)
# print(time_list)
sum = 0
for i in range(len(time_list)):
sum = sum+time_list[i]
avg_time = sum/len(time_list)
print('avg_time:', avg_time)
print('max_time:', max(time_list))
print('min_time:', min(time_list))
print('90%line:', time_list[math.ceil((len(time_list)-1)*0.1)])
logger.info(avg_time)
except Exception as err:
print(err)
logger.error(err)
def exec():
result = getRequests(url, token, parameters)
return result.get()
def open_threads():
try:
i = 0
# 开启{{tasks_number}}個线程
tasks_number = 5
while i < tasks_number:
t = threading.Thread(target=exec)
t.start()
i += 1
except Exception as err:
print(err)
# {{_delay}秒內開啟{tasks_number}}個線程
_delay = 2
# 每個線程組請求間隔{{_sleep_time}}
_sleep_time = 5
while True:
count = counter().incr_flag('counter')
if(count >= 200):
break
else:
timer = threading.Timer(_delay, open_threads)
timer.start()
time.sleep(_sleep_time)
网友评论