If you need to put load on an Azure VM for testing (like checking if your Azure Monitor Alerts or Autoscale settings are working), using a Perl script is a classic, lightweight way to do it.
Below are three scripts tailored for different types of “stress”: CPU, Memory, and I/O.
1. CPU Load Script
This script performs high-intensity mathematical calculations in a loop to pin the processor.
Perl
#!/usr/bin/perl# CPU Stress Testuse strict;use warnings;print "Starting CPU Load... Press Ctrl+C to stop.\n";# This will run on one core. # To stress multiple cores, run this script multiple times in the background.while (1) { my $x = sqrt(rand(1000000)) * sin(rand(1000000));}
- Pro Tip: If your VM has 4 cores and you want to hit 100% total CPU, run this 4 times:
perl cpu_load.pl & perl cpu_load.pl & perl cpu_load.pl & perl cpu_load.pl &
2. Memory (RAM) Load Script
This script creates a massive string and keeps adding to it to consume available RAM. Warning: Be careful with this; if it consumes all RAM, the Linux OOM (Out of Memory) killer might crash the VM.
Perl
#!/usr/bin/perl# Memory Stress Testuse strict;use warnings;print "How many MB of RAM should I consume? ";my $mb_to_hit = <STDIN>;chomp($mb_to_hit);my $data = "";my $chunk = "A" x (1024 * 1024); # 1MB string chunkprint "Allocating memory...\n";for (1..$mb_to_hit) { $data .= $chunk; print "Currently holding approx $_ MB\n" if $_ % 100 == 0;}print "Memory allocated. Press Enter to release memory and exit.";<STDIN>;
3. I/O (Disk) Load Script
This script continuously writes and deletes a file to stress the Virtual Machine’s disk IOPS (Input/Output Operations Per Second).
Perl
#!/usr/bin/perl# Disk I/O Stress Testuse strict;use warnings;my $filename = "test_load_file.tmp";print "Starting Disk I/O load... Press Ctrl+C to stop.\n";while (1) { open(my $fh, '>', $filename) or die "Could not open file: $!"; print $fh "This is a stress test line\n" x 10000; close $fh; unlink($filename); # Deletes the file immediately to repeat the write}
💡 The “Cloud Native” Alternative: stress-ng
While Perl scripts are great, most Azure Engineers use a tool called stress-ng. It is purpose-built for this and gives you much more granular control over exactly how many cores or how much RAM you hit.
To install and run (Ubuntu/Debian):
Bash
sudo apt update && sudo apt install stress-ng -y# Stress 2 CPUs for 60 secondsstress-ng --cpu 2 --timeout 60s# Stress 1GB of RAMstress-ng --vm 1 --vm-bytes 1G --timeout 60s
🛑 Important Reminder
When putting load on a VM, keep a separate window open with the command top or htop (if installed) to monitor the resource usage in real-time. If you are testing Azure Autoscale, remember that it usually takes 5–10 minutes for the Azure portal to reflect the spike and trigger the scaling action!