1

如何加载测试 gevent 套接字?我编写了一个简单的服务器,我想在调整代码之前对其进行负载测试以找出可以进行哪些改进。

负载测试套接字服务器是否有任何细节?我将如何对这段代码进行负载测试,以便在应用调整后比较结果?

from gevent import server
from gevent.monkey import patch_all; patch_all()
import gevent


class Client(object):
    def __init__(self, socket, address, server_handler):
        self.socket = socket
        self.address = address
        self.server_handler = server_handler
        gevent.spawn(self.listen)

    def listen(self):
        f = self.socket.makefile()
        while True:
            line = f.readline()
            if not line:
                print 'client died'
                break
            line = line.rstrip()
            for addr, c in self.server_handler.users.iteritems():
                msg = '<%s> says: %s' % (self.address, line)
                c.send(msg)

            print '<%s>: %s' % (self.address, line)

    def send(self, msg):
        f = self.socket.makefile()
        f.write('%s\r\n' % msg)
        f.flush()


class ServerHandler(object):
    def __init__(self):
        self.users = {}

    def __call__(self, socket, address):
        client = Client(socket, address, self)
        self.users[address] = client
        self.socket = socket
        self.address = address
        print 'connection made'


if __name__ == '__main__':
    server = server.StreamServer(('0.0.0.0', 5000), ServerHandler())
    server.serve_forever()
4

0 回答 0