Thursday, October 30, 2014

Stress Command

Hello all,
Today, I am going to talk about Stress command. It's a very useful and easy command if you want to impose load and stress on your systems/servers and then review the result of running your application or whatever it is under pressure. For example, if you want to review your application's performance or your website's performance under a busy server, you can use this command; let's say you know your web server is working fine with 100 customers connected at the same time to your web server in a regular business day, which is usually the case in your company, and it uses 30% of your server's cpu roughly, but how is your web server's performance working when your company has a good sale or it's close to the end of year, boxing day for instance? And you know that your connected customers to your web server would increase by 50% at that moment. It's too late if you find out that your application is poor in memory managment or has a memory leak or whatever reason on that day and your website goes down under heavy loads and whatelse, your company will lose money. So, you can predict/pervent that situation by increasing cpu and memory using this command and simulate the exact situation and then figure out the problem in your application or servers.

Here is how it works:
First you need to install stress command: 

yum install stress

By the way, you can stress your system by cpu, memory, io, hdd, or a combination of them. You can get a list of options and parameters in manpage as well.

Example 1:
Bring the system load average up to an arbitrary value for cpu. In this example, it forks 4 processes with calculating the sqrt() of a random number acquired with rand() function.

stress -c 4
or
strss --cpu 4  --timeout 15

"--timeout 15" means timeout after 15 seconds


                                                     Figure 1 (before running command)


                                                        Figure 2 (after running command)

Example 2:
To see how your system performs when it is I/O bound, use the -i switch.

stress -i 4
or
stress --io 4

This will call sync() which is a system call that flushes memory buffers to disk.
Of course, you can combine above commands like:

stress -c 4 -i 4 --verbose --timeout 15
 

Example 3:
Now, let's try memory stress. The following command forks 7 processes each spinning on malloc():

stress -m 7 --timeout 15
or
stress --vm 7 --timeout 15

                                                   Figure 3 (before running command)

                                                      Figure 4 (after running command)

According to manpage, we can use --vm-hang option which instructs each vm hog process to go to sleep after allocating memory. This contrasts with their normal behavior, which is to free the memory and reallocate it.  This is useful for simulating low memory conditions on a machine.  For example, the following command allocates 512M (2 x 256M) of RAM and holds it until killed (after 10 seconds). 

stress --vm 2 --vm-bytes 128M --vm-hang 10
 



                                                                       Figure 5


And here is what manpage says about stress command:
'stress' is not a benchmark, but is rather a tool designed to put given subsytems under a specified load. Instances in which this is useful include those in which a system administrator wishes to perform tuning activities, a kernel or libc programmer wishes to evaluate denial of service possibilities, etc.

And pretty much that's it. I hope you enjoyed. Don't forget to put your comments here.
Thanks all,
Khosro Taraghi