python Realizes the Client Stress Test of websocket
- 2021-07-01 07:50:34
- OfStack
Use python for client-side stress testing of websocket. This code is found on github. Then simply modified it. Great God uses the contents of process pool and thread pool. So save it and learn to learn
Then it should be noted that python2.7 used this time also tries to use python3.6, but it is true that websocket-client package and python3 are incompatible, suggesting that there is no relevant method. So I had to adopt python2 in the end
# -*- coding:utf-8 -*-
# __author__ == 'chenmingle'
import websocket
import time
import threading
import json
import multiprocessing
import uuid
from threadpool import ThreadPool, makeRequests
# Modify it into your own websocket Address
WS_URL = "xxxx"
# Defining the number of processes
processes = 4
# Define the number of threads (per file may limit 1024 Which can be modified fs.file Isoparametric)
thread_num = 700
index = 1
def on_message(ws, message):
# print(message)
pass
def on_error(ws, error):
print(error)
pass
def on_close(ws):
# print("### closed ###")
pass
def on_open(ws):
global index
index = index + 1
def send_thread():
# Set you websocket Content of
# Every interval 10 Send in seconds 1 Down the data to keep the link uninterrupted
while True:
ws.send(u'hello Server ')
time.sleep(10)
t = threading.Thread(target=send_thread)
t.start()
def on_start(num):
time.sleep(5)
# websocket.enableTrace(True)
ws = websocket.WebSocketApp(WS_URL + str(num),
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.on_open = on_open
ws.run_forever()
def thread_web_socket():
# Thread pool
pool_list = ThreadPool(thread_num)
num = list()
# Set the number of open threads
for ir in range(thread_num):
num.append(ir)
requests = makeRequests(on_start, num)
[pool_list.putRequest(req) for req in requests]
pool_list.wait()
if __name__ == "__main__":
# Process pool
pool = multiprocessing.Pool(processes=processes)
# Set the number of open processes
for i in xrange(processes):
pool.apply_async(thread_web_socket)
pool.close()
pool.join()