Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow None response time for requests #1088

Merged
merged 4 commits into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions locust/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def calculate_response_time_percentile(response_times, num_requests, percent):
processed_count += response_times[response_time]
if(num_requests - processed_count <= num_of_request):
return response_time
# if all response times were None
return 0


def diff_response_time_dicts(latest, old):
Expand Down Expand Up @@ -81,6 +83,10 @@ def __init__(self):
def num_requests(self):
return self.total.num_requests

@property
def num_none_requests(self):
return self.total.num_none_requests

@property
def num_failures(self):
return self.total.num_failures
Expand Down Expand Up @@ -155,6 +161,9 @@ class StatsEntry(object):
num_requests = None
""" The number of requests made """

num_none_requests = None
""" The number of requests made with a None response time (typically async requests) """

num_failures = None
""" Number of failed request """

Expand Down Expand Up @@ -214,6 +223,7 @@ def __init__(self, stats, name, method, use_response_times_cache=False):
def reset(self):
self.start_time = time.time()
self.num_requests = 0
self.num_none_requests = 0
self.num_failures = 0
self.total_response_time = 0
self.response_times = {}
Expand Down Expand Up @@ -246,6 +256,9 @@ def _log_time_of_request(self, t):
self.last_request_timestamp = t

def _log_response_time(self, response_time):
if response_time is None:
self.num_none_requests += 1
return

self.total_response_time += response_time

Expand Down Expand Up @@ -287,15 +300,15 @@ def fail_ratio(self):
@property
def avg_response_time(self):
try:
return float(self.total_response_time) / self.num_requests
return float(self.total_response_time) / (self.num_requests - self.num_none_requests)
except ZeroDivisionError:
return 0

@property
def median_response_time(self):
if not self.response_times:
return 0
median = median_from_dict(self.num_requests, self.response_times)
median = median_from_dict(self.num_requests - self.num_none_requests, self.response_times) or 0

# Since we only use two digits of precision when calculating the median response time
# while still using the exact values for min and max response times, the following checks
Expand Down Expand Up @@ -340,6 +353,7 @@ def extend(self, other):
self.start_time = min(self.start_time, other.start_time)

self.num_requests = self.num_requests + other.num_requests
self.num_none_requests = self.num_none_requests + other.num_none_requests
self.num_failures = self.num_failures + other.num_failures
self.total_response_time = self.total_response_time + other.total_response_time
self.max_response_time = max(self.max_response_time, other.max_response_time)
Expand All @@ -362,6 +376,7 @@ def serialize(self):
"last_request_timestamp": self.last_request_timestamp,
"start_time": self.start_time,
"num_requests": self.num_requests,
"num_none_requests": self.num_none_requests,
"num_failures": self.num_failures,
"total_response_time": self.total_response_time,
"max_response_time": self.max_response_time,
Expand All @@ -378,6 +393,7 @@ def unserialize(cls, data):
"last_request_timestamp",
"start_time",
"num_requests",
"num_none_requests",
"num_failures",
"total_response_time",
"max_response_time",
Expand Down
67 changes: 66 additions & 1 deletion locust/test/test_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,35 @@ class MyTestLocust(Locust):
s = master.stats.get("/", "GET")
self.assertEqual(700, s.median_response_time)

def test_slave_stats_report_with_none_response_times(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice with tests that tests the whole cycle with master/slave communication!

It'd be nice to also have much simpler test in test_stats.py that just tests the None response time functionality for StatsEntry.

class MyTestLocust(Locust):
pass

with mock.patch("locust.rpc.rpc.Server", mocked_rpc_server()) as server:
master = MasterLocustRunner(MyTestLocust, self.options)
server.mocked_send(Message("client_ready", None, "fake_client"))

master.stats.get("/mixed", "GET").log(0, 23455)
master.stats.get("/mixed", "GET").log(800, 23455)
master.stats.get("/mixed", "GET").log(700, 23455)
master.stats.get("/mixed", "GET").log(None, 23455)
master.stats.get("/mixed", "GET").log(None, 23455)
master.stats.get("/mixed", "GET").log(None, 23455)
master.stats.get("/mixed", "GET").log(None, 23455)
master.stats.get("/onlyNone", "GET").log(None, 23455)

data = {"user_count":1}
events.report_to_master.fire(client_id="fake_client", data=data)
master.stats.clear_all()

server.mocked_send(Message("stats", data, "fake_client"))
s1 = master.stats.get("/mixed", "GET")
self.assertEqual(700, s1.median_response_time)
self.assertEqual(500, s1.avg_response_time)
s2 = master.stats.get("/onlyNone", "GET")
self.assertEqual(0, s2.median_response_time)
self.assertEqual(0, s2.avg_response_time)

def test_master_marks_downed_slaves_as_missing(self):
class MyTestLocust(Locust):
pass
Expand Down Expand Up @@ -195,7 +224,43 @@ class MyTestLocust(Locust):
"user_count": 2,
}, "fake_client"))
self.assertEqual(700, master.stats.total.median_response_time)


def test_master_total_stats_with_none_response_times(self):
class MyTestLocust(Locust):
pass

with mock.patch("locust.rpc.rpc.Server", mocked_rpc_server()) as server:
master = MasterLocustRunner(MyTestLocust, self.options)
server.mocked_send(Message("client_ready", None, "fake_client"))
stats = RequestStats()
stats.log_request("GET", "/1", 100, 3546)
stats.log_request("GET", "/1", 800, 56743)
stats.log_request("GET", "/1", None, 56743)
stats2 = RequestStats()
stats2.log_request("GET", "/2", 700, 2201)
stats2.log_request("GET", "/2", None, 2201)
stats3 = RequestStats()
stats3.log_request("GET", "/3", None, 2201)
server.mocked_send(Message("stats", {
"stats":stats.serialize_stats(),
"stats_total": stats.total.serialize(),
"errors":stats.serialize_errors(),
"user_count": 1,
}, "fake_client"))
server.mocked_send(Message("stats", {
"stats":stats2.serialize_stats(),
"stats_total": stats2.total.serialize(),
"errors":stats2.serialize_errors(),
"user_count": 2,
}, "fake_client"))
server.mocked_send(Message("stats", {
"stats":stats3.serialize_stats(),
"stats_total": stats3.total.serialize(),
"errors":stats3.serialize_errors(),
"user_count": 2,
}, "fake_client"))
self.assertEqual(700, master.stats.total.median_response_time)

def test_master_current_response_times(self):
class MyTestLocust(Locust):
pass
Expand Down