Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finished two testplans #5

Merged
merged 30 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5b3d919
Updated docs and config.
alfredeen Jan 10, 2024
8af2e1a
Small change to instructions.
alfredeen Jan 12, 2024
7dc91f1
Refactored into using abstract base user types. These are implemented…
alfredeen Jan 12, 2024
0b74393
Added an app viewer user
alfredeen Jan 12, 2024
8f96c6f
Work on login to Serve as test user.
alfredeen Jan 16, 2024
a180aeb
Completed authentication methods. Work on user type power user..
alfredeen Jan 16, 2024
991cbe1
Created non-locust module for opening user apps which created k8s pod…
alfredeen Jan 18, 2024
16c6066
More settings and configuration to customize the user app behaviour
alfredeen Jan 18, 2024
82e8720
Better handling of switching between test environments.
alfredeen Jan 19, 2024
8286b6a
Added task to register new user account.
alfredeen Jan 19, 2024
0855d6a
Began test plan classroom. Implemented power user create and delete u…
alfredeen Jan 22, 2024
0075962
Finished test plan classroom.
alfredeen Jan 22, 2024
3aad2da
Small change to readme to make configuration URL into a link.
alfredeen Feb 1, 2024
b4b3c42
Added config files for pre-commit and flake8.
alfredeen Feb 1, 2024
db19a75
Added github workflow pre-commit
alfredeen Feb 1, 2024
0ec1f15
Update source/tests-dev/appviewer_requestshtml.py
alfredeen Feb 1, 2024
01cc795
Update source/tests-dev/appviewer_requestshtml.py
alfredeen Feb 1, 2024
0d82d4c
Update source/tests-dev/appviewer_requestshtml.py
alfredeen Feb 1, 2024
b955402
Update source/tests-dev/authenticated.py
alfredeen Feb 1, 2024
12b8fc9
Moved code check config files up to root level.
alfredeen Feb 1, 2024
359a383
Reformatted with code linters and checkers
alfredeen Feb 1, 2024
36c5c7f
Reformatted imports with isort
alfredeen Feb 1, 2024
b67f157
Reformatted and code changes for the mypy type checker.
alfredeen Feb 1, 2024
626027c
Fixed end of line rules
alfredeen Feb 1, 2024
046cdad
Introduced the standard logging library. Converted most prints to log…
alfredeen Feb 1, 2024
c176ef8
Update source/tests/base_user_types.py
alfredeen Feb 2, 2024
b29ce3f
Update source/tests/base_user_types.py
alfredeen Feb 2, 2024
14e0035
Clearer setup instructions
alfredeen Feb 2, 2024
e1c10eb
Formatting
alfredeen Feb 2, 2024
ab83dc1
Formatting
alfredeen Feb 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[flake8]
max-line-length = 120
extend-ignore =
# Whitespace before ':'
E203,
# imported but unused
F401
exclude =
.git,
__pycache__,
env,
.env,
venv,
.venv
Empty file added .github/workflows/.gitkeep
Empty file.
30 changes: 30 additions & 0 deletions .github/workflows/pre-commit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Pre-commit linters and code checks

on:
pull_request:
paths-ignore:
- 'README.md'
push:
branches:
- develop
paths-ignore:
- 'README.md'

jobs:
pre_commit:
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: 3.8

- name: Install dependencies
run: |
python -m pip install --upgrade pre-commit==3.3.3

- name: Run pre-commit
run: pre-commit run --all-files
52 changes: 52 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
default_stages:
- commit
repos:
# general hooks to verify or beautify code
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-added-large-files
args: [--maxkb=5000]
- id: trailing-whitespace
- id: check-json
- id: check-merge-conflict
- id: check-xml
- id: check-yaml
exclude: ^charts/
- id: detect-private-key
- id: mixed-line-ending
- id: end-of-file-fixer
- id: pretty-format-json
args: [--autofix]


# autoformat code with black formatter
- repo: https://github.com/psf/black
rev: 23.11.0
hooks:
- id: black

# beautify and sort imports
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
additional_dependencies: [toml]
exclude: '^(.*?/)?migrations/.*\.py$'


