2009-10-31 97 views
3

我已經寫了一個基於扭曲的服務器,我想用扭曲來測試它。測試扭曲的應用程序 - 加載客戶端

但我想寫一個負載測試,同時開始一堆請求。

但我相信,我沒有得到扭轉,主要是客戶端的概念,因爲我stucked這個問題:

from twisted.internet import reactor, protocol 
from threading import Thread 
from twisted.protocols.basic import LineReceiver 

__author__="smota" 
__date__ ="$30/10/2009 17:17:50$" 

class SquitterClient(LineReceiver): 

    def connectionMade(self): 
     self.sendLine("message from " % threading.current_thread().name); 
     pass 

    def connectionLost(self, reason): 
     print "connection lost" 

    def sendMessage(self, msg): 
     for m in [ "a", "b", "c", "d", "e"]: 
      self.sendLine(msg % " - " % m); 

class SquitterClientFactory(protocol.ClientFactory): 
    protocol = SquitterClient 

    def clientConnectionFailed(self, connector, reason): 
     print "Connection failed - goodbye!" 
     reactor.stop() 

    def clientConnectionLost(self, connector, reason): 
     print "Connection lost - goodbye!" 
     reactor.stop() 

def createAndRun(): 
    f = SquitterClientFactory() 
    reactor.connectTCP("localhost", 4010, f) 
    reactor.run(installSignalHandlers=0) 

# this connects the protocol to a server runing on port 8000 
def main(): 
    for n in range(0,10): 
     th=Thread(target=createAndRun) 
     th.start() 

# this only runs if the module was *not* imported 
if __name__ == '__main__': 
    main() 

socket_client.py:35: DeprecationWarning:反應器已經運行了 !因爲扭曲的8.0
reactor.run(installSignalHandlers = 0)

我缺少這個特性已經被廢棄 ?

如何測試它?

謝謝

塞繆爾

回答

9

的直接原因爲你的失敗是你學嘗試調用run()的反應堆上多次。你應該只調用run()一次。我認爲你期待有多個反應堆,每個反應堆都在自己的線程中,但實際上你只有一個反應堆。壞事是擁有多個反應器是困難的或不可能的 - 好處是它也是不必要的。事實上,你甚至不需要多個線程。您可以在一個反應​​器中複用多個客戶端連接,就像您可以監聽多個連接一樣。

修改您的示例代碼,像下面這樣的應該工作。關鍵的想法是,你不需要多個反應堆並行地完成任務。無論如何,唯一能夠與常規Python實現並行的是I/O。

from twisted.internet import reactor, protocol 
from twisted.protocols.basic import LineReceiver 

__author__="smota" 
__date__ ="$30/10/2009 17:17:50$" 

class SquitterClient(LineReceiver): 
    def connectionMade(self): 
     self.messageCount = 0 
     # The factory provides a reference to itself, we'll use it to enumerate the clients 
     self.factory.n += 1 
     self.name = "Client %d" %self.factory.n 

     # Send initial message, and more messages a bit later 
     self.sendLine("Client %s starting!" % self.name); 
     reactor.callLater(0.5, self.sendMessage, "Message %d" %self.messageCount) 

    def connectionLost(self, reason): 
     print "connection lost" 

    def sendMessage(self, msg): 
     for m in [ "a", "b", "c", "d", "e"]: 
      self.sendLine("Copy %s of message %s from client %s!" % (m, msg, self.name)) 
     if self.factory.stop: 
      self.sendLine("Client %s disconnecting!" % self.name) 
      self.transport.loseConnection() 
     else: 
      self.messageCount += 1 
      reactor.callLater(0.5, self.sendMessage, "Message %d" %self.messageCount) 

class SquitterClientFactory(protocol.ClientFactory): 
    protocol = SquitterClient 

    def __init__(self): 
     self.n = 0 
     self.stop = False 

    def stopTest(): 
     self.stop = True 

    def clientConnectionFailed(self, connector, reason): 
     print "Connection failed - goodbye!" 

    def clientConnectionLost(self, connector, reason): 
     print "Connection lost - goodbye!" 

# this connects the protocol to a server running on port 8000 
def main(): 
    # Create 10 clients 

    f = SquitterClientFactory() 
    for i in range(10): 
     reactor.connectTCP("localhost", 8000, f) 

    # Schedule end of test in 10 seconds 
    reactor.callLater(10, f.stopTest) 

    # And let loose the dogs of war 
    reactor.run() 

# this only runs if the module was *not* imported 
if __name__ == '__main__': 
    main() 
+2

您可能還想指出多個/線程/也不是必需的。 – 2009-11-01 14:20:33

+1

添加關於線程的註釋也是不必要的。 – 2009-11-01 14:34:42