This was a project where I needed to solve an issue with multiple load performance tests simultaneously (using k6), the test users were experiencing collisions with usernames.
Rather than work out complex logic to have them determine which user number they were, I created this small development server to resolve it, all it does is serve up numbers for them. They borrow a number, and when they're done, they return it for another load test user.
Reminder this is just an example and no guarantees are provided. A Production grade WSGI server like gunicorn would be recommended over this basic flask example.
- Install Python
- Install Redis Cluster (I used redis-cluster helm chart from bitnami) and telepresence on a k3s cluster, but regular redis-cluster works fine too.
- (Optional) - setup virtualenv for your python
- Install from requirements:
pip install -r requirements.txt
- Create a .env file with your variables:
This example would serve numbers
REDIS_PASSWORD="secret" RANGE_START=10000 RANGE_AMOUNT=10000
10000-20000
- Run the server:
python BorrowANumber.py
The REST API to the example app is described below.
GET /checked
curl http://localhost:5000/checked
Connection: close
Content-Length: 27
Content-Type: application/json
Date: Fri, 01 Mar 2024 00:00:00 GMT
Server: Werkzeug/3.0.1 Python/3.12.0
{
"checked_out_numbers": []
}
GET /check
curl http://localhost:5000/check
HTTP/1.1 200 OK
Connection: close
Content-Length: 31
Content-Type: application/json
Date: Fri, 01 Mar 2024 00:00:00 GMT
Server: Werkzeug/3.0.1 Python/3.12.0
{
"checked_out_number": "16713"
}
POST /return
curl -H "Content-Type: application/json" -XPOST http://localhost:5000/return
HTTP/1.1 200 OK
Connection: close
Content-Length: 49
Content-Type: application/json
Date: Fri, 01 Mar 2024 00:00:00 GMT
Server: Werkzeug/3.0.1 Python/3.12.0
{
"message": "Number 16713 returned successfully"
}
GET /reset
curl http://localhost:5000/reset
HTTP/1.1 200 OK
Connection: close
Content-Length: 17
Content-Type: application/json
Date: Fri, 01 Mar 2024 00:00:00 GMT
Server: Werkzeug/3.0.1 Python/3.12.0
{
"reset": "true"
}