From 22f0556ab110ee5529240d1eedfcd97537a4e55b Mon Sep 17 00:00:00 2001 From: Zach Martin Date: Tue, 27 Aug 2019 13:17:43 -0700 Subject: [PATCH 1/3] Fix fail percentage The fail percentage was calculated incorrectly. Something that failed all of the time would be reported as failing 50% total. --- locust/stats.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/locust/stats.py b/locust/stats.py index 2041e4f62d..736ac4217e 100644 --- a/locust/stats.py +++ b/locust/stats.py @@ -385,7 +385,7 @@ def get_stripped_report(self): def __str__(self): try: - fail_percent = (self.num_failures/float(self.num_requests + self.num_failures))*100 + fail_percent = float(self.num_failures/self.num_requests)*100 except ZeroDivisionError: fail_percent = 0 From 97dc3dec1feba68bd749ece603898239a204fd51 Mon Sep 17 00:00:00 2001 From: Raiyan Date: Thu, 5 Sep 2019 20:59:57 -0400 Subject: [PATCH 2/3] Used existing method for claculating error --- locust/stats.py | 7 ++--- locust/test/test_stats.py | 60 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/locust/stats.py b/locust/stats.py index 736ac4217e..bc0da37704 100644 --- a/locust/stats.py +++ b/locust/stats.py @@ -384,11 +384,8 @@ def get_stripped_report(self): return report def __str__(self): - try: - fail_percent = float(self.num_failures/self.num_requests)*100 - except ZeroDivisionError: - fail_percent = 0 - + fail_percent = self.fail_ratio*100 + return (" %-" + str(STATS_NAME_WIDTH) + "s %7d %12s %7d %7d %7d | %7d %7.2f") % ( (self.method and self.method + " " or "") + self.name, self.num_requests, diff --git a/locust/test/test_stats.py b/locust/test/test_stats.py index e37774e2ac..f6c52bfd59 100644 --- a/locust/test/test_stats.py +++ b/locust/test/test_stats.py @@ -1,5 +1,6 @@ import time import unittest +import re from locust.core import HttpLocust, TaskSet, task from locust.inspectlocust import get_task_ratio_dict @@ -253,15 +254,66 @@ def test_diff_response_times_dicts(self): class TestStatsEntry(unittest.TestCase): + + def parse_string_output(self, text): + tokenlist = re.split('[\s\(\)%|]+', text.strip()) + tokens = { + 'method': tokenlist[0], + 'name': tokenlist[1], + 'request_count': int(tokenlist[2]), + 'failure_count': int(tokenlist[3]), + 'failure_precentage': float(tokenlist[4]), + } + return tokens + def setUp(self, *args, **kwargs): super(TestStatsEntry, self).setUp(*args, **kwargs) self.stats = RequestStats() - def test_fail_ratio_with_failures(self): + def test_fail_ratio_with_no_failures(self): + REQUEST_COUNT = 10 + FAILURE_COUNT = 0 + EXPECTED_FAIL_RATIO = 0.0 + + s = StatsEntry(self.stats, "/", "GET") + s.num_requests = REQUEST_COUNT + s.num_failures = FAILURE_COUNT + + self.assertAlmostEqual(s.fail_ratio, EXPECTED_FAIL_RATIO) + output_fields = self.parse_string_output(str(s)) + self.assertEqual(output_fields['request_count'], REQUEST_COUNT) + self.assertEqual(output_fields['failure_count'], FAILURE_COUNT) + self.assertAlmostEqual(output_fields['failure_precentage'], EXPECTED_FAIL_RATIO*100) + + def test_fail_ratio_with_all_failures(self): + REQUEST_COUNT = 10 + FAILURE_COUNT = 10 + EXPECTED_FAIL_RATIO = 1.0 + + s = StatsEntry(self.stats, "/", "GET") + s.num_requests = REQUEST_COUNT + s.num_failures = FAILURE_COUNT + + self.assertAlmostEqual(s.fail_ratio, EXPECTED_FAIL_RATIO) + output_fields = self.parse_string_output(str(s)) + self.assertEqual(output_fields['request_count'], REQUEST_COUNT) + self.assertEqual(output_fields['failure_count'], FAILURE_COUNT) + self.assertAlmostEqual(output_fields['failure_precentage'], EXPECTED_FAIL_RATIO*100) + + def test_fail_ratio_with_half_failures(self): + REQUEST_COUNT = 10 + FAILURE_COUNT = 5 + EXPECTED_FAIL_RATIO = 0.5 + s = StatsEntry(self.stats, "/", "GET") - s.num_requests = 10 - s.num_failures = 5 - self.assertAlmostEqual(s.fail_ratio, 0.5) + s.num_requests = REQUEST_COUNT + s.num_failures = FAILURE_COUNT + + self.assertAlmostEqual(s.fail_ratio, EXPECTED_FAIL_RATIO) + output_fields = self.parse_string_output(str(s)) + self.assertEqual(output_fields['request_count'], REQUEST_COUNT) + self.assertEqual(output_fields['failure_count'], FAILURE_COUNT) + self.assertAlmostEqual(output_fields['failure_precentage'], EXPECTED_FAIL_RATIO*100) class TestRequestStatsWithWebserver(WebserverTestCase): From fc65bdd2cf3a174ce007a4585bbcaca4ea2451ac Mon Sep 17 00:00:00 2001 From: Corey Goldberg Date: Sat, 7 Sep 2019 09:46:03 -0400 Subject: [PATCH 3/3] pep8 fix added spacing --- locust/stats.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/locust/stats.py b/locust/stats.py index bc0da37704..c2b679af77 100644 --- a/locust/stats.py +++ b/locust/stats.py @@ -384,7 +384,7 @@ def get_stripped_report(self): return report def __str__(self): - fail_percent = self.fail_ratio*100 + fail_percent = self.fail_ratio * 100 return (" %-" + str(STATS_NAME_WIDTH) + "s %7d %12s %7d %7d %7d | %7d %7.2f") % ( (self.method and self.method + " " or "") + self.name,