From 3a78c95d0a202b486d062951ee0593f43a16eac2 Mon Sep 17 00:00:00 2001 From: Marcelo Glezer Date: Thu, 26 Feb 2015 18:14:20 -0300 Subject: [PATCH 1/2] added to flowlist / flowdetail time elapsed between request sent and response received --- libmproxy/console/common.py | 7 ++++++- libmproxy/utils.py | 12 ++++++++++++ test/test_utils.py | 13 +++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/libmproxy/console/common.py b/libmproxy/console/common.py index a2cfd57b3..8f074cfdd 100644 --- a/libmproxy/console/common.py +++ b/libmproxy/console/common.py @@ -163,6 +163,7 @@ def raw_format_flow(f, focus, extended, padding): 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")) @@ -345,19 +346,23 @@ def format_flow(f, focus, extended=False, hostheader=False, padding=2): contentdesc = "[content missing]" else: contentdesc = "[no content]" - + duration = 0 if f.response.timestamp_end: delta = f.response.timestamp_end - f.response.timestamp_start + if f.request.timestamp_end: + duration = f.response.timestamp_end - f.request.timestamp_end else: delta = 0 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: diff --git a/libmproxy/utils.py b/libmproxy/utils.py index 33af035f1..76e99c34f 100644 --- a/libmproxy/utils.py +++ b/libmproxy/utils.py @@ -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): diff --git a/test/test_utils.py b/test/test_utils.py index d99a146df..45bfb4f7f 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -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: From 09828ff2d9f64b326efbfda0d84d5e461bc672cf Mon Sep 17 00:00:00 2001 From: Marcelo Glezer Date: Sun, 1 Mar 2015 22:19:32 -0300 Subject: [PATCH 2/2] changes requested by @mhils --- libmproxy/console/common.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/libmproxy/console/common.py b/libmproxy/console/common.py index 8f074cfdd..fa21c93e3 100644 --- a/libmproxy/console/common.py +++ b/libmproxy/console/common.py @@ -162,7 +162,6 @@ 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"]: @@ -347,21 +346,15 @@ def format_flow(f, focus, extended=False, hostheader=False, padding=2): else: contentdesc = "[no content]" duration = 0 - if f.response.timestamp_end: - delta = f.response.timestamp_end - f.response.timestamp_start - if f.request.timestamp_end: - duration = f.response.timestamp_end - f.request.timestamp_end - else: - delta = 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"]