我具有基于paste.httpserver作为之间的HTTP和WSGI一个adapater的web服务器。当我做性能测试的httperf用,我可以,如果我使用--num - 康恩每次开始一个新的请求做每秒超过1,000个请求。如果我使用--num呼叫代替重用连接然后我得到大约每秒11个请求,1 /第100次的速度。

如果我尝试AB我收到了超时。

我测试

% ./httperf --server localhost --port 8080 --num-conn 100
...
Request rate: 1320.4 req/s (0.8 ms/req)
...

% ./httperf --server localhost --port 8080 --num-call 100
...
Request rate: 11.2 req/s (89.4 ms/req)
...

下面是一个简单的可重复的服务器

from paste import httpserver

def echo_app(environ, start_response):
    n = 10000
    start_response("200 Ok", [("Content-Type", "text/plain"),
                              ("Content-Length", str(n))])
    return ["*" * n]

httpserver.serve(echo_app, protocol_version="HTTP/1.1")

这是一个多线程服务器,这是很难的轮廓。这里的一个变型,其是单线程:

from paste import httpserver

class MyHandler(httpserver.WSGIHandler):
    sys_version = None
    server_version = "MyServer/0.0"
    protocol_version = "HTTP/1.1"

    def log_request(self, *args, **kwargs):
        pass


def echo_app(environ, start_response):
    n = 10000
    start_response("200 Ok", [("Content-Type", "text/plain"),
                              ("Content-Length", str(n))])
    return ["*" * n]

# WSGIServerBase is single-threaded
server = httpserver.WSGIServerBase(echo_app, ("localhost", 8080), MyHandler)
server.handle_request()

剖析与

% python2.6 -m cProfile -o paste.prof paste_slowdown.py

和与打它

%httperf --client=0/1 --server=localhost --port=8080 --uri=/ \ 
   --send-buffer=4096 --recv-buffer=16384 --num-conns=1 --num-calls=500

我得到这样的简档

>>> p=pstats.Stats("paste.prof")
>>> p.strip_dirs().sort_stats("cumulative").print_stats()
Sun Nov 22 21:31:57 2009    paste.prof

         109749 function calls in 46.570 CPU seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000   46.571   46.571 {execfile}
        1    0.001    0.001   46.570   46.570 paste_slowdown.py:2(<module>)
        1    0.000    0.000   46.115   46.115 SocketServer.py:250(handle_request)
        1    0.000    0.000   44.675   44.675 SocketServer.py:268(_handle_request_noblock)
        1    0.000    0.000   44.675   44.675 SocketServer.py:301(process_request)
        1    0.000    0.000   44.675   44.675 SocketServer.py:318(finish_request)
        1    0.000    0.000   44.675   44.675 SocketServer.py:609(__init__)
        1    0.000    0.000   44.675   44.675 httpserver.py:456(handle)
        1    0.001    0.001   44.675   44.675 BaseHTTPServer.py:325(handle)
      501    0.006    0.000   44.674    0.089 httpserver.py:440(handle_one_request)
     2001    0.020    0.000   44.383    0.022 socket.py:373(readline)
      501   44.354    0.089   44.354    0.089 {method 'recv' of '_socket.socket' objects}
        1    1.440    1.440    1.440    1.440 {select.select}
         ....

您可以看到,几乎所有的时间是在recv的。

我决定保释httpref和我自己写的HTTP / 1.1与 - 保持活动的请求,并使用netcat的发送:

GET / HTTP/1.1
Location: localhost
Connection: Keep-Alive
Content-Length: 0

GET / HTTP/1.1
Location: localhost
Connection: Keep-Alive
Content-Length: 0

 ... repeat 97 more times, to have 99 keep-alives in total ...

GET / HTTP/1.1
Location: localhost
Connection: Close
Content-Length: 0

我与发送

nc localhost 8080 < ~/src/send_to_paste.txt

总时间为100个请求为0.03秒,所以这是非常不错的表现。

这表明的httperf做得不对(但它是一种广泛使用的和推崇一段代码),所以我试图“AB”

% ab -n 100 -k localhost:8080/
This is ApacheBench, Version 1.3d <$Revision: 1.73 $> apache-1.3
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)...
Server timed out

: Operation now in progress

插装服务器,它处理一个请求并正在等待所述第二

这是怎么回事的任何想法?

有帮助吗?

解决方案

经过努力,这似乎是要么 Nagle算法或延迟ACK,或者它们之间的相互作用。它消失了,如果我这样做

server.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

我怎么追查?首先,我在仪器每socket.py“的recv”,所以我可以找出其中的recv等待。我会看到大约5的recv的出中有11个的几乎200毫秒的延迟。我无法弄清楚,为什么有任何拖延。然后我用Wireshark来观察消息,并注意到,它实际上是从服务器到有延迟的客户端发送。这意味着什么在从我的客户端中的传出消息TCP层。

一个朋友建议的明显,我搜索“200ms的插座延迟”,发现这个问题的描述。

在糊TRAC报告是在 http://trac.pythonpaste.org/pythonpaste/ticket/用补丁使TCP_NODELAY当处理程序使用HTTP / 1.1。

沿着392
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top