开源的是一个测试客户端,可以用来检验服务器端程序的并发处理能力。
使用方法为:
python epolltest.py --host=192.168.15.128 --port=8809 --degree=200
其中degree为并发线程数
python 版本为2.6
代码如下
from Queue import Queue
from threading import Thread
from subprocess import check_call
from os import *
import socket
import threading
import exceptions
import sys
import getopt
def do_stuff(q,host,port):
s = socket.socket() # Create a socket object
s.connect((host, int(port)))
while True:
try:
value=q.get()
except:
return
# call(["telnet", "192.168.15.128", "8088" , "<" , "echo.txt"])
# check_call("sh" +" autosend.sh" , shell=True)
s.send("suck me\n")
# print threading.current_thread(), value
s.close # Close the socket when done
q.task_done()
def main(argv):
try:
opts, args = getopt.getopt(argv,"h:p:d:",["host=","port=","degree="])
except getopt.GetoptError:
print "python epolltest.py --host=<ip> --port=<port> --degree=<degree>"
sys.exit(2)
for opt, arg in opts:
if opt=="--host":
host=arg
elif opt=="--port":
port=arg
elif opt=="--degree":
degree=arg
q = Queue(maxsize=0)
print degree
for i in range(int(degree)):
worker = Thread(target=do_stuff, args=(q,host,port ))
worker.setDaemon(True)
worker.start()
x=0
while x<=10000000:
q.put(x)
x+=1
q.join()
print "finish"
if __name__ == "__main__":
main(sys.argv[1:])