Skip to content

Commit

Permalink
Create cpu_spike.py
Browse files Browse the repository at this point in the history
  • Loading branch information
iam-veeramalla authored Jul 19, 2023
1 parent 86d9246 commit 841a416
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions day-16/default_metrics_demo/cpu_spike.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import time

def simulate_cpu_spike(duration=30, cpu_percent=80):
print(f"Simulating CPU spike at {cpu_percent}%...")
start_time = time.time()

# Calculate the number of iterations needed to achieve the desired CPU utilization
target_percent = cpu_percent / 100
total_iterations = int(target_percent * 5_000_000) # Adjust the number as needed

# Perform simple arithmetic operations to spike CPU utilization
for _ in range(total_iterations):
result = 0
for i in range(1, 1001):
result += i

# Wait for the rest of the time interval
elapsed_time = time.time() - start_time
remaining_time = max(0, duration - elapsed_time)
time.sleep(remaining_time)

print("CPU spike simulation completed.")

if __name__ == '__main__':
# Simulate a CPU spike for 30 seconds with 80% CPU utilization
simulate_cpu_spike(duration=30, cpu_percent=80)

0 comments on commit 841a416

Please sign in to comment.