使用 Autobahn Testsuite 进行 Websocket 压力测试 [英] Websocket stress test with Autobahn Testsuite

查看:107
本文介绍了使用 Autobahn Testsuite 进行 Websocket 压力测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试对我的 websocket 服务器进行一些压力测试.在客户端,我运行以下脚本 从此站点 :

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)

但运行此脚本后,服务器端没有建立连接.服务器似乎配置正确,因为当我使用不同的客户端(例如 Web 浏览器)连接到我的服务器时,它工作正常并建立了 websocket 连接.我还检查了网络嗅探器,似乎脚本没有产生任何 websocket 连接.我在这个脚本中做错了什么?

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?

推荐答案

您使用的 massconnect.py 脚本应该从 autobahntestsuite 的另一部分调用,例如 wstest 命令:

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

如果你想直接复制massconnect,我认为它缺少启动Twisted延迟任务的命令:

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

并检查您的 Python 缩进,要么在此处粘贴时其中一些缩进已损坏,要么原始代码在某些类和函数定义中缩进不正确.

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.

这篇关于使用 Autobahn Testsuite 进行 Websocket 压力测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