python socket调试

昨日写的websocket的调试测速脚本,未完善,精确到毫秒级别了,脚本自身的速度也会影响很大,感觉这个方法可信度不高。

长连接就是一次建立socket后不断开请求,短链接就是每次都重新建立socket请求,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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import websocket
import json
import datetime
import numpy as np
import requests
import time

url_ws = "ws://127.0.0.1:8090/ws/1111"#"ws://yandongxunjing.club:8090/ws/1111" # 接口地址
url_http = "http://127.0.0.1:8090/gggg"#"http://yandongxunjing.club:8090/gggg"


content = [{"request_index":0,"time_stamp":111111111111111111,"source_id":13,"chapter_id":4,"event_id":4},{"request_index":1,"time_stamp":111111111111111111,"source_id":13,"chapter_id":4,"event_id":4}]
#0是菜单,1是内容
socket_time_list = []
http_time_list = []
time_list_socket_s = []
time_list_socket_l = []



send_list = []
def test_http(i):
#global
data={'message':str(content[i]),'sessionid':1111}
print(data)
r = requests.get(url_http,params=data)
print(r.text)

def on_message(ws, message):
endtime = datetime.datetime.now()
starttime = send_list.pop(0)
time_list_socket_l.append((starttime - endtime).microseconds)

# global times
# global endtime
# global starttime
# endtime = datetime.datetime.now()
# time_list_socket_l.append((starttime - endtime).microseconds)

# times -= 1
# if(times>0):
# starttime = datetime.datetime.now()
# ws.send(json.dumps(content[1]))
# #print(message)
# else:
# print("---------------------------------")
# print("短链接时间:")
# print(time_list_socket_s)
# print(np.mean(time_list_socket_s))
# print("---------------------------------")
# print("长连接时间:")
# print(time_list_socket_l)
# print(np.mean(time_list_socket_l))
# print("---------------------------------")
# print("http时间:")
# print(time_list_http)
# print(np.mean(time_list_http))
# print("times up")


def on_error(ws, error):
print(error)
def on_close(ws):
print("close connection")

def on_open(ws):
for i in range(20):
starttime = datetime.datetime.now()
ws1.send(json.dumps(content[1]))
send_list.append(starttime)
time.sleep(3)
#time_list_socket_l.append((starttime - endtime).microseconds)
#ws.send(json.dumps(content[1]))
print("on open")

if __name__ == "__main__":
time_list_http = []
# for i in range(20):
# starttime = datetime.datetime.now()
# test_http(1)
# endtime = datetime.datetime.now()
# time.sleep(3)
# time_list_http.append((endtime - starttime).microseconds)




# websocket.enableTrace(True)




for i in range(20):#短链接测试
starttime = datetime.datetime.now()
ws = websocket.create_connection(url_ws)

ws.send(json.dumps(content[1]))
result = ws.recv()
print(result)
endtime = datetime.datetime.now()
time_list_socket_s.append((endtime - starttime).microseconds)
time.sleep(3)
# ws1 = websocket.WebSocketApp(url_ws,
# on_message = on_message,
# on_error = on_error,
# on_close = on_close)
# times = 20
# ws1.on_open = on_open
# starttime = datetime.datetime.now()
# ws1.run_forever()



print("---------------------------------")
print("短链接时间:")
print(time_list_socket_s)
print(np.mean(time_list_socket_s))
print("---------------------------------")
print("长连接时间:")
print(time_list_socket_l)
print(np.mean(time_list_socket_l))
print("---------------------------------")
print("http时间:")
print(time_list_http)
print(np.mean(time_list_http))
print("times up")
# print(time_list_socket_l)
# print(time_list_socket_s)


#预计统计的时间,socket建立时间+请求(onOpen),socket通信中请求(send & respond),
# http请求时间
#
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2015-2024 galaxy
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信