我已经使用simplehttpserver设置了http服务器。我有2个使用请求库的客户端。两者都连续请求100个文件。一种使用持久性,另一种使用非持久性。发送的文件很小,每个大约20个字节。计算总时间。结果:非持久性比持久性9/10倍快。对此有解释吗?
示例代码 服务器:
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
Handler.protocol_version = "HTTP/1.1"
httpd = SocketServer.TCPServer(("", 8000), Handler)
httpd.serve_forever()
客户:
header = {"Connection" : "close"} #remove for persistant
s = requests.session()
response = sess.get((url) + filename, headers=header)
答案 0 :(得分:0)
我无法重现此内容(Python 3.7)。
有了Connection: close
标头,我的RPS降低了约25%。
import socketserver
import http.server
PORT = 8000
Handler = http.server.SimpleHTTPRequestHandler
Handler.protocol_version = "HTTP/1.1"
httpd = socketserver.TCPServer(("", PORT), Handler)
httpd.serve_forever()
import requests
import time
def test(headers, n=1500):
with requests.session() as sess:
t0 = time.time()
for x in range(n):
assert sess.get('http://127.0.0.1:8000/server.py', headers=headers).content
t1 = time.time()
print(headers, n / (t1 - t0), 'rps')
test({})
test({"Connection": "close"})
test({"Connection": "keep-alive"})
$ python3 client.py
{} 514.4191683333725 rps
{'Connection': 'close'} 424.7938184319337 rps
{'Connection': 'keep-alive'} 492.5251360628307 rps
答案 1 :(得分:0)
我不确定为什么,但是当我运行它时,得到了以下结果
$ python3 client.py{} 22.51256576012429 rps
{'Connection': 'close'} 320.24445743902817 rps
{'Connection': 'keep-alive'} 22.209050176933395 rps