#!/usr/bin/perl # alphaload - authored by pal@ekran.no, Thu Jun 26 10:05:47 CEST 2014 # ------------------------------------------------------------------- # runs calculations (simple math) on X cores for Y seconds # example: ./alphaload.pl 4 10 # which would run on 4 cores for 10 seconds # ## note: program does not care if you overtax the CPU (run on more than available cores) # my $cores=$ARGV[0] || die ("usage: alphaload \n"); my $runs=$ARGV[1] || die ("usage: alphaload \n"); my @pids; for ($ccnt=0; $ccnt<$cores; $ccnt++) { $pid=fork(); if ($pid) { ($itter,$secs)=it($runs); ($mitter)=sprintf ("%2.2f",(($itter/1000)/1000)); ($itsec)=($itter/$secs); ($mitsec)=(($itsec/1000)/1000); $itsec=int($itsec); $mitsec=sprintf ("%2.2f",$mitsec); print "Thread ".$ccnt." ran for ".$secs." seconds doing ".$itter." (".$mitter."M) iterations, which is ".$itsec." (".$mitsec."M) I/sec.\n"; exit(); } } sleep ($runs+2); # this works as long as you don't spawn more than you have cores available sub it() { my ($lruns)=@_; my $t1=time(); my $s=0; $cnt=0; $x=0; while ($s < $lruns) { $cnt++; # do some CPU calculation $x=sqrt($cnt*3.14159); $t2=time(); $s=$t2-$t1; } return ($cnt,$s); }