This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #281 from flibbertigibbet/feature/load-testing
Feature/load testing
- Loading branch information
Showing
6 changed files
with
179 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
FROM python:3-slim | ||
|
||
MAINTAINER Azavea | ||
|
||
RUN apt-get update && apt-get install -y --no-install-recommends \ | ||
ipython \ | ||
python3-pip \ | ||
build-essential \ | ||
&& rm -rf /var/lib/apt/lists/* | ||
|
||
COPY requirements.txt /tmp/ | ||
RUN pip3 install --upgrade pip && pip3 install --no-cache-dir -r /tmp/requirements.txt | ||
|
||
COPY ./ /opt/loadtest | ||
|
||
WORKDIR /opt/loadtest | ||
|
||
EXPOSE 8089 | ||
|
||
ENTRYPOINT ["/bin/bash"] | ||
|
||
CMD ["-c", "locust --host=${API_HOST}"] |
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 |
---|---|---|
@@ -0,0 +1,117 @@ | ||
#!/usr/bin/python3 | ||
|
||
from functools import partial | ||
import os | ||
import uuid | ||
|
||
from locust import HttpLocust, TaskSet, task | ||
|
||
CITY_ID = os.getenv('LOAD_TEST_CITY_ID') or 1 | ||
SCENARIO = os.getenv('LOAD_TEST_SCENARIO') or 'RCP85' | ||
|
||
DEFAULT_THRESHOLD_PARAMS = { | ||
'threshold': 100, | ||
'threshold_comparator': 'lte', | ||
'threshold_units': 'F' | ||
} | ||
|
||
|
||
def general_indicator_query(locust_object, indicator, params): | ||
# append a randomized extra parameter to the query to defeat caching | ||
params['random'] = uuid.uuid4() | ||
locust_object.client.get('/api/climate-data/{city}/{scenario}/indicator/{indicator}'.format( | ||
city=CITY_ID, scenario=SCENARIO, indicator=indicator), | ||
headers=locust_object.headers, | ||
params=params, | ||
name='{indicator} {agg}'.format(indicator=indicator, | ||
agg=params.get('time_aggregation', ''))) | ||
|
||
|
||
class UserBehavior(TaskSet): | ||
|
||
def get_api_url(self, url): | ||
""" Helper for querying API with authorization header and random parameter to defeat cache | ||
""" | ||
self.client.get(url, headers=self.headers, params={'random': uuid.uuid4()}, name=url) | ||
|
||
def build_indicator_queries(self): | ||
""" | ||
Add a load test query for each indciator / time aggregation | ||
""" | ||
indicators = self.client.get('/api/indicator/', | ||
headers=self.headers).json() | ||
for indicator in indicators: | ||
indicator_name = indicator['name'] | ||
if indicator_name.endswith('threshold'): | ||
params = DEFAULT_THRESHOLD_PARAMS | ||
if indicator_name.find('precepitation') > -1: | ||
params['threshold_units'] = 'in' | ||
self.tasks.append(partial(general_indicator_query, | ||
indicator=indicator_name, | ||
params=params)) | ||
else: | ||
# for non-threshold indicators, test all non-custom aggregation levels | ||
for agg in indicator['valid_aggregations']: | ||
if agg != 'custom': | ||
self.tasks.append(partial(general_indicator_query, | ||
indicator=indicator_name, | ||
params={'time_aggregation': agg})) | ||
|
||
def on_start(self): | ||
""" on_start is called when a Locust start before any task is scheduled """ | ||
print('starting locust...') | ||
token = os.getenv('API_TOKEN') | ||
if not token: | ||
raise ValueError('Must set API_TOKEN on environment to run load tests.') | ||
self.headers = {'Authorization': 'Token {token}'.format(token=token)} | ||
print('adding indciator queries...') | ||
indicator_tasks = TaskSet(self) | ||
self.build_indicator_queries() | ||
print('ready to go!') | ||
|
||
@task(1) | ||
def index(self): | ||
self.get_api_url('/') | ||
|
||
@task(1) | ||
def api_main(self): | ||
self.get_api_url('/api/') | ||
|
||
@task(1) | ||
def scenarios(self): | ||
self.get_api_url('/api/scenario/') | ||
|
||
@task(1) | ||
def scenario_details(self): | ||
self.get_api_url('/api/scenario/{scenario}/'.format(scenario=SCENARIO)) | ||
|
||
@task(1) | ||
def cities(self): | ||
self.get_api_url('/api/city/') | ||
|
||
@task(1) | ||
def city_data(self): | ||
self.get_api_url('/api/climate-data/{city}/{scenario}/'.format(city=CITY_ID, | ||
scenario=SCENARIO)) | ||
|
||
@task(1) | ||
def projects(self): | ||
self.get_api_url('/api/project/') | ||
|
||
@task(1) | ||
def climate_models(self): | ||
self.get_api_url('/api/climate-model/') | ||
|
||
@task(1) | ||
def climate_model_detail(self): | ||
self.get_api_url('/api/climate-model/ACCESS1-0/') | ||
|
||
@task(1) | ||
def indicator_list(self): | ||
self.get_api_url('/api/indicator/') | ||
|
||
|
||
class WebsiteUser(HttpLocust): | ||
task_set = UserBehavior | ||
min_wait = 5000 | ||
max_wait = 9000 |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
locustio==0.8a2 | ||
ipdb==0.10.2 | ||
ipython==5.2.2 |