Skip to content

Commit

Permalink
feat(sandbox): prototype locust integration to the monorepo (#2896)
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaffter authored Oct 14, 2024
1 parent b95bc34 commit 1493b11
Show file tree
Hide file tree
Showing 14 changed files with 1,728 additions and 1 deletion.
53 changes: 53 additions & 0 deletions .github/workflows/locust-load-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Locust Load Test

on:
workflow_dispatch:
inputs:
product:
type: choice
description: 'Name of the product to test'
required: true
options:
- agora
- openchallenges
# user_count:
# description: 'Number of users to simulate in Locust'
# required: true
# default: '50'
# spawn_rate:
# description: 'Rate at which users are spawned (users per second)'
# required: true
# default: '10'

jobs:
deploy:
runs-on: ubuntu-22.04-4core-16GBRAM-150GBSSD
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 1

- name: Derive appropriate SHAs for base and head for `nx affected` commands
uses: nrwl/nx-set-shas@v4

- name: Set up the dev container
uses: ./.github/actions/setup-dev-container

# - name: Build the specified product, serve it, and perform a load test
# run: |
# devcontainer exec --workspace-folder ../sage-monorepo bash -c ". ./dev-env.sh \
# && nx run-many --target=build-image --projects=${{ inputs.product }}-*" \
# && nx serve-detach ${{ inputs.product }}-apex \
# && nx serve sandbox-locust --configuration=${{ inputs.product }}

- name: Perform a load test
run: |
devcontainer exec --workspace-folder ../sage-monorepo bash -c ". ./dev-env.sh \
&& nx run sandbox-locust:serve:${{ inputs.product }} --headless
- name: Upload Locust reports as artifacts
uses: actions/upload-artifact@v4
with:
name: locust-reports
path: apps/sandbox/locust/locust_*.csv
11 changes: 11 additions & 0 deletions apps/sandbox/locust/.flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[flake8]
exclude =
.git,
__pycache__,
build,
dist,
.tox,
venv,
.venv,
.pytest_cache
max-line-length = 120
2 changes: 2 additions & 0 deletions apps/sandbox/locust/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Locust reports
locust_*.csv
1 change: 1 addition & 0 deletions apps/sandbox/locust/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.10.14
Empty file added apps/sandbox/locust/README.md
Empty file.
13 changes: 13 additions & 0 deletions apps/sandbox/locust/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

PYTHON_VERSION="3.10.14"

pyenv install --skip-existing $PYTHON_VERSION

# Initializing pyenv again solves an issue encountered by GitHub action where the version of Python
# installed above is not detected.
eval "$(pyenv init -)"

pyenv local $PYTHON_VERSION
poetry env use $(pyenv which python)
poetry install --with dev
1,511 changes: 1,511 additions & 0 deletions apps/sandbox/locust/poetry.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions apps/sandbox/locust/poetry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[virtualenvs]
in-project = true
70 changes: 70 additions & 0 deletions apps/sandbox/locust/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"name": "sandbox-locust",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"projectType": "application",
"sourceRoot": "apps/sandbox/locust/locust",
"targets": {
"lock": {
"executor": "nx:run-commands",
"options": {
"command": "poetry lock --no-update",
"cwd": "{projectRoot}"
}
},
"build": {
"executor": "@nxlv/python:build",
"outputs": ["{projectRoot}/dist"],
"options": {
"outputPath": "{projectRoot}/dist",
"publish": false,
"lockedVersions": true,
"bundleLocalDependencies": true
}
},
"prepare": {
"executor": "nx:run-commands",
"options": {
"command": "./install.sh",
"cwd": "{projectRoot}"
}
},
"update": {
"executor": "nx:run-commands",
"options": {
"command": "poetry update",
"cwd": "{projectRoot}"
}
},
"lint": {
"executor": "@nxlv/python:flake8",
"outputs": ["{workspaceRoot}/reports/{projectRoot}/pylint.txt"],
"options": {
"outputFile": "reports/{projectRoot}/pylint.txt"
}
},
"serve": {
"executor": "nx:run-commands",
"options": {
"command": "poetry run locust",
"cwd": "{projectRoot}"
},
"configurations": {
"agora": {
"locustfile": "sandbox_locust/locustfile_agora.py",
"host": "http://localhost:8000"
},
"openchallenges": {
"locustfile": "sandbox_locust/locustfile_openchallenges.py",
"host": "https://openchallenges.io"
}
},
"defaultConfiguration": "agora"
}
},
"tags": ["language:python", "package-manager:poetry"],
"release": {
"version": {
"generator": "@nxlv/python:release-version"
}
}
}
46 changes: 46 additions & 0 deletions apps/sandbox/locust/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[tool.coverage.run]
branch = true
source = [ "sandbox_locust" ]

[tool.coverage.report]
exclude_lines = [ "if TYPE_CHECKING:" ]
show_missing = true

[tool.poetry]
name = "sandbox-locust"
version = "1.0.0"
description = ""
authors = [ ]
license = "Proprietary"
readme = "README.md"

[[tool.poetry.packages]]
include = "sandbox_locust"

[tool.poetry.dependencies]
python = "3.10.14"
locust = "2.31.8"

[tool.poetry.group.dev.dependencies]
autopep8 = "2.0.2"
flake8 = "6.0.0"
pytest = "7.3.1"
pytest-sugar = "0.9.7"
pytest-cov = "4.1.0"
pytest-html = "3.2.0"

[tool.locust]
# locustfile = ""
headless = false
# master = false
# expect-workers = 5
# host = ""
users = 10
spawn-rate = 2
run-time = "1m"
csv = "locust"
# tags = ["Critical", "Normal"]

[build-system]
requires = [ "poetry-core" ]
build-backend = "poetry.core.masonry.api"
Empty file.
10 changes: 10 additions & 0 deletions apps/sandbox/locust/sandbox_locust/locustfile_agora.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from locust import HttpUser, task, between


class AgoraUser(HttpUser):
wait_time = between(1, 5)

@task
def hello_world(self):
self.client.get("/")
self.client.get("/about")
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from locust import HttpUser, task


class OpenChallengesUser(HttpUser):
@task
def hello_world(self):
self.client.get("/")
self.client.get("/about")
2 changes: 1 addition & 1 deletion libs/sage-monorepo/nx-plugin/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function writeProjectConfigurationsToCache(
writeJsonFile(cachePath, results);
}

const projectFilePattern = '{apps,libs}/{openchallenges,agora}/**/project.json';
const projectFilePattern = '{apps,libs}/{openchallenges,agora,sandbox}/**/project.json';

export const createNodesV2: CreateNodesV2<SageMonorepoPluginOptions> = [
projectFilePattern,
Expand Down

0 comments on commit 1493b11

Please sign in to comment.