tcp压力测试脚本

该脚本用于从指定的文件中读取数据包,然后在特定的时间内,向tcp服务器发送指定数量的数据包,以测试服务的性能

#!/usr/bin/python

import socket
import sys
import time

f = open('./send.dat', 'r')
data = f.read()
#数据包数量
num = 12
#发送包的持续时间
howlong=60.0
minInterval = (float)(howlong / num)
lastTimeCalled = time.time()
accDelay=0.0

startTime=time.time()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 4500))

while num > 0:
    s.send(data)
    #rvcdata = s.recv(2048)
    #print(rvcdata)
    num -= 1
    elapsed = time.time() - lastTimeCalled
    curDelay = accDelay-(minInterval-elapsed)
    print curDelay
    if(curDelay < 0):
        time.sleep(-curDelay)
        accDelay = 0.0
    else:
        accDelay = curDelay
    lastTimeCalled = time.time()
    #sys.exit(0)
s.close()
timeCost = time.time()-startTime
print("time cost:%f" %timeCost)

你可能感兴趣的:(tcp压力测试脚本)