Example #1
0
 def test_web_ui_no_runner(self):
     env = Environment()
     web_ui = WebUI(env, "127.0.0.1", 0)
     gevent.sleep(0.01)
     try:
         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()
Example #2
0
    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
Example #3
0
    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
Example #4
0
    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()
Example #5
0
    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, runner=runner)
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()
Example #6
0
            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