Skip to content

Commit

Permalink
fix code and fix unit tests (locustio#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
pancaprima authored and hariesef committed Jun 28, 2018
1 parent 96b1762 commit 9bb7ef7
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 46 deletions.
4 changes: 1 addition & 3 deletions locust/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
import socket
import sys
import time
import tests_loader
import fileio
from optparse import OptionParser

import gevent

import locust

from . import events, runners, web
from . import events, runners, web, tests_loader, fileio
from .core import HttpLocust, Locust
from .inspectlocust import get_task_ratio_dict, print_task_ratio
from .log import console_logger, setup_logging
Expand Down
4 changes: 1 addition & 3 deletions locust/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@

from six.moves import xrange

from . import events, configuration
from . import events, configuration, fileio, tests_loader
from .rpc import Message, rpc
from .stats import global_stats
import fileio
import tests_loader

logger = logging.getLogger(__name__)

Expand Down
16 changes: 8 additions & 8 deletions locust/test/test_main.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from locust import main
from locust import main, tests_loader
from locust.core import HttpLocust, Locust, TaskSet

from .testcases import LocustTestCase


class TestTaskSet(LocustTestCase):
def test_is_locust(self):
self.assertFalse(main.is_locust(("Locust", Locust)))
self.assertFalse(main.is_locust(("HttpLocust", HttpLocust)))
self.assertFalse(main.is_locust(("random_dict", {})))
self.assertFalse(main.is_locust(("random_list", [])))
self.assertFalse(tests_loader.is_locust(("Locust", Locust)))
self.assertFalse(tests_loader.is_locust(("HttpLocust", HttpLocust)))
self.assertFalse(tests_loader.is_locust(("random_dict", {})))
self.assertFalse(tests_loader.is_locust(("random_list", [])))

class MyTaskSet(TaskSet):
pass
Expand All @@ -20,10 +20,10 @@ class MyHttpLocust(HttpLocust):
class MyLocust(Locust):
task_set = MyTaskSet

self.assertTrue(main.is_locust(("MyHttpLocust", MyHttpLocust)))
self.assertTrue(main.is_locust(("MyLocust", MyLocust)))
self.assertTrue(tests_loader.is_locust(("MyHttpLocust", MyHttpLocust)))
self.assertTrue(tests_loader.is_locust(("MyLocust", MyLocust)))

class ThriftLocust(Locust):
pass

self.assertFalse(main.is_locust(("ThriftLocust", ThriftLocust)))
self.assertFalse(tests_loader.is_locust(("ThriftLocust", ThriftLocust)))
56 changes: 28 additions & 28 deletions locust/test/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ def setUp(self):
self.s.log(45, 0)
self.s.log(135, 0)
self.s.log(44, 0)
self.s.log_error(Exception("dummy fail"))
self.s.log_error(Exception("dummy fail"))
self.s.log_error(50,Exception("dummy fail"))
self.s.log_error(100,Exception("dummy fail"))
self.s.log(375, 0)
self.s.log(601, 0)
self.s.log(35, 0)
self.s.log(79, 0)
self.s.log_error(Exception("dummy fail"))
self.s.log_error(520,Exception("dummy fail"))

def test_percentile(self):
s = StatsEntry(self.stats, "percentile_test", "GET")
Expand All @@ -39,33 +39,33 @@ def test_median(self):
self.assertEqual(self.s.median_response_time, 79)

def test_total_rps(self):
self.assertEqual(self.s.total_rps, 7)
self.assertEqual(self.s.total_rps, 10)

def test_current_rps(self):
self.stats.last_request_timestamp = int(time.time()) + 4
self.assertEqual(self.s.current_rps, 3.5)
self.assertEqual(self.s.current_rps, 5)

self.stats.last_request_timestamp = int(time.time()) + 25
self.assertEqual(self.s.current_rps, 0)

def test_num_reqs_fails(self):
self.assertEqual(self.s.num_requests, 7)
self.assertEqual(self.s.num_requests, 10)
self.assertEqual(self.s.num_failures, 3)

def test_avg(self):
self.assertEqual(self.s.avg_response_time, 187.71428571428571428571428571429)
self.assertEqual(self.s.avg_response_time, 198.4)

def test_reset(self):
self.s.reset()
self.s.log(756, 0)
self.s.log_error(Exception("dummy fail after reset"))
self.s.log_error(1000,Exception("dummy fail after reset"))
self.s.log(85, 0)

self.assertEqual(self.s.total_rps, 2)
self.assertEqual(self.s.num_requests, 2)
self.assertEqual(self.s.total_rps, 3)
self.assertEqual(self.s.num_requests, 3)
self.assertEqual(self.s.num_failures, 1)
self.assertEqual(self.s.avg_response_time, 420.5)
self.assertEqual(self.s.median_response_time, 85)
self.assertEqual(self.s.avg_response_time, 613.6666666666666)
self.assertEqual(self.s.median_response_time, 760)

def test_reset_min_response_time(self):
self.s.reset()
Expand All @@ -77,11 +77,11 @@ def test_aggregation(self):
s1.log(12, 0)
s1.log(12, 0)
s1.log(38, 0)
s1.log_error("Dummy exzeption")
s1.log_error(100,"Dummy exzeption")

s2 = StatsEntry(self.stats, "aggregate me!", "GET")
s2.log_error("Dummy exzeption")
s2.log_error("Dummy exzeption")
s2.log_error(100,"Dummy exzeption")
s2.log_error(60,"Dummy exzeption")
s2.log(12, 0)
s2.log(99, 0)
s2.log(14, 0)
Expand All @@ -94,25 +94,25 @@ def test_aggregation(self):
s.extend(s1, full_request_history=True)
s.extend(s2, full_request_history=True)

self.assertEqual(s.num_requests, 10)
self.assertEqual(s.num_requests, 13)
self.assertEqual(s.num_failures, 3)
self.assertEqual(s.median_response_time, 38)
self.assertEqual(s.avg_response_time, 43.2)
self.assertEqual(s.median_response_time, 55)
self.assertEqual(s.avg_response_time, 53.23076923076923)

def test_error_grouping(self):
# reset stats
self.stats = RequestStats()

s = StatsEntry(self.stats, "/some-path", "GET")
s.log_error(Exception("Exception!"))
s.log_error(Exception("Exception!"))
s.log_error(100,Exception("Exception!"))
s.log_error(100,Exception("Exception!"))

self.assertEqual(1, len(self.stats.errors))
self.assertEqual(2, list(self.stats.errors.values())[0].occurences)

s.log_error(Exception("Another exception!"))
s.log_error(Exception("Another exception!"))
s.log_error(Exception("Third exception!"))
s.log_error(100,Exception("Another exception!"))
s.log_error(100,Exception("Another exception!"))
s.log_error(100,Exception("Third exception!"))
self.assertEqual(3, len(self.stats.errors))

def test_error_grouping_errors_with_memory_addresses(self):
Expand All @@ -122,8 +122,8 @@ class Dummy(object):
pass

s = StatsEntry(self.stats, "/", "GET")
s.log_error(Exception("Error caused by %r" % Dummy()))
s.log_error(Exception("Error caused by %r" % Dummy()))
s.log_error(100,Exception("Error caused by %r" % Dummy()))
s.log_error(100,Exception("Error caused by %r" % Dummy()))

self.assertEqual(1, len(self.stats.errors))

Expand Down Expand Up @@ -204,7 +204,7 @@ class MyLocust(HttpLocust):
response = locust.client.get("/", timeout=0.1)
self.assertEqual(response.status_code, 0)
self.assertEqual(1, global_stats.get("/", "GET").num_failures)
self.assertEqual(0, global_stats.get("/", "GET").num_requests)
self.assertEqual(1, global_stats.get("/", "GET").num_requests)

def test_max_requests(self):
class MyTaskSet(TaskSet):
Expand Down Expand Up @@ -257,15 +257,15 @@ class MyLocust(HttpLocust):

l = MyLocust()
self.assertRaises(StopLocust, lambda: l.task_set(l).run())
self.assertEqual(1, global_stats.num_requests)
self.assertEqual(3, global_stats.num_requests)
self.assertEqual(2, global_stats.num_failures)

global_stats.clear_all()
global_stats.max_requests = 2
self.assertEqual(0, global_stats.num_requests)
self.assertEqual(0, global_stats.num_failures)
l.run()
self.assertEqual(1, global_stats.num_requests)
self.assertEqual(2, global_stats.num_requests)
self.assertEqual(1, global_stats.num_failures)
finally:
global_stats.clear_all()
Expand Down
2 changes: 1 addition & 1 deletion locust/test/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_distribution_stats_csv(self):
self.assertEqual(200, response.status_code)

def test_request_stats_with_errors(self):
stats.global_stats.get("/", "GET").log_error(Exception("Error1337"))
stats.global_stats.get("/", "GET").log_error(100,Exception("Error1337"))
response = requests.get("http://127.0.0.1:%i/stats/requests" % self.web_port)
self.assertEqual(200, response.status_code)
self.assertIn("Error1337", str(response.content))
Expand Down
5 changes: 2 additions & 3 deletions locust/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
from locust import __version__ as version
from six.moves import StringIO, xrange
from werkzeug.utils import secure_filename
import tests_loader
import requests

from . import runners, configuration
from . import runners, configuration, tests_loader
from .cache import memoize
from .runners import MasterLocustRunner
from .stats import distribution_csv, median_from_dict, requests_csv, sort_stats
Expand Down Expand Up @@ -199,7 +198,7 @@ def request_stats():
report["state"] = runners.locust_runner.state
report["user_count"] = runners.locust_runner.user_count
report["running_type"] = runners.locust_runner.running_type
report["host"] = runners.locust_runner.locust_classes[0].host
report["host"] = runners.locust_runner.locust_classes[0].host if len(runners.locust_runner.locust_classes) > 0 else None
return json.dumps(report)

@app.route("/exceptions")
Expand Down

0 comments on commit 9bb7ef7

Please sign in to comment.