AnxietyMonger
AnxietyMonger

Reputation: 3

Huge gap between two methods of measuring request time via python-requests

I am trying to measure the respone time of a certain request using python-requests.

import requests
import time

start = time.time()
r = requests.get("https://www.dl.soc.i.kyoto-u.ac.jp/index.php/members/")
end = time.time()

print(end - start)
print(r.elapsed.seconds)

It gave me a result of

64.67747116088867
0.631163

Could anyone please explain the reason of this huge gap? Thanks. By the way, when I was trying the same request on Google-Chrome, actually the first result is what I want.

Upvotes: 0

Views: 1467

Answers (1)

Alfe
Alfe

Reputation: 59436

I made some test with an artificially delaying webserver:

nc -l 8080

Then in another terminal in a Python session:

import time, requests
a=time.time()
r = requests.get("http://localhost:8080/")
b=time.time()
print r.elapsed, b-a

Pasting this issued this HTTP request on the server terminal:

GET / HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: python-requests/2.9.1

I waited for 5 seconds, then I pasted this reply in the server:

HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 12
Connection: keep-alive
Keep-Alive: timeout=15
Date: Thu, 04 Jan 2018 10:12:09 GMT
Server: Apache
Last-Modified: Wed, 09 Dec 2015 13:57:24 GMT
ETag: "28bd-52677784b6090"
Accept-Ranges: bytes

hello

I stated 12 bytes but only sent 6 (hello\n), so this was unfinished. I waited another five seconds, then pasted this text:

world

This finished the reply with the remaining six bytes (world\n). In the client terminal I saw the result appear:

0:00:05.185509 10.8904578686

So, obviously the r.elapsed is the Time-To-First-Byte (TTFB) while the call to requests.get() only terminates after the whole message has been received (Time-To-Last-Byte, TTLB).

Upvotes: 1

Related Questions