Skip to content

Commit

Permalink
Used existing method for claculating error
Browse files Browse the repository at this point in the history
  • Loading branch information
raiyankamal committed Sep 6, 2019
1 parent 22f0556 commit 97dc3de
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
7 changes: 2 additions & 5 deletions locust/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
60 changes: 56 additions & 4 deletions locust/test/test_stats.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 97dc3de

Please sign in to comment.