我的python代码调用了REST
上的localhost
服务器。服务器本身会记录指标(即处理时间),并在响应中发回自己的测量结果以进行调试。
由于某种原因,我不知道从python代码调用localhost服务器会有很大的开销。我已经用一些简单的代码测试了时间:
import logging
import requests
import time
import os
os.environ['no_proxy'] = '127.0.0.1,localhost'
# asserting that the connection is being re-used - this prints it out
logging.basicConfig(level=logging.DEBUG)
# re-use the session
s = requests.Session()
# try variations of host to see if there is a difference
localhost = "localhost"
local_ip = "127.0.0.1"
# https://github.com/requests/requests/issues/879
NO_PROXY = {
'no': 'pass',
}
def measure():
url = "http://{}:8888/endpoint".format(localhost)
start = time.time()
response = s.get(url, proxies=NO_PROXY)
end = time.time()
print("took {} ms".format((end-start)*1000))
url = "http://{}:8888/endpoint".format(local_ip)
start = time.time()
response = s.get(url, proxies=NO_PROXY)
end = time.time()
print("took {} ms".format((end-start)*1000))
for i in range(0,1000):
make_request()
ping()
日志看起来像
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): localhost:8888
DEBUG:urllib3.connectionpool:http://localhost:8888 "GET /endpoint HTTP/1.1" 200 4
took 7.76600837708 ms
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): 127.0.0.1:8888
DEBUG:urllib3.connectionpool:http://127.0.0.1:8888 "GET /endpoint HTTP/1.1" 200 4
took 4.73213195801 ms
DEBUG:urllib3.connectionpool:http://localhost:8888 "GET /endpoint HTTP/1.1" 200 4
took 1.47581100464 ms
DEBUG:urllib3.connectionpool:http://127.0.0.1:8888 "GET /endpoint HTTP/1.1" 200 4
took 1.25908851624 ms
DEBUG:urllib3.connectionpool:http://localhost:8888 "GET /endpoint HTTP/1.1" 200 4
took 1.25503540039 ms
DEBUG:urllib3.connectionpool:http://127.0.0.1:8888 "GET /endpoint HTTP/1.1" 200 4
took 1.21402740479 ms
DEBUG:urllib3.connectionpool:http://localhost:8888 "GET /endpoint HTTP/1.1" 200 4
took 1.31893157959 ms
DEBUG:urllib3.connectionpool:http://127.0.0.1:8888 "GET /endpoint HTTP/1.1" 200 4
took 1.54900550842 ms
我还打印了标题,以了解此请求中正在传输多少数据(总共约300个字节):
{'Content-Length': '138', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'User-Agent': 'python-requests/2.19.1','Connection': 'keep-alive', 'debug': 'true', 'Content-Type': 'application/json'}
我看到初始调用很慢,但是后来的调用正在重新使用连接(明显更快),但是仍然,当我与服务器日志关联时,我发现这1.5毫秒是一个相对较长的时间。服务器的速度显着提高,花费了不到0.1毫秒的时间,这使我相信本地主机调用要付出一些严厉的代价。
此外,我们看到"content-length"
很小-在这种情况下为138
Just for comparison with regular "ping" command:
ping localhost
PING localhost (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.051 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.096 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.073 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.089 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.120 ms
64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.078 ms
64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.097 ms
所以快20倍。我意识到它使用的是完全不同的协议,但仍仅作为参考。