Skip to content

Commit bdfd95b

Browse files
committed
LFRequest.py : Track last_elapsed_ms around urlopen() for API call duration
Signed-off-by: Sidartha-CT <neelapu.sidartha@candelatech.com>
1 parent 99bf697 commit bdfd95b

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

py-json/LANforge/LFRequest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import logging
88
import sys
99
import os
10+
import time
1011
from datetime import datetime
1112
from pprint import pformat, PrettyPrinter
1213
import urllib
@@ -45,6 +46,7 @@ def __init__(self, url=None,
4546
self.last_response_code = None
4647
self.last_diagnostics = None
4748
self.last_sent_at = None
49+
self.last_elapsed_ms = None
4850

4951
# please see this discussion on ProxyHandlers:
5052
# https://docs.python.org/3/library/urllib.request.html#urllib.request.ProxyHandler
@@ -188,8 +190,10 @@ def json_post(self, show_error=True, debug=False, die_on_error_=False, response_
188190
# https://stackoverflow.com/a/59635684/11014343
189191

190192
self.last_sent_at = datetime.now()
193+
_t0 = time.perf_counter()
191194
try:
192195
resp = urllib.request.urlopen(myrequest)
196+
self.last_elapsed_ms = (time.perf_counter() - _t0) * 1000
193197
self.last_response_code = getattr(resp, 'status', None)
194198
resp_data = resp.read().decode('utf-8')
195199
if debug or die_on_error_:
@@ -214,6 +218,7 @@ def json_post(self, show_error=True, debug=False, die_on_error_=False, response_
214218
return responses[0]
215219

216220
except urllib.error.HTTPError as error:
221+
self.last_elapsed_ms = (time.perf_counter() - _t0) * 1000
217222
self.last_response_code = error.code
218223
self.last_diagnostics = print_diagnostics(url_=self.requested_url,
219224
request_=myrequest,
@@ -222,6 +227,7 @@ def json_post(self, show_error=True, debug=False, die_on_error_=False, response_
222227
debug_=debug)
223228

224229
except urllib.error.URLError as uerror:
230+
self.last_elapsed_ms = (time.perf_counter() - _t0) * 1000
225231
self.last_diagnostics = print_diagnostics(url_=self.requested_url,
226232
request_=myrequest,
227233
responses_=responses,
@@ -257,11 +263,14 @@ def get(self, method_='GET'):
257263
method=method_)
258264
myresponses = []
259265
self.last_sent_at = datetime.now()
266+
_t0 = time.perf_counter()
260267
try:
261268
myresponses.append(request.urlopen(myrequest))
269+
self.last_elapsed_ms = (time.perf_counter() - _t0) * 1000
262270
return myresponses[0]
263271

264272
except urllib.error.HTTPError as error:
273+
self.last_elapsed_ms = (time.perf_counter() - _t0) * 1000
265274
self.last_response_code = error.code
266275
self.last_diagnostics = print_diagnostics(url_=self.requested_url,
267276
request_=myrequest,
@@ -271,6 +280,7 @@ def get(self, method_='GET'):
271280
debug_=self.debug)
272281

273282
except urllib.error.URLError as uerror:
283+
self.last_elapsed_ms = (time.perf_counter() - _t0) * 1000
274284
self.last_diagnostics = print_diagnostics(url_=self.requested_url,
275285
request_=myrequest,
276286
responses_=myresponses,

0 commit comments

Comments
 (0)