Websocket stress test with Autobahn Testsuite

筅森魡賤 提交于 2019-12-13 04:37:31

问题


I try to do some stress test against my websocket server. On client side I run the following script from this site :

 import time, sys
 from twisted.internet import defer, reactor
 from twisted.internet.defer import Deferred, returnValue, inlineCallbacks

 from autobahn.twisted.websocket import connectWS, \
                                   WebSocketClientFactory, \
                                   WebSocketClientProtocol


 class MassConnectProtocol(WebSocketClientProtocol):

  didHandshake = False

  def onOpen(self):

    print("websocket connection opened")
    self.factory.test.onConnected()
    self.factory.test.protos.append(self)
    self.didHandshake = True


class MassConnectFactory(WebSocketClientFactory):

 protocol = MassConnectProtocol

  def clientConnectionFailed(self, connector, reason):
   if self.test.onFailed():
     reactor.callLater(float(self.retrydelay)/1000., connector.connect)

  def clientConnectionLost(self, connector, reason):
   if self.test.onLost():
     reactor.callLater(float(self.retrydelay)/1000., connector.connect)


class MassConnect:

  def __init__(self, name, uri, connections, batchsize, batchdelay, retrydelay):
   print('MassConnect init')
   self.name = name
   self.uri = uri
   self.batchsize = batchsize
   self.batchdelay = batchdelay
   self.retrydelay = retrydelay
   self.failed = 0
   self.lost = 0
   self.targetCnt = connections
   self.currentCnt = 0
   self.actual = 0
   self.protos = []

 def run(self):
   print('MassConnect runned')
   self.d = Deferred()
   self.started = time.clock()
   self.connectBunch()
   return self.d

 def onFailed(self):
   self.failed += 1
   sys.stdout.write("!")
   return True

 def onLost(self):
   self.lost += 1
   #sys.stdout.write("*")
   return False
   return True

 def onConnected(self):
   print("onconnected")
   self.actual += 1
   if self.actual % self.batchsize == 0:
     sys.stdout.write(".")
   if self.actual == self.targetCnt:
     self.ended = time.clock()
     duration = self.ended - self.started
     print " connected %d clients to %s at %s in %s seconds (retries %d = failed %d + lost %d)" % (self.currentCnt, self.name, self.uri, duration, self.failed + self.lost, self.failed, self.lost)
     result = {'name': self.name,
               'uri': self.uri,
               'connections': self.targetCnt,
               'retries': self.failed + self.lost,
               'lost': self.lost,
               'failed': self.failed,
               'duration': duration}
     for p in self.protos:
        p.sendClose()
     #self.d.callback(result)

  def connectBunch(self):
    if self.currentCnt + self.batchsize < self.targetCnt:
      c = self.batchsize
      redo = True
    else:
      c = self.targetCnt - self.currentCnt
      redo = False
    for i in xrange(0, c):
      factory = MassConnectFactory(self.uri)
      factory.test = self
      factory.retrydelay = self.retrydelay
      connectWS(factory)
      self.currentCnt += 1
    if redo:
      reactor.callLater(float(self.batchdelay)/1000., self.connectBunch)


class MassConnectTest:
  def __init__(self, spec):
   self.spec = spec
   print('MassConnetest init')
 @inlineCallbacks
 def run(self):
  print self.spec
  res = []
  for s in self.spec['servers']:
     print s['uri']
     t = MassConnect(s['name'],
                     s['uri'],
                     self.spec['options']['connections'],
                     self.spec['options']['batchsize'],
                     self.spec['options']['batchdelay'],
                     self.spec['options']['retrydelay'])
     r = yield t.run()
     res.append(r)
  returnValue(res)


def startClient(spec, debug = False):
  test = MassConnectTest(spec)
  d = test.run()
  return d

if __name__ == '__main__':

  spec = {}
  spec['servers'] = [{'name': 'test', 'uri':"ws://127.0.0.1:8080"} ]
  spec['options'] ={'connections': 1000,'batchsize': 500, 'batchdelay': 1000, 'retrydelay': 200 }
  startClient(spec,False)

But after running this script there are no connections established on the server side. Server seems to be configured properly, because when I connect to my server using different client side (for example web browser), it works fine and websocket connection is established. I also checked network sniffer and it seems that script doesn't produce any websocket connections. What did I do wrong in this script?


回答1:


The massconnect.py script you used was supposed to be invoked from another part of the autobahntestsuite, such as the wstest command:

$ echo '{"servers": [{"name": "test", "uri":"ws://127.0.0.1:8080"} ], "options": {"connections": 1000,"batchsize": 500, "batchdelay": 1000, "retrydelay": 200 }}' > spec.json
$ wstest -m massconnect --spec spec.json

If you want to copy massconnect directly, I think it's missing the command to start the Twisted deferred tasks:

if __name__ == '__main__':
  spec = {}
  spec['servers'] = [{'name': 'test', 'uri':"ws://127.0.0.1:8080"} ]
  spec['options'] ={'connections': 1000,'batchsize': 500, 'batchdelay': 1000, 'retrydelay': 200 }
  startClient(spec,False)
  reactor.run() # <-- add this

And check your Python indentations, either some of them got corrupted when pasting here, or the original code had incorrect indentations in some class and function definitions.



来源:https://stackoverflow.com/questions/33318976/websocket-stress-test-with-autobahn-testsuite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!