root/branches/network/stresstest.py

Revision 424 (by dkeeney, 12/08/07 13:22:13)

add spinner and remove cruft comments

import sys, time

import pausingreactor; pausingreactor.install()

from twisted.internet import reactor
from twisted.internet.protocol import Protocol, ClientFactory
from twisted.protocols.basic import LineReceiver, NetstringReceiver


class StressTester(NetstringReceiver):
    """Sends increasingly long strings. """

    def __init__(self, *args):
        NetstringReceiver.MAX_LENGTH = 5000000
        self.testString = 'abcdef'
        self._dataStarted = False
        print 'maxlen', self.MAX_LENGTH

    def sendMessage(self, msg):
        #self.transport.write("MESSAGE %s\r\n" % msg)
        self.sendString(msg)
        
    def sendTestMessage(self):
        print '#',
        slen = len(self.testString)
        self.testString += self.testString[:int(slen/2)]
        self.sendMessage(self.testString)
        
    def dataReceived(self, data):
        if not self._dataStarted:
            print '>',
            self._dataStarted = True
        NetstringReceiver.dataReceived(self, data)
        
    def stringReceived(self, line):
        #sys.stdout.write(line)
        limValue = self.MAX_LENGTH / 1.5
        self._dataStarted = False
        tmp = self.testString.upper()
        if line == tmp:
            print 'test good:', len(tmp)
            if len(tmp) < limValue:
                reactor.callLater(0, self.sendTestMessage)
            else:
                print 'done'
                reactor.stop()
        else:
            print 'test bad'
            print 'line:', line
            print 'tmp:', tmp
            reactor.stop()

    def connectionMade(self):
        print 'connection was made'
        reactor.callLater(0.1, self.sendTestMessage)


class StresserClientFactory(ClientFactory):

    protocol = StressTester
    
    def startedConnecting(self, connector):
        print 'Started to connect.'

    def clientConnectionLost(self, connector, reason):
        print 'Lost connection. ' # Reason:', reason

    def clientConnectionFailed(self, connector, reason):
        print 'Connection failed. Reason:', reason

spinct = 0
def spinner():
    global spinct
    spinct += 1
    reactor.callLater(0, spinner)

def skipper():
    reactor.callLater(0.5, skipper)
    reactor.release()

cf = StresserClientFactory()
reactor.connectTCP('localhost', 1079, cf)

reactor.callLater(0.25, skipper)
reactor.callLater(0.25, spinner)

begin = time.time()
reactor.run()

while reactor.isRunning():
    print '!',
    time.sleep(0.25)
    reactor.resume()    
    
#
end = time.time()
print 'spinct', spinct, 'rate', int(1.0*spinct/(end-begin))
Note: See TracBrowser for help on using the browser.