1

我有一个用 python 实现的简单 HTTP 服务器,如下所示:

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer

class Handler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-Type', 'text/html')
        self.end_headers()
        self.wfile.write('Hello World!')

if __name__ == '__main__':
    try:
        server = HTTPServer(('', 8000), Handler)
        print 'Listening on 0.0.0.0:8000'
        server.serve_forever()
    except KeyboardInterrupt:
        print '^C, halting'
        server.socket.close()

然后我运行并发级别为 50 的 apache bench:

ab -v 3 -c 50 -n 50000 http://localhost:8000/

我开始在 apache bench 中收到此错误:

apr_socket_recv:对等方重置连接(104)

在 HTTP 服务器控制台上,我看到了这个:

Exception happened during processing of request from ('127.0.0.1', 51545)
Traceback (most recent call last):

  File "/usr/lib/python2.6/SocketServer.py", line 281, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python2.6/SocketServer.py", line 307, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python2.6/SocketServer.py", line 320, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python2.6/SocketServer.py", line 615, in __init__
    self.handle()
  File "/usr/lib/python2.6/BaseHTTPServer.py", line 329, in handle
    self.handle_one_request()
  File "/usr/lib/python2.6/BaseHTTPServer.py", line 323, in handle_one_request
    method()
  File "./test.py", line 12, in do_GET
    self.send_response(200)
  File "/usr/lib/python2.6/BaseHTTPServer.py", line 384, in send_response
    self.send_header('Server', self.version_string())
  File "/usr/lib/python2.6/BaseHTTPServer.py", line 390, in send_header
    self.wfile.write("%s: %s\r\n" % (keyword, value))
  File "/usr/lib/python2.6/socket.py", line 300, in write
    self.flush()
  File "/usr/lib/python2.6/socket.py", line 286, in flush
    self._sock.sendall(buffer)
error: [Errno 32] Broken pipe

我还注意到 HTTPServer 变得非常缓慢

我试图request_queue_size通过这样做来增加 HTTPServer :

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
class MyServer(HTTPServer):
    request_queue_size = 100
    timeout = None


class Handler(BaseHTTPRequestHandler):
    counter = 0

    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-Type', 'text/html')
        self.end_headers()
        self.wfile.write(str(self.counter))
        Handler.counter += 1

if __name__ == '__main__':
    try:
        server = MyServer(('', 8000), Handler)
        print 'Listening on 0.0.0.0:8000'
        server.serve_forever()
    except KeyboardInterrupt:
        print '^C, halting'
        server.socket.close()

但我仍然偶尔会在测试运行结束时遇到管道损坏错误。如果我增加到request_queue_size一个巨大的数字,那么错误就会消失。我不明白为什么,因为 Apache bench-c值是 50,request_queue_size而 100 应该就足够了。为什么request_queue_size需要比 Apache bench 并发设置大得多?即使前一个尚未完成,Apache bench 是否会触发更多连接,因此并发连接的总数可能超过-c设置?

4

1 回答 1

0

这是 apachebench 的问题,但不是 httperf 的问题。我不太确定他们处理连接的方式有什么不同。

从默认值(5)增加到 1000。我没有注意到内存有任何巨大的爆炸,性能显着提高,同时消除了错误。

于 2016-03-18T15:15:10.443 回答