# check code style
- repo: https://github.com/pycqa/flake8
rev: 6.1.0
hooks:
- id: flake8
# exclude added here because the .flake8 settings are not respected by pre-commit --all-files
exclude: __init__.py|.venv|.env|venv|env|__pycache__


# static type checking
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.7.0
hooks:
- id: mypy
additional_dependencies: [types-requests==2.25.9]
85 changes: 61 additions & 24 deletions README.md
alfredeen marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -23,82 +23,119 @@ Make changes in the develop branch, commit and submit pull requests to merge to
## Configuration options

To configure the test runs, use the Locust configuration file ./source/locust.conf
For options, see https://docs.locust.io/en/stable/configuration.html
For options, see the [Locust docs](https://docs.locust.io/en/stable/configuration.html)

## Create tests

Create locust tests in the /source/tests directory.

## Verify the setup by running a simple test

These tests do not need access to a host (URL) to test against.

### Running tests using the command line (headlessly)

Run the command:

locust --headless -f ./tests/simple.py --users 1 --run-time 10s --html ./reports/locust-report-simple.html
locust --headless -f ./tests/test_verify_setup.py --users 1 --run-time 10s --html ./reports/locust-report-verify-setup.html

Open the generated html report and verify that there are no errors and that the statistics look reasonable. The report is created under /source/reports/

### Using the Locust UI web client

Run the commands
Run the command

cd ./tests
locust --modern-ui --class-picker -f simple.py
locust --config locust-ui.conf --modern-ui --class-picker -f ./tests/test_verify_setup.py --html ./reports/locust-report-verify-setup-ui.html

Open a browser tab at URL http://localhost:8089/

Paste in as host

https://staging.serve-dev.scilifelab.se

## Verify the setup and access to a host (URL)

Run the command:

locust --headless -f ./tests/test_verify_host.py --users 1 --run-time 10s --html ./reports/locust-report-verify-host.html

Open the generated html report and verify that there are no errors and that the statistics look reasonable. The report is created under /source/reports/


## Running tests

If desired generate html reports by editing the report name in the below commands.
### Prepare for running tests

### Run tests headlessly
Before running the tests, run scripts to pre-create test users "locust_test_user_"*
alfredeen marked this conversation as resolved.
Show resolved Hide resolved

Move into the source directory if not already there:

cd ./source

### To run only the Website tests
- Copy the template environment file .env.template as .env
- Edit the values in the .env file
alfredeen marked this conversation as resolved.
Show resolved Hide resolved

locust -f ./tests/website.py --html ./reports/locust-report-website.html
Set the environment values from the file

### To run only the API tests
set -o allexport; source .env; set +o allexport

locust -f ./tests/api.py --html ./reports/locust-report-api.html
If desired then generate html reports by editing the report name in the below commands.

### To run all tests, execute one of the below commands. Beware, please be nice to the system resources.
### To run the Normal test plan/scenario

The configuration file parameter here is not necessary but is included for extra intelligibility.
Use minimum 10 users for the Normal test plan

locust --config locust.conf --html ./reports/locust-report-all.html
locust --headless -f ./tests/test_plan_normal.py --html ./reports/locust-report-normal.html --users 10 --run-time 30s

This executes the same command as above.
### To run the Classroom test plan/scenario

locust --html ./reports/locust-report-all.html
locust --headless -f ./tests/test_plan_classroom.py --html ./reports/locust-report-classroom.html --users 1 --run-time 30s


## Tests under development

These tests are not yet ready to be used in a load testing session.
The tests are located under directory /source/tests-dev/

### To run only the AppViewer tests (using user apps as a non-authenticated user)
### To run only the AppViewer tests

This test uses a non-authenticated user

locust -f ./tests-dev/appviewer.py --html ./reports/locust-report-appviewer.html --users 1 --run-time 20s
locust --headless -f ./tests-dev/appviewer.py --html ./reports/locust-report-appviewer.html --users 1 --run-time 20s

### To run only the test class requiring authentication

These tests require a user account in Serve and a protected page such as a project page.

- Copy the template environment file .env.template as .env
- Edit the values in the .env file
Run the tests

Set the environment values from the file
locust --headless -f ./tests-dev/authenticated.py --html ./reports/locust-report-authenticated.html --users 1 --run-time 10s

set -o allexport; source .env; set +o allexport

Run the tests
## To run tests using a Docker base image

Using provided Locust base image. To select which tests to execute, edit the file parameter -f as shown above.

cd ./source

docker run -p 8089:8089 -v $PWD:/mnt/locust locustio/locust -f /mnt/locust/tests/simple.py --config /mnt/locust/locust.conf --html /mnt/locust/reports/locust-report-from-docker.html


## To run k8s pod-creating tests

The Locust app-viewer tests do not currently create pods on the cluster. In order to run such tests, configure and execute appviewer_requestshtml.py

python3 ./tests-dev/appviewer_requestshtml.py


## Use the shell script to run tests

The shell script can be used to execute multiple simultaneous types of tests (such as Locust plus a scripted test).

Edit the configuration in locust.conf and in .env. If running the appviewer_requestshtml.py module, then also configure settings in this file.

locust -f ./tests-dev/authenticated.py --html ./reports/locust-report-authenticated.html --users 1 --run-time 10s
```
$ cd ./source
$ chmod +x run_test_plan.sh
$ ./run_test_plan.sh
```
53 changes: 53 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# This file contains project information.
# It also contains settings for linters and code checkers isort, black and mypy.
# Note that these settings are not respected with pre-commit run --all-files
# In that case add configurations to the .pre-commit-config.yaml file.

[project]
name = "serve-load-testing"
version = "1.0.0"
description = "Load testing of the SciLifeLab Serve platform."
requires-python = "=3.8"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is a new project, maybe let's use some newer version of python like 3.12?

keywords = ["load testing", "locust", "python"]

[tool.isort]
profile = 'black'

[tool.black]
line-length = 120
target-version = ['py38']
include = '\.pyi?$'
extend-exclude = '''
/(
\.git
| \.mypy_cache
| \.venv
| venv
| migrations
)/
'''

[tool.mypy]
strict = false
python_version = "3.8"
ignore_missing_imports = true
warn_return_any = true
exclude = ["venv", ".venv", "examples"]

[[tool.mypy.overrides]]
module = "*.migrations.*"
ignore_errors = true

[[tool.mypy.overrides]]
module = [
"flatten_json.*",
"guardian.*",
"tagulous.*",
"dash.*",
"markdown.*",
"pytz.*",
"requests.*",
"setuptools.*",
"yaml.*",
]
ignore_missing_imports = true
4 changes: 3 additions & 1 deletion source/.env.template
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[email protected]
SERVE_PASS=a_pass
PROTECTED_PAGE_RELATIVE_URL=/a/relative/url
SERVE_LOCUST_TEST_USER_PASS=a_pass2
SERVE_LOCUST_DO_CREATE_OBJECTS=True
PROTECTED_PAGE_RELATIVE_URL=/a/relative/url
12 changes: 12 additions & 0 deletions source/locust-ui.conf
alfredeen marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# locust.conf
locustfile = tests
headless = false
#master = true
#expect-workers = 5
host = https://staging.serve-dev.scilifelab.se
users = 1
spawn-rate = 1
run-time = 10s
only-summary = true
csv = stats/locust
loglevel = INFO
9 changes: 5 additions & 4 deletions source/locust.conf
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# locust.conf
locustfile = tests
headless = true
#headless = true
#master = true
#expect-workers = 5
host = https://staging.serve-dev.scilifelab.se
host = https://serve-dev.scilifelab.se
users = 1
spawn-rate = 1
run-time = 10s
only-summary = true
csv = stats/locust
#only-summary = true
csv = stats/locust
loglevel = INFO
2 changes: 1 addition & 1 deletion source/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
locust>=2.20.0
requests-html>=0.10.0
requests-html>=0.10.0
14 changes: 14 additions & 0 deletions source/run_test_plan.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

set -o errexit

# Activate the virtual env
source ./.venv/bin/activate

# Set the env variables from this project
set -o allexport; source .env; set +o allexport

# Run 2 sets of tests in parallel: the non-locust user apps and the locust test plan

python3 ./tests-dev/appviewer_requestshtml.py & \
locust --headless -f ./tests/test_plan_normal.py --users 10 --run-time 60s --html ./reports/locust-report-normal.html
Loading
Loading