Skip to main content

Memory Regression Perl Scripts for Linux

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 <occupy MB>\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 MEM;
printf "$mb MB memory is occupied, press ENTER to release: "; <STDIN>;

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

2. Advanced Memory Regression Script

$ 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 = <MEMINFO>;

    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 <occupy MB>\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 MEM;

    print "$mb MB memory is occupied, press ENTER to release: "; <STDIN>;
    undef $memfile;
    print "Memory released";

Comments

Popular posts from this blog

20 perl programming tips

1. List all Installed Perl Modules from Unix Command Line Get a list of all installed perl modules as shown below. $ perl -MFile::Find=find -MFile::Spec::Functions -Tlw -e 'find { wanted => sub { print canonpath $_ if /\.pm\z/ }, no_chdir => 1 }, @INC' /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/HTML/Filter.pm /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/HTML/LinkExtor.pm /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/HTML/PullParser.pm /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/HTML/Parser.pm /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/HTML/TokeParser.pm ..... In the above example, File::Find  and  File::Spec::Functions  module are used to list all installed modules. -M option  loads the module. It executes  use module  before executing the script -T option  enables taint checking, which instructs perl to keep track of data from the user and avoid doing anything insecure w...

Perl and Excel: How to Read, Write, Parse Excel Files using Perl

If you want to manipulate excel files programmatically, you can use Perl Spreadsheet module, which provides an object interface that makes it easier to create and parse Excel files. Install Spreadsheet WriteExcel Module Method 1: Standard install using make Download the zipped tar file of  Spreadsheet-ParseExcel  and  Spreadsheet-WriteExcel  from cpan. Untar and unzip the module as follows: tar -zxvf Spreadsheet-WriteExcel.tar.gz cd to the directory the tar creates. Execute the steps below to to install the Spreadsheet-WriteExcel module. perl Makefile.PL make make test make install Use the above procedure to install Spreadsheet-ParseExcel also. Method 2: CPAN.pm install If you have CPAN.pm configured you can install the module as follows: perl -MCPAN -e 'install "Spreadsheet::WriteExcel"' perl -MCPAN -e 'install "Spreadsheet::ParseExcel"' For additional installation help refer to: How To Install Perl Modules Manually and Using ...

How to Connect to MySQL from Perl

How do I connect to a MySQL database from a perl program? use perl DBI module to connect to a MySQL database as explained below. If you don’t have perl DBI and DBD::mysql module installed, install perl module using cpan as we discussed earlier. # perl -MCPAN -e shell cpan> install DBI cpan> install DBD::mysql On a very high level, you’ll have to do the following three steps to connect to a MySQL database and get data. 1. Connect to the MySQL Database In the DBI module, you’ll use the connect function as shown below. $dbc = DBI->connect($source, $username, $password) DBI->connect function takes the following three arguments: $source – This is in the format of “DBI:mysql:[database]:[hostname]”. Replace the [database] and [hostname] with values from your system. In the example shown below, it is connecting to the database called “mycompany” that is running on the localhost. $username – The username that is used to connect to the MySQL database. $pass...