Skip to content

Commit

Permalink
Improved master performance by not summing response_time and num_reqs…
Browse files Browse the repository at this point in the history
…_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).
  • Loading branch information
heyman committed Sep 5, 2011
1 parent ed074f6 commit 7ee7389
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
29 changes: 19 additions & 10 deletions locust/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion locust/test/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions locust/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 7ee7389

Please sign in to comment.