Skip to content

Commit

Permalink
Use custom rounding function
Browse files Browse the repository at this point in the history
  • Loading branch information
ajt89 authored and cgoldberg committed Sep 24, 2019
1 parent 3b2fd9f commit 14bd777
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
16 changes: 16 additions & 0 deletions locust/test/test_util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
from locust.util.time import parse_timespan
from locust.util.rounding import proper_round


class TestParseTimespan(unittest.TestCase):
Expand All @@ -14,3 +15,18 @@ def test_parse_timespan(self):
self.assertEqual(60, parse_timespan("1m"))
self.assertEqual(7200, parse_timespan("2h"))
self.assertEqual(3787, parse_timespan("1h3m7s"))


class TestRounding(unittest.TestCase):
def test_rounding_down(self):
self.assertEqual(1, proper_round(1.499999999))
self.assertEqual(5, proper_round(5.499999999))
self.assertEqual(2, proper_round(2.05))
self.assertEqual(3, proper_round(3.05))

def test_rounding_up(self):
self.assertEqual(2, proper_round(1.5))
self.assertEqual(3, proper_round(2.5))
self.assertEqual(4, proper_round(3.5))
self.assertEqual(5, proper_round(4.5))
self.assertEqual(6, proper_round(5.5))
6 changes: 3 additions & 3 deletions locust/test/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ def test_stats_cache(self):
self.assertEqual(3, len(data["stats"])) # this should no longer be cached

def test_stats_rounding(self):
stats.global_stats.log_request("GET", "/test", 1.5, 15)
stats.global_stats.log_request("GET", "/test", 999.99, 99999)
stats.global_stats.log_request("GET", "/test", 1.39764125, 2)
stats.global_stats.log_request("GET", "/test", 999.9764125, 1000)
response = requests.get("http://127.0.0.1:%i/stats/requests" % self.web_port)
self.assertEqual(200, response.status_code)

data = json.loads(response.text)
self.assertEqual(2, data["stats"][0]["min_response_time"])
self.assertEqual(1, data["stats"][0]["min_response_time"])
self.assertEqual(1000, data["stats"][0]["max_response_time"])

def test_request_stats_csv(self):
Expand Down
2 changes: 2 additions & 0 deletions locust/util/rounding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def proper_round(val, digits=0):
return round(val + 10 ** (-len(str(val)) - 1), digits)
6 changes: 3 additions & 3 deletions locust/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from .runners import MasterLocustRunner
from .stats import distribution_csv, failures_csv, median_from_dict, requests_csv, sort_stats
from .util.cache import memoize
from .util.rounding import proper_round

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -105,15 +106,14 @@ def request_stats():
stats = []

for s in chain(sort_stats(runners.locust_runner.request_stats), [runners.locust_runner.stats.total]):
0 if s.min_response_time is None else round(s.min_response_time)
stats.append({
"method": s.method,
"name": s.name,
"num_requests": s.num_requests,
"num_failures": s.num_failures,
"avg_response_time": s.avg_response_time,
"min_response_time": 0 if s.min_response_time is None else round(s.min_response_time),
"max_response_time": round(s.max_response_time),
"min_response_time": 0 if s.min_response_time is None else proper_round(s.min_response_time),
"max_response_time": proper_round(s.max_response_time),
"current_rps": s.current_rps,
"median_response_time": s.median_response_time,
"avg_content_length": s.avg_content_length,
Expand Down

0 comments on commit 14bd777

Please sign in to comment.