-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1327 from locustio/environment-refactor
Add Runners, WebUI and Environment to the public API
- Loading branch information
Showing
14 changed files
with
382 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,42 @@ | ||
import gevent | ||
from locust import HttpLocust, TaskSet, task, between | ||
from locust.runners import LocalLocustRunner | ||
from locust import HttpLocust, task, between | ||
from locust.env import Environment | ||
from locust.stats import stats_printer | ||
from locust.log import setup_logging | ||
from locust.web import WebUI | ||
|
||
setup_logging("INFO", None) | ||
|
||
|
||
class User(HttpLocust): | ||
wait_time = between(1, 3) | ||
host = "https://docs.locust.io" | ||
|
||
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)) | ||
@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(locust_classes=[User]) | ||
env.create_local_runner() | ||
|
||
# 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 WebUI instance | ||
env.create_web_ui("127.0.0.1", 8089) | ||
|
||
# start a greenlet that periodically outputs the current stats | ||
gevent.spawn(stats_printer(runner.stats)) | ||
gevent.spawn(stats_printer(env.stats)) | ||
|
||
# start the test | ||
runner.start(1, hatch_rate=10) | ||
# wait for the greenlets (indefinitely) | ||
runner.greenlet.join() | ||
env.runner.start(1, hatch_rate=10) | ||
|
||
# in 60 seconds stop the runner | ||
gevent.spawn_later(60, lambda: env.runner.quit()) | ||
|
||
# wait for the greenlets | ||
env.runner.greenlet.join() | ||
|
||
# stop the web server for good measures | ||
env.web_ui.stop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.