Skip to content

Commit 1479ee2

Browse files
author
Dennis Kieselhorst
authored
Merge pull request #1239 from brrist/main
Lab Dragon Resort
2 parents f882e20 + 429da5b commit 1479ee2

File tree

7 files changed

+167
-0
lines changed

7 files changed

+167
-0
lines changed

labs/dragon-resort/env.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"DragonResortApp": {
3+
"DYNAMODB_ENDPOINT": "http://host.docker.internal:8000",
4+
"DYNAMODB_TABLE_NAME": "DragonTable"
5+
}
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"httpMethod": "POST",
3+
"path": "/",
4+
"headers": {"Content-Type": "application/json"},
5+
"body": "{\"id\":\"11111111-2222-3333-4444-555555555555\",\"name\":\"Elliot\",\"type\":\"Forest Dragon\",\"age\":\"1200\",\"color\":\"green\"}"
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"httpMethod": "DELETE",
3+
"path": "/",
4+
"headers": {"Content-Type": "application/json"},
5+
"pathParameters": {"id": "11111111-2222-3333-4444-555555555555"},
6+
"body": null
7+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#!/bin/bash
2+
3+
# Get API endpoint from AWS CLI using API name for REST API (v1)
4+
API_NAME="Dragon Store API"
5+
6+
# Get API ID from name (REST API)
7+
API_ID=$(aws apigateway get-rest-apis --region $AWS_REGION | \
8+
jq -r --arg NAME "$API_NAME" '.items[] | select(.name==$NAME) | .id')
9+
10+
if [ -z "$API_ID" ]; then
11+
echo "Failed to find REST API with name: $API_NAME"
12+
exit 1
13+
fi
14+
15+
# Construct API endpoint for REST API
16+
API_ENDPOINT="https://${API_ID}.execute-api.${AWS_REGION}.amazonaws.com/prod"
17+
18+
# Print API information for verification
19+
echo "Found API ID: $API_ID"
20+
echo "Using API endpoint: $API_ENDPOINT"
21+
22+
# Rest of the configuration
23+
NUM_REQUESTS=100
24+
CONTENT_TYPE="Content-Type: application/json"
25+
26+
# Dragon characteristics arrays
27+
FIRST_NAMES=("Ancient" "Mighty" "Wise" "Fierce" "Noble" "Shadow" "Storm" "Crystal" "Ember" "Frost")
28+
SECOND_NAMES=("Wing" "Claw" "Fang" "Scale" "Heart" "Soul" "Spirit" "Flame" "Thunder" "Ice")
29+
TYPES=("Celestial Dragon" "Fire Dragon" "Ice Dragon" "Storm Dragon" "Earth Dragon")
30+
COLORS=("bronze" "golden" "silver" "crimson" "azure" "emerald" "obsidian")
31+
32+
# Function to get random element from an array
33+
get_random_element() {
34+
local array=("$@")
35+
local index=$((RANDOM % ${#array[@]}))
36+
echo "${array[$index]}"
37+
}
38+
39+
# Function to generate random number in range
40+
random_number() {
41+
local min=$1
42+
local max=$2
43+
echo $((RANDOM % (max - min + 1) + min))
44+
}
45+
46+
# Function to make API calls with error handling and response logging
47+
make_request() {
48+
local method=$1
49+
local endpoint=$2
50+
local data=$3
51+
52+
response=$(curl -s -w "\n%{http_code}" -X "$method" \
53+
"$API_ENDPOINT$endpoint" \
54+
-H "$CONTENT_TYPE" \
55+
${data:+-d "$data"})
56+
57+
status_code=$(echo "$response" | tail -n1)
58+
response_body=$(echo "$response" | sed '$d')
59+
60+
if [ "$status_code" -lt 200 ] || [ "$status_code" -gt 299 ]; then
61+
echo "Error: $method request to $endpoint failed with status $status_code"
62+
echo "Response: $response_body"
63+
fi
64+
}
65+
66+
# Function to create dragon payload with random attributes
67+
create_dragon_payload() {
68+
local id=$1
69+
local first_name=$(get_random_element "${FIRST_NAMES[@]}")
70+
local second_name=$(get_random_element "${SECOND_NAMES[@]}")
71+
local dragon_type=$(get_random_element "${TYPES[@]}")
72+
local color=$(get_random_element "${COLORS[@]}")
73+
local age=$(random_number 100 5000)
74+
75+
echo "{
76+
\"id\": \"$id\",
77+
\"name\": \"$first_name$second_name\",
78+
\"type\": \"$dragon_type\",
79+
\"age\": \"$age\",
80+
\"color\": \"$color\"
81+
}"
82+
}
83+
84+
# Function to run batch of requests with progress indicator
85+
run_batch() {
86+
local operation=$1
87+
local total=$2
88+
local successful=0
89+
local failed=0
90+
91+
echo "Starting $operation test..."
92+
for ((i=1; i<=$total; i++)); do
93+
echo -ne "Progress: $i/$total (Success: $successful, Failed: $failed)\r"
94+
95+
case $operation in
96+
"CREATE")
97+
make_request "POST" "/dragons" "$(create_dragon_payload $i)"
98+
;;
99+
"LIST")
100+
make_request "GET" "/dragons"
101+
;;
102+
"GET")
103+
make_request "GET" "/dragons/$i"
104+
;;
105+
"DELETE")
106+
make_request "DELETE" "/dragons/$i"
107+
;;
108+
esac
109+
110+
if [ $? -eq 0 ]; then
111+
((successful++))
112+
else
113+
((failed++))
114+
fi
115+
116+
# Small delay to prevent overwhelming the API
117+
sleep 0.1
118+
done
119+
echo -e "\nCompleted $operation test (Success: $successful, Failed: $failed)"
120+
}
121+
122+
# Main execution
123+
echo "Starting load test at $(date)"
124+
run_batch "CREATE" $NUM_REQUESTS
125+
run_batch "LIST" $NUM_REQUESTS
126+
run_batch "GET" $NUM_REQUESTS
127+
run_batch "DELETE" $NUM_REQUESTS
128+
echo "Load test completed at $(date)"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"httpMethod": "GET",
3+
"path": "/",
4+
"headers": {"Content-Type": "application/json"},
5+
"pathParameters": {"id": "11111111-2222-3333-4444-555555555555"},
6+
"body": null
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"httpMethod": "GET",
3+
"path": "/",
4+
"headers": {"Content-Type": "application/json"},
5+
"body": null
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"httpMethod": "PUT",
3+
"path": "/",
4+
"headers": {"Content-Type": "application/json"},
5+
"pathParameters": {"id": "11111111-2222-3333-4444-555555555555"},
6+
"body": "{\"name\":\"Elliot\",\"type\":\"Red Forest Dragon\",\"age\":\"1200\",\"color\":\"red\"}"
7+
}

0 commit comments

Comments
 (0)