Python学习练手小项目:requests 获取接口响应时间

使用elapsed获取响应时间

1、elapsed介绍

官方文档:http://cn.python-requests.org/zh_CN/latest/api.html?highlight=elapsed#requests.Response.elapsed

  • 查看帮助:
import requests

test = requests.get('https://www.qq.com')

print(help(test.elapsed))       # 查看帮助
  • 结果显示:
Help on timedelta object:

class timedelta(builtins.object)
 |  Difference between two datetime values.
 |  
 |  timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
 |  
 |  All arguments are optional and default to 0.
 |  Arguments may be integers or floats, and may be positive or negative.
 |  
 |  Methods defined here:
 |  
 |  __abs__(self, /)
 |      abs(self)
 |  
 |  __add__(self, value, /)
 |      Return self+value.
 |  
 |  __bool__(self, /)
 |      self != 0
 |  
 |  __divmod__(self, value, /)
 |      Return divmod(self, value).
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __floordiv__(self, value, /)
 |      Return self//value.
 |  
 |  __ge__(self, value, /)
 |      Return self>=value.
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __gt__(self, value, /)
 |      Return self>value.
 |  
 |  __hash__(self, /)
 |      Return hash(self).
 |  
 |  __le__(self, value, /)
 |      Return self<=value.
 |  
 |  __lt__(self, value, /)
 |      Return self<value.
 |  
 |  __mod__(self, value, /)
 |      Return self%value.
 |  
 |  __mul__(self, value, /)
 |      Return self*value.
 |  
 |  __ne__(self, value, /)
 |      Return self!=value.
 |  
 |  __neg__(self, /)
 |      -self
 |  
 |  __pos__(self, /)
 |      +self
 |  
 |  __radd__(self, value, /)
 |      Return value+self.
 |  
 |  __rdivmod__(self, value, /)
 |      Return divmod(value, self).
 |  
 |  __reduce__(...)
 |      __reduce__() -> (cls, state)
 |  
 |  __repr__(self, /)
 |      Return repr(self).
 |  
 |  __rfloordiv__(self, value, /)
 |      Return value//self.
 |  
 |  __rmod__(self, value, /)
 |      Return value%self.
 |  
 |  __rmul__(self, value, /)
 |      Return value*self.
 |  
 |  __rsub__(self, value, /)
 |      Return value-self.
 |  
 |  __rtruediv__(self, value, /)
 |      Return value/self.
 |  
 |  __str__(self, /)
 |      Return str(self).
 |  
 |  __sub__(self, value, /)
 |      Return self-value.
 |  
 |  __truediv__(self, value, /)
 |      Return self/value.
 |  
 |  total_seconds(...)
 |      Total seconds in the duration.
 |  
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  days
 |      Number of days.
 |  
 |  microseconds
 |      Number of microseconds (>= 0 and less than 1 second).
 |  
 |  seconds
 |      Number of seconds (>= 0 and less than 1 day).
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  max = datetime.timedelta(days=999999999, seconds=86399, microseconds=9...
 |  
 |  min = datetime.timedelta(days=-999999999)
 |  
 |  resolution = datetime.timedelta(microseconds=1)

  • 内置方法介绍
import requests

test = requests.get('https://www.qq.com')

help(test.elapsed)
print(test.elapsed.seconds)
print(test.elapsed.days)
print(test.elapsed.min)
print(test.elapsed.max)
print(test.elapsed.total_seconds())
print(test.elapsed.microseconds)
print(test.elapsed.resolution)

返回结果:
0
0
-999999999 days, 0:00:00
999999999 days, 23:59:59.999999
0.071056
71056
0:00:00.000001

  • 小结
    网上提出获取接口响应时间均为:test.elapsed.microseconds/(1000*1000),但是该种方式有个缺点(当接口的响应时间大于1s时,仅会截取后面的小数位)
    所以推荐使用:test.elapsed.total_seconds(),准确性较高,单位为s

注:1 秒(second)[s] = 1000 毫秒millisecond[ms] = 1000000 微秒microsecond[us]


Blog:

推荐阅读更多精彩内容