From 1854f7c97a12c4828bf72ef9c22bfa4b67d1af62 Mon Sep 17 00:00:00 2001 From: Riad Elimemmedov Date: Sat, 24 Feb 2024 12:59:04 +0400 Subject: [PATCH] Automate stress testing process --- backend/apps/pet/views.py | 3 +++ backend/config/base.py | 6 +++++- backend/scripts/benchmarks.py | 22 ++++++++++++++++++++++ commands.md | 3 +++ docker-compose.yml | 22 +++++++++++----------- 5 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 backend/scripts/benchmarks.py diff --git a/backend/apps/pet/views.py b/backend/apps/pet/views.py index e5652be..c9741e2 100644 --- a/backend/apps/pet/views.py +++ b/backend/apps/pet/views.py @@ -1,6 +1,7 @@ from django.conf import settings from django.core.files.storage import FileSystemStorage from django.shortcuts import render +from django.views.decorators.cache import cache_page from rest_framework import status from rest_framework.response import Response from rest_framework.views import APIView @@ -17,6 +18,7 @@ class PetView(APIView): serializer_class = PetSerializer + # @cache_page(60) def get(self, request): """ Retrieve a list of all pets. @@ -28,6 +30,7 @@ def get(self, request): Response: A response object containing the serialized data of all pets and the HTTP status code. """ pets = Pet.objects.all() + print("Cached dataaaaa") serializer = self.serializer_class(pets, many=True) return Response(serializer.data, status=status.HTTP_200_OK) diff --git a/backend/config/base.py b/backend/config/base.py index 21f66d9..dc98694 100644 --- a/backend/config/base.py +++ b/backend/config/base.py @@ -275,7 +275,11 @@ CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", - "LOCATION": "redis://127.0.0.1:6379/1", + "LOCATION": "redis://{}:{}/{}".format( + config("REDIS_HOST"), + config("REDIS_PORT"), + config("REDIS_DB"), + ), "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", }, diff --git a/backend/scripts/benchmarks.py b/backend/scripts/benchmarks.py new file mode 100644 index 0000000..6e870df --- /dev/null +++ b/backend/scripts/benchmarks.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python + +import subprocess + + +# ?run_ab +def run_ab(url: str): + # http://127.0.0.1:8000/pets/ + # Run the ab command with the specified parameters and capture the output + process = subprocess.Popen( + ["./ab.exe", "-c 10", "-n 20", f"{url}"], + stdout=subprocess.PIPE, + universal_newlines=True, + ) + # Wait for the process to finish and get the output + output, _ = process.communicate() + + # Print the output + print("AB result is ", output) + + +run_ab("http://127.0.0.1:8000/pets/") diff --git a/commands.md b/commands.md index ed84a23..4b4839e 100644 --- a/commands.md +++ b/commands.md @@ -63,3 +63,6 @@ Backend - `poetry run waitress-serve --listen=*:8000 config.wsgi:application` - Stress test using Apache Benchmark - `./ab -c 10 -n 20 http://127.0.0.1:8000/pets/` + +- `./ab -c 10 -n 20 http://localhost:8000/pets/` + chmod +x ./ab.exe diff --git a/docker-compose.yml b/docker-compose.yml index cd9318e..50dd8b7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,14 +18,14 @@ services: container_name: redis image: redis:alpine - frontend: - container_name: frontend - build: ./frontend/contract-ui/ - ports: - - 5173:8001 #http://localhost:5173/ - working_dir: /frontend/contract-ui/ - volumes: - - type: bind - source: ./frontend/contract-ui/ - target: /frontend/contract-ui/ - tty: true + # frontend: + # container_name: frontend + # build: ./frontend/contract-ui/ + # ports: + # - 5173:8001 #http://localhost:5173/ + # working_dir: /frontend/contract-ui/ + # volumes: + # - type: bind + # source: ./frontend/contract-ui/ + # target: /frontend/contract-ui/ + # tty: true