Friday, July 3, 2009

How to create high CPU load in perl

Sometimes you need to test how a host behaves when it's bogged down.

This code is fairly safe. Change $procs to a big number if you want to see it kill your host.


#!perl
use forks;

my $procs = 2;
for (1..$procs) {
threads->new(\&fpu_load);
threads->new(\&cpu_load);
}
sleep 100;

sub fpu_load {
my ($f1, $f2, $sum);
while(1) {
$f1 = rand(1000);
$f2 = rand(1000);
$sum = $f1 * $f2;
}
}

sub cpu_load {
my ($f1, $f2, $sum);
while(1) {
$f1 = rand(1000);
$f2 = rand(1000);
$sum = $f1 * $f2;
}
}

No comments:

Post a Comment