Skip to content

Commit

Permalink
Merge pull request #489 from tekii/feature-487
Browse files Browse the repository at this point in the history
added time elapsed between request sent and response received
  • Loading branch information
mhils committed Mar 2, 2015
2 parents 75ba0a9 + 09828ff commit e65a865
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
14 changes: 6 additions & 8 deletions libmproxy/console/common.py
Expand Up @@ -162,7 +162,7 @@ def raw_format_flow(f, focus, extended, padding):
if f["resp_ctype"]:
resp.append(fcol(f["resp_ctype"], rc))
resp.append(fcol(f["resp_clen"], rc))
resp.append(fcol(f["resp_rate"], rc))
resp.append(fcol(f["roundtrip"], rc))

elif f["err_msg"]:
resp.append(fcol(SYMBOL_RETURN, "error"))
Expand Down Expand Up @@ -345,19 +345,17 @@ def format_flow(f, focus, extended=False, hostheader=False, padding=2):
contentdesc = "[content missing]"
else:
contentdesc = "[no content]"

if f.response.timestamp_end:
delta = f.response.timestamp_end - f.response.timestamp_start
else:
delta = 0
duration = 0
if f.response.timestamp_end and f.request.timestamp_start:
duration = f.response.timestamp_end - f.request.timestamp_start
size = f.response.size()
rate = utils.pretty_size(size / ( delta if delta > 0 else 1 ) )
roundtrip = utils.pretty_duration(duration)

d.update(dict(
resp_code = f.response.code,
resp_is_replay = f.response.is_replay,
resp_clen = contentdesc,
resp_rate = "{0}/s".format(rate),
roundtrip = roundtrip,
))
t = f.response.headers["content-type"]
if t:
Expand Down
12 changes: 12 additions & 0 deletions libmproxy/utils.py
Expand Up @@ -79,6 +79,18 @@ def pretty_size(size):
x = int(x)
return str(x) + suf

def pretty_duration(secs):
formatters = [
(100, "{:.0f}s"),
(10, "{:2.1f}s"),
(1, "{:1.2f}s"),
]

for limit, formatter in formatters:
if secs >= limit:
return formatter.format(secs)
#less than 1 sec
return "{:.0f}ms".format(secs*1000)

class Data:
def __init__(self, name):
Expand Down
13 changes: 13 additions & 0 deletions test/test_utils.py
Expand Up @@ -50,6 +50,19 @@ def test_urldecode():
s = "one=two&three=four"
assert len(utils.urldecode(s)) == 2

def test_pretty_duration():
assert utils.pretty_duration(0.00001) == "0ms"
assert utils.pretty_duration(0.0001) == "0ms"
assert utils.pretty_duration(0.001) == "1ms"
assert utils.pretty_duration(0.01) == "10ms"
assert utils.pretty_duration(0.1) == "100ms"
assert utils.pretty_duration(1) == "1.00s"
assert utils.pretty_duration(10) == "10.0s"
assert utils.pretty_duration(100) == "100s"
assert utils.pretty_duration(1000) == "1000s"
assert utils.pretty_duration(10000) == "10000s"
assert utils.pretty_duration(1.123) == "1.12s"
assert utils.pretty_duration(0.123) == "123ms"

def test_LRUCache():
class Foo:
Expand Down

0 comments on commit e65a865

Please sign in to comment.