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