Skip to content
Myr

Time how long each request takes

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Stored as attribute Response.time_taken

authored by Chris Lee · committed by Chris Lee · parent ac5e680 · Browse files · 3 files+12 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+12 added−0 removed

  • other:+12 added−0 removed

3 files+12 added−0 removed

modified:requests/adapters.py+4 added
7 unmodified lines
8and maintain connections.9"""1011import socket12import time1314from .models import Response15from .packages.urllib3.poolmanager import PoolManager, ProxyManager16from .packages.urllib3.response import HTTPResponse
138 unmodified lines
155 self.cert_verify(conn, request.url, verify, cert)156 url = self.request_url(request, proxies)157158 chunked = not (request.body is None or 'Content-Length' in request.headers)159 start = time.time()160161 try:162 if not chunked:163 resp = conn.urlopen(
35 unmodified lines
199 connection=low_conn,200 preload_content=False,201 decode_content=False202 )203 finish = time.time()204205 except socket.error as sockerr:206 raise ConnectionError(sockerr)207
8 unmodified lines
216 else:217 raise Timeout('Request timed out.')218219 r = self.build_response(request, resp)220 r.time_taken = finish - start221222 if not stream:223 r.content224
1 unmodified line
modified:requests/models.py+3 added
469 unmodified lines
470471 #: A CookieJar of Cookies the server sent back.472 self.cookies = cookiejar_from_dict({})473474 #: The amount of time taken for the response to arrive (in seconds).475 self.time_taken = 0.0476477 def __repr__(self):478 return '<Response [%s]>' % (self.status_code)479480 def __bool__(self):
182 unmodified lines
modified:test_requests.py+5 added
321 unmodified lines
322 self.assertEqual(cookie.secure, secure)323 self.assertEqual(cookie.domain, domain)324 self.assertEqual(cookie._rest['HttpOnly'], rest['HttpOnly'])325326 def test_time_taken_blank(self):327 r = requests.get(httpbin('get'))328 self.assertTrue(r.time_taken > 0.0)329330331if __name__ == '__main__':332 unittest.main()