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) "
0 commit comments