def setUp(self): super(TestWebUI, self).setUp() parser = get_parser(default_config_files=[]) self.environment.options = parser.parse_args([]) self.runner = LocustRunner(self.environment, []) self.stats = self.runner.stats self.web_ui = WebUI(self.environment, self.runner) self.web_ui.app.view_functions["request_stats"].clear_cache() gevent.spawn(lambda: self.web_ui.start("127.0.0.1", 0)) gevent.sleep(0.01) self.web_port = self.web_ui.server.server_port
def test_web_ui_no_runner(self): env = Environment() web_ui = WebUI(env) try: gevent.spawn(lambda: web_ui.start("127.0.0.1", 0)) gevent.sleep(0.01) response = requests.get("http://127.0.0.1:%i/" % web_ui.server.server_port) self.assertEqual(500, response.status_code) self.assertEqual( "Error: Locust Environment does not have any runner", response.text) finally: web_ui.stop()
class TestWebUIAuth(LocustTestCase): def setUp(self): super(TestWebUIAuth, self).setUp() parser = get_parser(default_config_files=[]) self.environment.options = parser.parse_args( ["--web-auth", "john:doe"]) self.runner = LocustRunner(self.environment, []) self.stats = self.runner.stats self.web_ui = WebUI(self.environment, self.environment.options.web_auth) self.web_ui.app.view_functions["request_stats"].clear_cache() gevent.spawn(lambda: self.web_ui.start("127.0.0.1", 0)) gevent.sleep(0.01) self.web_port = self.web_ui.server.server_port def tearDown(self): super(TestWebUIAuth, self).tearDown() self.web_ui.stop() self.runner.quit() def test_index_with_basic_auth_enabled_correct_credentials(self): self.assertEqual( 200, requests.get("http://127.0.0.1:%i/?ele=phino" % self.web_port, auth=('john', 'doe')).status_code) def test_index_with_basic_auth_enabled_incorrect_credentials(self): self.assertEqual( 401, requests.get("http://127.0.0.1:%i/?ele=phino" % self.web_port, auth=('john', 'invalid')).status_code) def test_index_with_basic_auth_enabled_blank_credentials(self): self.assertEqual( 401, requests.get("http://127.0.0.1:%i/?ele=phino" % self.web_port).status_code)
def create_web_ui(self, host="", port=8089, auth_credentials=None, tls_cert=None, tls_key=None, stats_csv_writer=None): """ Creates a :class:`WebUI <locust.web.WebUI>` instance for this Environment and start running the web server :param host: Host/interface that the web server should accept connections to. Defaults to "" which means all interfaces :param port: Port that the web server should listen to :param auth_credentials: If provided (in format "username:password") basic auth will be enabled :param tls_cert: An optional path (str) to a TLS cert. If this is provided the web UI will be served over HTTPS :param tls_key: An optional path (str) to a TLS private key. If this is provided the web UI will be served over HTTPS :param stats_csv_writer: `StatsCSV <stats_csv.StatsCSV>` instance. """ self.web_ui = WebUI(self, host, port, auth_credentials=auth_credentials, tls_cert=tls_cert, tls_key=tls_key, stats_csv_writer=stats_csv_writer) return self.web_ui
class task_set(TaskSet): @task def my_task(self): self.client.get("/") @task def task_404(self): self.client.get("/non-existing-path") # setup Environment and Runner env = Environment() runner = LocalLocustRunner(environment=env, locust_classes=[User]) # start a WebUI instance web_ui = WebUI(environment=env) gevent.spawn(lambda: web_ui.start("127.0.0.1", 8089)) # TODO: fix #def on_request_success(request_type, name, response_time, response_length, **kwargs): # report_to_grafana("%_%s" % (request_type, name), response_time) #env.events.request_succes.add_listener(on_request_success) # start a greenlet that periodically outputs the current stats gevent.spawn(stats_printer(runner.stats)) # start the test runner.start(1, hatch_rate=10) # wait for the greenlets (indefinitely) runner.greenlet.join()
class TestWebUI(LocustTestCase): def setUp(self): super(TestWebUI, self).setUp() parser = get_parser(default_config_files=[]) self.environment.options = parser.parse_args([]) self.runner = LocustRunner(self.environment, []) self.stats = self.runner.stats self.web_ui = WebUI(self.environment) self.web_ui.app.view_functions["request_stats"].clear_cache() gevent.spawn(lambda: self.web_ui.start("127.0.0.1", 0)) gevent.sleep(0.01) self.web_port = self.web_ui.server.server_port def tearDown(self): super(TestWebUI, self).tearDown() self.web_ui.stop() self.runner.quit() def test_web_ui_reference_on_environment(self): self.assertEqual(self.web_ui, self.environment.web_ui) def test_web_ui_no_runner(self): env = Environment() web_ui = WebUI(env) try: gevent.spawn(lambda: web_ui.start("127.0.0.1", 0)) gevent.sleep(0.01) response = requests.get("http://127.0.0.1:%i/" % web_ui.server.server_port) self.assertEqual(500, response.status_code) self.assertEqual( "Error: Locust Environment does not have any runner", response.text) finally: web_ui.stop() def test_index(self): self.assertEqual( 200, requests.get("http://127.0.0.1:%i/" % self.web_port).status_code) def test_stats_no_data(self): self.assertEqual( 200, requests.get("http://127.0.0.1:%i/stats/requests" % self.web_port).status_code) def test_stats(self): self.stats.log_request("GET", "/<html>", 120, 5612) 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, len(data["stats"])) # one entry plus Aggregated self.assertEqual("/<html>", data["stats"][0]["name"]) self.assertEqual("/<html>", data["stats"][0]["safe_name"]) self.assertEqual("GET", data["stats"][0]["method"]) self.assertEqual(120, data["stats"][0]["avg_response_time"]) self.assertEqual("Aggregated", data["stats"][1]["name"]) self.assertEqual(1, data["stats"][1]["num_requests"]) self.assertEqual(120, data["stats"][1]["avg_response_time"]) def test_stats_cache(self): self.stats.log_request("GET", "/test", 120, 5612) 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, len(data["stats"])) # one entry plus Aggregated # add another entry self.stats.log_request("GET", "/test2", 120, 5612) data = json.loads( requests.get("http://127.0.0.1:%i/stats/requests" % self.web_port).text) self.assertEqual(2, len(data["stats"])) # old value should be cached now self.web_ui.app.view_functions["request_stats"].clear_cache() data = json.loads( requests.get("http://127.0.0.1:%i/stats/requests" % self.web_port).text) self.assertEqual(3, len(data["stats"])) # this should no longer be cached def test_stats_rounding(self): self.stats.log_request("GET", "/test", 1.39764125, 2) self.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(1, data["stats"][0]["min_response_time"]) self.assertEqual(1000, data["stats"][0]["max_response_time"]) def test_request_stats_csv(self): self.stats.log_request("GET", "/test2", 120, 5612) response = requests.get("http://127.0.0.1:%i/stats/requests/csv" % self.web_port) self.assertEqual(200, response.status_code) def test_failure_stats_csv(self): self.stats.log_error("GET", "/", Exception("Error1337")) response = requests.get("http://127.0.0.1:%i/stats/failures/csv" % self.web_port) self.assertEqual(200, response.status_code) def test_request_stats_with_errors(self): self.stats.log_error("GET", "/", 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", response.text) def test_reset_stats(self): try: raise Exception(u"A cool test exception") except Exception as e: tb = sys.exc_info()[2] self.runner.log_exception("local", str(e), "".join(traceback.format_tb(tb))) self.runner.log_exception("local", str(e), "".join(traceback.format_tb(tb))) self.stats.log_request("GET", "/test", 120, 5612) self.stats.log_error("GET", "/", Exception("Error1337")) response = requests.get("http://127.0.0.1:%i/stats/reset" % self.web_port) self.assertEqual(200, response.status_code) self.assertEqual({}, self.stats.errors) self.assertEqual({}, self.runner.exceptions) self.assertEqual(0, self.stats.get("/", "GET").num_requests) self.assertEqual(0, self.stats.get("/", "GET").num_failures) self.assertEqual(0, self.stats.get("/test", "GET").num_requests) self.assertEqual(0, self.stats.get("/test", "GET").num_failures) def test_exceptions(self): try: raise Exception(u"A cool test exception") except Exception as e: tb = sys.exc_info()[2] self.runner.log_exception("local", str(e), "".join(traceback.format_tb(tb))) self.runner.log_exception("local", str(e), "".join(traceback.format_tb(tb))) response = requests.get("http://127.0.0.1:%i/exceptions" % self.web_port) self.assertEqual(200, response.status_code) self.assertIn("A cool test exception", response.text) response = requests.get("http://127.0.0.1:%i/stats/requests" % self.web_port) self.assertEqual(200, response.status_code) def test_exceptions_csv(self): try: raise Exception("Test exception") except Exception as e: tb = sys.exc_info()[2] self.runner.log_exception("local", str(e), "".join(traceback.format_tb(tb))) self.runner.log_exception("local", str(e), "".join(traceback.format_tb(tb))) response = requests.get("http://127.0.0.1:%i/exceptions/csv" % self.web_port) self.assertEqual(200, response.status_code) reader = csv.reader(StringIO(response.text)) rows = [] for row in reader: rows.append(row) self.assertEqual(2, len(rows)) self.assertEqual("Test exception", rows[1][1]) self.assertEqual(2, int(rows[1][0]), "Exception count should be 2") def test_swarm_host_value_specified(self): class MyLocust(Locust): wait_time = constant(1) @task(1) def my_task(self): pass self.environment.locust_classes = [MyLocust] response = requests.post( "http://127.0.0.1:%i/swarm" % self.web_port, data={ "locust_count": 5, "hatch_rate": 5, "host": "https://localhost" }, ) self.assertEqual(200, response.status_code) self.assertEqual("https://localhost", response.json()["host"]) self.assertEqual(self.environment.host, "https://localhost") def test_swarm_host_value_not_specified(self): class MyLocust(Locust): wait_time = constant(1) @task(1) def my_task(self): pass self.runner.locust_classes = [MyLocust] response = requests.post( "http://127.0.0.1:%i/swarm" % self.web_port, data={ 'locust_count': 5, 'hatch_rate': 5 }, ) self.assertEqual(200, response.status_code) self.assertEqual(None, response.json()["host"]) self.assertEqual(self.environment.host, None) def test_host_value_from_locust_class(self): class MyLocust(Locust): host = "http://example.com" self.environment.runner.locust_classes = [MyLocust] response = requests.get("http://127.0.0.1:%i/" % self.web_port) self.assertEqual(200, response.status_code) self.assertIn("http://example.com", response.content.decode("utf-8")) self.assertNotIn( "setting this will override the host on all Locust classes", response.content.decode("utf-8")) def test_host_value_from_multiple_locust_classes(self): class MyLocust(Locust): host = "http://example.com" class MyLocust2(Locust): host = "http://example.com" self.environment.runner.locust_classes = [MyLocust, MyLocust2] response = requests.get("http://127.0.0.1:%i/" % self.web_port) self.assertEqual(200, response.status_code) self.assertIn("http://example.com", response.content.decode("utf-8")) self.assertNotIn( "setting this will override the host on all Locust classes", response.content.decode("utf-8")) def test_host_value_from_multiple_locust_classes_different_hosts(self): class MyLocust(Locust): host = None class MyLocust2(Locust): host = "http://example.com" self.environment.runner.locust_classes = [MyLocust, MyLocust2] response = requests.get("http://127.0.0.1:%i/" % self.web_port) self.assertEqual(200, response.status_code) self.assertNotIn("http://example.com", response.content.decode("utf-8")) self.assertIn( "setting this will override the host on all Locust classes", response.content.decode("utf-8")) def test_swarm_in_step_load_mode(self): class MyLocust(Locust): wait_time = constant(1) @task(1) def my_task(self): pass self.environment.locust_classes = [MyLocust] self.environment.step_load = True response = requests.post("http://127.0.0.1:%i/swarm" % self.web_port, data={ "locust_count": 5, "hatch_rate": 2, "step_locust_count": 2, "step_duration": "2m" }) self.assertEqual(200, response.status_code) self.assertIn("Step Load Mode", response.text)
events.request_success.fire(request_type="ws", name="请求", response_time=recv_time, response_length=0) class MyUser(User): wait_time = between(0, 0) tasks = [MyTaskSet] def on_start(self): url = 'ws://192.168.1.8:8765' start_time = time.time() self.ws = websocket.create_connection(url, timeout=30) create_connection = int((time.time() - start_time) * 1000) events.request_success.fire(request_type="ws", name="创建连接", response_time=create_connection, response_length=0) def on_stop(self): self.ws.close(reason='used') web_ui = WebUI(env, host='localhost', port=8089) @web_ui.app.route("/my_custom_route") def my_custom_route(): return "your IP is: %s" % request.remote_addr