如何对基于Redis、Twisted和iPhone应用的客户端/服务器进行负载测试
我想要模拟成千上万的客户端同时连接到我的服务器,看看服务器能不能承受得住。现在我只是想用我的iPhone和iPhone模拟器来创建连接,但这并不是一个实时的模拟。我该如何进行负载测试呢?
下面是我的服务器代码的一个例子:
from twisted.internet.protocol import Factory, Protocol
from twisted.internet import defer
class STSFactory(Factory):
def __init__(self,conn):
self.conn = conn
self.protocol = STSProtocol
class STSProtocol(Protocol):
def log(self, message):
print "%s: %s" % (self, message)
def connectionMade(self):
self.log("Connection made")
def connectionLost(self, reason):
self.factory.clients.remove(self)
self.log("Connection Lost")
@defer.inlineCallbacks
def getUser(self,user):
val = yield self.factory.conn.hgetall("user:%s"%user)
def dataReceived(self, data):
cmd = data.split(':')
command = cmd[0]
arg1 = cmd[1]
arg2 = cmd[2]
if arg1 == "logon":
self.getUser(arg2)
if __name__ == '__main__':
from twisted.internet import reactor
import redis
conn = redis.Redis(unix_socket_path='/tmp/redis.sock')
factory = STSFactory(conn)
factory.clients = []
print "Server started"
reactor.listenTCP(11000,factory)
reactor.listenTCP(11001,factory)
reactor.listenTCP(11002,factory)
reactor.run()
2 个回答
3
有一个很棒的压力测试工具,叫做 Locust。你可以用Python编写测试场景,它可以根据需要扩展,而且如果你需要的话,还有一个基于网页的图形界面。
1
你是在哪里托管你的服务器的?如果情况简单的话,你可以在本地电脑上使用Siege这个工具(对那些会收到大部分请求的URL进行测试) http://www.joedog.org/index/siege-home
你也可以使用像Blitz.io这样的服务 http://blitz.io/,这样你就可以从不同的地理位置来测试服务(这对性能影响很大)。类似这样的服务有很多,只要搜索“网页负载测试”就能找到。我喜欢Blitz,因为我用的是Heroku,它们的结合非常好。