From 7ee7389dc6c96763b90364acca80d20a04ee30e3 Mon Sep 17 00:00:00 2001 From: Jonatan Heyman Date: Mon, 5 Sep 2011 22:40:11 +0200 Subject: [PATCH] Improved master performance by not summing response_time and num_reqs_per_sec dicts on stats HTTP request in web interface. Unfortunately this removes the Total median value in the web interface, but it's still present in the request stats CSV (issue #7 created). --- locust/stats.py | 29 +++++++++++++++++++---------- locust/test/test_stats.py | 2 +- locust/web.py | 4 ++-- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/locust/stats.py b/locust/stats.py index 140cd5aebd..b435758831 100644 --- a/locust/stats.py +++ b/locust/stats.py @@ -38,6 +38,7 @@ def clear_all(cls): @classmethod def reset_all(cls): + cls.global_start_time = time.time() cls.total_num_requests = 0 for name, stats in cls.requests.iteritems(): stats.reset() @@ -149,11 +150,12 @@ def avg_content_length(self): return self.total_content_length / self.num_reqs except ZeroDivisionError: return 0 - + + # TODO: for better performance use __iadd__ instead def __add__(self, other): - #if self.name != other.name: - # raise RequestStatsAdditionError("Trying to add two RequestStats objects of different names (%s and %s)" % (self.name, other.name)) - + return self.add_stats(other) + + def add_stats(self, other, full_request_history=False): new = RequestStats(self.name) new.last_request_timestamp = max(self.last_request_timestamp, other.last_request_timestamp) new.start_time = min(self.start_time, other.start_time) @@ -168,12 +170,19 @@ def __add__(self, other): def merge_dict_add(d1, d2): """Merge two dicts by adding the values from each dict""" merged = copy(d1) - for key in set(d2.keys()): + for key in d2.keys(): merged[key] = d1.get(key, 0) + d2.get(key, 0) return merged - new.num_reqs_per_sec = merge_dict_add(self.num_reqs_per_sec, other.num_reqs_per_sec) - new.response_times = merge_dict_add(self.response_times, other.response_times) + if full_request_history: + new.response_times = merge_dict_add(self.response_times, other.response_times) + new.num_reqs_per_sec = merge_dict_add(self.num_reqs_per_sec, other.num_reqs_per_sec) + else: + new.response_times = {} + new.num_reqs_per_sec = {} + for i in xrange(new.last_request_timestamp - 20, new.last_request_timestamp +1): + new.num_reqs_per_sec[i] = self.num_reqs_per_sec.get(i, 0) + other.num_reqs_per_sec.get(i, 0) + return new def get_stripped_report(self): @@ -241,10 +250,10 @@ def get(cls, name): return request @classmethod - def sum_stats(cls, name="Total"): + def sum_stats(cls, name="Total", full_request_history=False): stats = RequestStats(name) for s in cls.requests.itervalues(): - stats += s + stats = stats.add_stats(s, full_request_history) return stats def avg(values): @@ -291,7 +300,7 @@ def on_slave_report(client_id, data): for stats in data["stats"]: if not stats.name in RequestStats.requests: RequestStats.requests[stats.name] = RequestStats(stats.name) - RequestStats.requests[stats.name] += stats + RequestStats.requests[stats.name] = RequestStats.requests[stats.name].add_stats(stats, full_request_history=True) RequestStats.global_last_request_timestamp = max(RequestStats.global_last_request_timestamp, stats.last_request_timestamp) for err_message, err_count in data["errors"].iteritems(): diff --git a/locust/test/test_stats.py b/locust/test/test_stats.py index 2b629d5c7d..1d2e6df75c 100644 --- a/locust/test/test_stats.py +++ b/locust/test/test_stats.py @@ -70,7 +70,7 @@ def test_aggregation(self): s2.log(55, 0) s2.log(97, 0) - s = s1 + s2 + s = s1.add_stats(s2, full_request_history=True) self.assertEqual(s.num_reqs, 10) self.assertEqual(s.num_failures, 3) diff --git a/locust/web.py b/locust/web.py index 644b1722e9..236a91421f 100644 --- a/locust/web.py +++ b/locust/web.py @@ -77,7 +77,7 @@ def request_stats_csv(): ]) ] - for s in _sort_stats(locust_runner.request_stats) + [RequestStats.sum_stats("Total")]: + for s in _sort_stats(locust_runner.request_stats) + [RequestStats.sum_stats("Total", full_request_history=True)]: rows.append('"%s",%i,%i,%i,%i,%i,%i,%i,%.2f' % ( s.name, s.num_reqs, @@ -111,7 +111,7 @@ def distribution_stats_csv(): '"99%"', '"100%"', ))] - for s in _sort_stats(locust_runner.request_stats) + [RequestStats.sum_stats("Total")]: + for s in _sort_stats(locust_runner.request_stats) + [RequestStats.sum_stats("Total", full_request_history=True)]: rows.append(s.percentile(tpl='"%s",%i,%i,%i,%i,%i,%i,%i,%i,%i,%i')) response = make_response("\n".join(rows))