python – 测试扭曲的应用程序 – 加载客户端
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 测试扭曲的应用程序 – 加载客户端,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3471字,纯文字阅读大概需要5分钟。
内容图文
我写过一个基于Twisted的服务器,我也想用扭曲测试它.
但我想写一个负载测试,同时启动一堆请求.
但我相信我没有得到Twisted的概念,主要是客户端,因为我遇到了这个问题:
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: Reactor already
running! This behavior is deprecated
since Twisted 8.0
reactor.run(installSignalHandlers=0)
我错过了什么?
怎么测试呢?
谢谢,
塞缪尔
解决方法:
失败的直接原因是您尝试多次调用reactor上的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()
内容总结
以上是互联网集市为您收集整理的python – 测试扭曲的应用程序 – 加载客户端全部内容,希望文章能够帮你解决python – 测试扭曲的应用程序 – 加载客户端所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。