-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinux-stress-test.sh
More file actions
203 lines (173 loc) · 5.75 KB
/
linux-stress-test.sh
File metadata and controls
203 lines (173 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/bin/bash
# CPU and Memory Stress Testing Script
# This script provides multiple methods to stress test your Linux instance
set -e
echo "=== EC2 Stress Testing Tools ==="
echo ""
# Function to install stress tools
install_stress_tools() {
echo "Installing stress testing tools..."
if command -v yum >/dev/null 2>&1; then
# Amazon Linux/RHEL/CentOS
yum update -y
yum install -y stress stress-ng htop
amazon-linux-extras install epel -y 2>/dev/null || true
elif command -v apt >/dev/null 2>&1; then
# Ubuntu/Debian
apt update
apt install -y stress stress-ng htop
else
echo "Unsupported package manager. Please install stress tools manually."
exit 1
fi
}
# Function to get system info
get_system_info() {
echo "=== System Information ==="
echo "CPU Cores: $(nproc)"
echo "Total Memory: $(free -h | awk '/^Mem:/ {print $2}')"
echo "Available Memory: $(free -h | awk '/^Mem:/ {print $7}')"
echo "Load Average: $(uptime | awk -F'load average:' '{print $2}')"
echo ""
}
# Function for CPU stress testing
cpu_stress_test() {
local duration=${1:-60}
local workers=${2:-$(nproc)}
echo "=== CPU Stress Test ==="
echo "Duration: ${duration} seconds"
echo "Workers: ${workers}"
echo "Starting CPU stress test..."
# Method 1: Using stress command
echo "Method 1: Using stress command"
stress --cpu ${workers} --timeout ${duration}s &
STRESS_PID=$!
# Monitor CPU usage
echo "Monitoring CPU usage (press Ctrl+C to stop monitoring)..."
while kill -0 $STRESS_PID 2>/dev/null; do
echo "CPU Usage: $(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | awk -F'%' '{print $1}')%"
sleep 5
done
echo "CPU stress test completed."
echo ""
}
# Function for memory stress testing
memory_stress_test() {
local duration=${1:-60}
local memory_mb=${2:-1024}
echo "=== Memory Stress Test ==="
echo "Duration: ${duration} seconds"
echo "Memory to allocate: ${memory_mb}MB"
echo "Starting memory stress test..."
# Method 1: Using stress command
echo "Method 1: Using stress command"
stress --vm 1 --vm-bytes ${memory_mb}M --timeout ${duration}s &
STRESS_PID=$!
# Monitor memory usage
echo "Monitoring memory usage (press Ctrl+C to stop monitoring)..."
while kill -0 $STRESS_PID 2>/dev/null; do
echo "Memory Usage: $(free | grep Mem | awk '{printf("%.2f%%", $3/$2 * 100.0)}')"
sleep 5
done
echo "Memory stress test completed."
echo ""
}
# Function for combined stress testing
combined_stress_test() {
local duration=${1:-60}
local cpu_workers=${2:-$(nproc)}
local memory_mb=${3:-1024}
echo "=== Combined CPU & Memory Stress Test ==="
echo "Duration: ${duration} seconds"
echo "CPU Workers: ${cpu_workers}"
echo "Memory: ${memory_mb}MB"
echo "Starting combined stress test..."
# Start both CPU and memory stress
stress --cpu ${cpu_workers} --vm 1 --vm-bytes ${memory_mb}M --timeout ${duration}s &
STRESS_PID=$!
# Monitor both CPU and memory
echo "Monitoring system resources..."
while kill -0 $STRESS_PID 2>/dev/null; do
CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | awk -F'%' '{print $1}')
MEM_USAGE=$(free | grep Mem | awk '{printf("%.2f", $3/$2 * 100.0)}')
echo "CPU: ${CPU_USAGE}% | Memory: ${MEM_USAGE}%"
sleep 5
done
echo "Combined stress test completed."
echo ""
}
# Function to show menu
show_menu() {
echo "=== Stress Test Options ==="
echo "1. Install stress tools"
echo "2. Show system information"
echo "3. CPU stress test"
echo "4. Memory stress test"
echo "5. Combined CPU & Memory stress test"
echo "6. Custom stress test"
echo "7. Exit"
echo ""
}
# Function for custom stress test
custom_stress_test() {
echo "=== Custom Stress Test Configuration ==="
read -p "Enter duration in seconds (default: 60): " duration
duration=${duration:-60}
read -p "Enter number of CPU workers (default: $(nproc)): " cpu_workers
cpu_workers=${cpu_workers:-$(nproc)}
read -p "Enter memory to allocate in MB (default: 1024): " memory_mb
memory_mb=${memory_mb:-1024}
echo ""
echo "Configuration:"
echo "Duration: ${duration}s"
echo "CPU Workers: ${cpu_workers}"
echo "Memory: ${memory_mb}MB"
echo ""
read -p "Proceed with this configuration? (y/n): " confirm
if [ "$confirm" = "y" ]; then
combined_stress_test "$duration" "$cpu_workers" "$memory_mb"
fi
}
# Main menu loop
while true; do
show_menu
read -p "Select an option (1-7): " choice
case $choice in
1)
install_stress_tools
;;
2)
get_system_info
;;
3)
echo "CPU Stress Test"
read -p "Enter duration in seconds (default: 60): " duration
read -p "Enter number of workers (default: $(nproc)): " workers
cpu_stress_test "${duration:-60}" "${workers:-$(nproc)}"
;;
4)
echo "Memory Stress Test"
read -p "Enter duration in seconds (default: 60): " duration
read -p "Enter memory in MB (default: 1024): " memory
memory_stress_test "${duration:-60}" "${memory:-1024}"
;;
5)
echo "Combined Stress Test"
read -p "Enter duration in seconds (default: 60): " duration
combined_stress_test "${duration:-60}"
;;
6)
custom_stress_test
;;
7)
echo "Exiting..."
exit 0
;;
*)
echo "Invalid option. Please try again."
;;
esac
echo ""
read -p "Press Enter to continue..."
echo ""
done