Skip to content

Commit 841a416

Browse files
Create cpu_spike.py
1 parent 86d9246 commit 841a416

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import time
2+
3+
def simulate_cpu_spike(duration=30, cpu_percent=80):
4+
print(f"Simulating CPU spike at {cpu_percent}%...")
5+
start_time = time.time()
6+
7+
# Calculate the number of iterations needed to achieve the desired CPU utilization
8+
target_percent = cpu_percent / 100
9+
total_iterations = int(target_percent * 5_000_000) # Adjust the number as needed
10+
11+
# Perform simple arithmetic operations to spike CPU utilization
12+
for _ in range(total_iterations):
13+
result = 0
14+
for i in range(1, 1001):
15+
result += i
16+
17+
# Wait for the rest of the time interval
18+
elapsed_time = time.time() - start_time
19+
remaining_time = max(0, duration - elapsed_time)
20+
time.sleep(remaining_time)
21+
22+
print("CPU spike simulation completed.")
23+
24+
if __name__ == '__main__':
25+
# Simulate a CPU spike for 30 seconds with 80% CPU utilization
26+
simulate_cpu_spike(duration=30, cpu_percent=80)

0 commit comments

Comments
 (0)