Skip to content

Commit 766bb17

Browse files
authored
Merge pull request #49 from DevOps-Cloud-Team5/SCRUM-92_cf
load testing, testing, tf doc
2 parents d0fd3f6 + 046d159 commit 766bb17

6 files changed

+17169
-0
lines changed

.github/workflows/cd.yml

+12
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ jobs:
5050
chmod +x ./getEnv.sh && ./getEnv.sh
5151
python manage.py migrate
5252
53+
- name: Run DB migrations
54+
run: |
55+
source venv/bin/activate
56+
chmod +x ./getEnv.sh && ./getEnv.sh
57+
python manage.py migrate
58+
59+
- name: Run API Tests
60+
run: |
61+
source venv/bin/activate
62+
chmod +x ./getEnv.sh && ./getEnv.sh
63+
chmod +x ./test_backend.sh && ./test_backend.sh
64+
5365
- name: Setup up Requirements
5466
run: |
5567
source venv/bin/activate

.github/workflows/cloudFormation.yml

+6
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ jobs:
9595
chmod +x ./getEnv.sh && ./getEnv.sh
9696
python manage.py migrate
9797
98+
- name: Run API Tests
99+
run: |
100+
source venv/bin/activate
101+
chmod +x ./getEnv.sh && ./getEnv.sh
102+
chmod +x ./test_backend.sh && ./test_backend.sh
103+
98104
- name: Setup up Requirements
99105
run: |
100106
source venv/bin/activate

terraform.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Terraform AWS Lambda and API Gateway Configuration
2+
3+
This Terraform configuration sets up an AWS infrastructure that includes Lambda functions and an API Gateway to expose those functions as HTTP endpoints. This is part of the backend for the `PLACEHOLDER_PROJECT_NAME` project.
4+
5+
## Overview
6+
7+
The configuration provisions the following resources:
8+
- AWS IAM Role and Policy for Lambda execution and CloudWatch logging.
9+
- AWS Lambda function to run the application code.
10+
- AWS API Gateway to expose the Lambda function via HTTP endpoints.
11+
- S3 Bucket Website Configuration for redirects.
12+
13+
## Requirements
14+
15+
- Terraform ~> 5.0
16+
- AWS Provider ~> 3.0
17+
- AWS CLI configured with appropriate access rights.
18+
19+
## Usage
20+
21+
To use this Terraform configuration:
22+
1. Ensure AWS CLI is configured with the necessary access credentials.
23+
2. Replace `PLACEHOLDER_PROJECT_NAME` and `PLACEHOLDER_REGION` with actual values in the configuration.
24+
3. Initialize Terraform:
25+
```bash
26+
terraform init
27+
```
28+
4. Apply the Terraform configuration:
29+
```bash
30+
terraform apply
31+
```
32+
33+
## Resources
34+
35+
- **aws_iam_role.lambda_role**: IAM Role for AWS Lambda execution with the necessary permissions.
36+
- **aws_lambda_function.app**: The Lambda function containing the backend logic.
37+
- **aws_api_gateway_rest_api.api**: The API Gateway setup for HTTP access to the Lambda function.
38+
- **aws_api_gateway_deployment.deployment**: Deployment instance for the API Gateway.
39+
- **aws_s3_bucket_website_configuration.redirect_bucket**: S3 Bucket configured for web hosting and redirects.
40+
- **aws_iam_policy.cloudwatch_logs**: IAM Policy for logging to AWS CloudWatch.
41+
- **aws_iam_role_policy_attachment.cloudwatch_logs_attachment**: Attaches the CloudWatch logging policy to the Lambda execution role.
42+
43+
## Outputs
44+
45+
- **api_url**: The URL of the deployed API Gateway endpoint. This URL is used to access the Lambda function over the web.
46+
47+
## Notes
48+
49+
- Ensure to replace `PLACEHOLDER_PROJECT_NAME` with your project's name to correctly configure resource names and references.
50+
- `PLACEHOLDER_REGION` should be replaced with the AWS region you intend to deploy your resources in.
51+
- It is recommended to manage sensitive information, such as AWS credentials and any other secrets, outside of this Terraform configuration for security reasons.

test_load.py

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import requests
2+
import concurrent.futures
3+
import time
4+
import sys
5+
6+
# Redirect standard output to a file
7+
original_stdout = sys.stdout
8+
with open('test_load_details.txt', 'w') as f:
9+
sys.stdout = f
10+
11+
# The URL of the endpoint you want to test
12+
url = 'https://bmjg67cbef.execute-api.eu-central-1.amazonaws.com/prod/docs/'
13+
14+
# Function to make a single HTTP request
15+
def make_request():
16+
try:
17+
start_time = time.time()
18+
response = requests.get(url) # You can change this to requests.post() if needed
19+
end_time = time.time()
20+
21+
return {
22+
'status_code': response.status_code,
23+
'response_time': end_time - start_time
24+
}
25+
except Exception as e:
26+
return {
27+
'error': str(e)
28+
}
29+
30+
# Number of requests you want to make (if negative, resort to time_limit)
31+
num_requests = 12000
32+
33+
# Time limit for the load test (2 minutes)
34+
time_limit = 120
35+
36+
# List to store response times and status codes
37+
responses = []
38+
39+
# Record the start time of the load test
40+
start_time = time.time()
41+
42+
# Use ThreadPoolExecutor to manage concurrent requests
43+
if num_requests > 0:
44+
with concurrent.futures.ThreadPoolExecutor(max_workers=50) as executor:
45+
future_to_request = {executor.submit(make_request): i for i in range(num_requests)}
46+
for future in concurrent.futures.as_completed(future_to_request):
47+
response = future.result()
48+
responses.append(response)
49+
else:
50+
with concurrent.futures.ThreadPoolExecutor(max_workers=50) as executor:
51+
# Run the loop for time_limit
52+
while time.time() - start_time < time_limit:
53+
# Submit a new request
54+
future = executor.submit(make_request)
55+
# Get the result of the request and add it to the list of responses
56+
try:
57+
response = future.result(timeout=1) # Set a timeout for each request if needed
58+
responses.append(response)
59+
except concurrent.futures.TimeoutError:
60+
responses.append({'error': 'Request timed out'})
61+
62+
# Record the end time of the load test
63+
end_time = time.time()
64+
65+
# Calculate and print the total execution time of the load test
66+
total_time = end_time - start_time
67+
print(f"Total execution time: {total_time} seconds")
68+
69+
# Print the total number of requests
70+
print(f"Total requests: {len(responses)} requests")
71+
72+
# Print the total number of successful requests
73+
success_responses = sum(1 for d in responses if d.get('status_code') == 200)
74+
print(f"Total successful requests: {success_responses} requests")
75+
76+
# Calculate and print the average response time
77+
average_response_time = sum(d['response_time'] for d in responses if 'response_time' in d) / len(responses)
78+
print(f"Average response time: {average_response_time} seconds")
79+
80+
# Calculate and print success rate
81+
success_rate = 100.0 * (1.0 * success_responses / len(responses))
82+
print(f"Success rate: {success_rate}%")
83+
84+
# Optionally, log the detailed responses or any errors encountered
85+
for response in responses:
86+
if 'error' in response:
87+
print(f"Error: {response['error']}")
88+
else:
89+
print(f"Status Code: {response['status_code']}, Response Time: {response['response_time']} seconds")
90+
91+
# Reset standard output to original
92+
sys.stdout = original_stdout

0 commit comments

Comments
 (0)