Pages

Friday, November 4, 2011

Memory Regression Perl Scripts for Linux

Courtesy : thegeekstuff
During performance testing of your application you might want to perform some sort of memory regression testing. This article contains two memory regression scripts that are written in perl which will occupy a specific amount of memory for a certain amount of time, for your testing.

1. Basic Memory Regression Script

To execute this script, do the following:
$ perl memtest.pl 100
The above example will occupy 100 MB of memory, and waits for your input – a simple enter will terminate the script and releases the memory. This way you can let this script occupy the amount of memory you need for as much time as you need.
First argument is taken as the amount of memory to be occupied ( in MB ).
Note: Don’t execute this script on any critical system. Be careful while running this script. Don’t give a large memory value to this script. If the amount of memory given is huge or not available, your system might hang.
Do ‘vi memtest.pl’ and copy/paste the following perl code to create this file.
# store and validate the command line parameter
$mb = $ARGV[0];
unless ( defined $mb and $mb =~ /^\d+$/ and $mb >= 1)  {
    die "Usage: $0 \nEx: $0 100 - occupies 100 MB memory\n"
}
# convert it to bytes.
$b = $mb * 1024 * 1024;

# open in-memory file, and seek to size specified to get memory from OS.
open MEM, '>', \$memfile;
seek MEM, $b - 1, 0;
print MEM 'A';
close RAM;
printf "$mb MB memory is occupied, press ENTER to release: "; ;

# till here the memory is occupied by this program.
undef $ramfile;
printf "Memory released";

2. Advanced Memory Regression Script

To execute this script, do the following:
$ perl memtest-adv.pl 250

(or)

$ perl memtest-adv.pl 25%
This memtest-adv.pl works the same way as basic script, but you can also specify percentage as an input. When your system has total of 2GB of physical memory, you can specify 25%, which will occupy 500MB of memory for testing.
First argument can be either the amount of memory in MB or percentage of memory to be occupied, where percentage represents the percentage against total primary memory available.
Note: This Perl script occupies approximately the given amount of memory. On the system we tested, it took a maximum of 5MB more. So, do not use this on any critical system. Use this script only on a test system, as the system might hang.
Do ‘vi memtest-adv.pl’ and copy/paste the following perl code to create this file.
# calculate memory to be occupied from percentage given
sub find_memto_occupy
{
    $pc = $_[0];
    die "Wrong percentage given $pc\n" if ($pc > 100);

    open MEMINFO, '<', '/proc/meminfo' or die "Unable to open /proc/meminfo to find available memory\n";
    my $mem = ;

    if ( $mem =~ /^MemTotal:\s+(\d+)\s.*$/ )  {
        $mem = $1;
    } else {
        die "Unable to find the available memory\n";
    }

    $mem = ( $mem / 100 ) * $pc;
    return int($mem / 1024);
}

# main script
{
    $num = $ARGV[0];
    unless ( defined $num and $num =~ /^\d+%?$/ and $num >= 1)  {
             die "Usage: $0 \nEx: $0 100 - occupies 100 MB memory\n"
    }

    if ( $num =~ /^(\d+)%$/ )  {
        # convert percentage to bytes.
        $pc = $1;
        $mb = find_memto_occupy($pc);
    } else {
        $mb = $num;
    }

    $b = $mb * 1024 * 1024;
    open MEM, '>', \$memfile;
    seek MEM, $b - 1, 0;
    print MEM chr(0);
    close RAM;

    print "$mb MB memory is occupied, press ENTER to release: "; ;
    undef $ramfile;
    print "Memory released";
}