I am testing a small webserver in python 3.8 using the Bottle framework on Windows 10 and the response times feel very sluggish.
After boiling down the example to a minimum the server looks like this:
from bottle import *
import time
@route("/now")
def time_service():
response.content_type = 'text/plain'
response.set_header('Cache-Control', 'max-age=10')
return time.ctime()
if __name__ == '__main__':
run(host='localhost', port=8080, debug=True, reloader=False)
Then a simple benchmark revealed giant response times:
def get_resource():
with urllib.request.urlopen('http://localhost:8080/now') as response:
html = response.read()
return html
%timeit get_resource()
2.03 s ± 3.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
So this seems very odd, so I tried replacing the name lookup with a fixed ip value:
def get_resource():
with urllib.request.urlopen('http://127.0.0.1:8080/now') as response:
html = response.read()
return html
%timeit get_resource()
512 µs ± 14.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
Here we observe a decrease of 4000x in the response time. Where is the bottleneck here? Certainly it can't take 2 seconds to perform a name lookup on localhost?