Skip to main content

How to Measure CPU and Memory while Running a Load Test

How to Measure CPU and Memory while Running a Load Test

Key takeaways

  • Response times alone cannot tell you whether the server is saturated or merely waiting.
  • Watch the load generator too, since a saturated generator caps the test rather than the target.
  • Correlate resource use with the load curve; the point where they diverge is the finding.

In order to make sure that your application will not break under a heavy load, a web application owner has to perform some basic performance tests.

Here are 4 simple steps how to achieve this with LoadFocus load testing tool:

  1. Log into your account on the LoadFocus platform

  2. Click on the “New Load Test” menu

  3. Enter the details of your web application (the endpoint/URL that you want to access, the number of clients per second and the number of seconds that the test will run) – check image attached for an example

  4. Before starting the test log into your server and use the script that we provide for monitoring the CPU and memory as below (the script is developed for Linux servers):

    1. copy the script cpu_mem_stats.sh to the server where your application is installed: click here to download script.

    2. run the following command:

      
      chmod 777 cpu_mem_stats.sh
    3. run the following command:

      
      ./cpu_mem_stats.sh -p node -t 200 -f /home/ubuntu/log.txt -c true
    4. the meaning of the parameters is (please modify according to your needs):

    • -p – The process name to look for
    • -t – The time to monitor specified in seconds
    • -f – path to the file to write the output (if not specified the output will be logged into the console)
    • -c – if specified as true will output in CSV format
    1. Start your load test from LoadFocus.com
    2. The result of the response times will give you a graph like this:
    3. After the test ends get the result from the stats test on your server (in our example log.txt)
    4. For creating the graphs from the result files we recommend the following online solution: https://plot.ly/ . From that you will get a graph similar to:
    5. Now by using the graphs from LoadFocus and the results from the cpu_mem_stats.sh you can correlate how you server is behaving under load, which will give you an idea if the machine needs to be upgraded or is enough to support the load your clients will generate.

Frequently Asked Questions

Why measure CPU and memory if I already have response times?

Response times tell you the system is slow but not why. A server at 30 percent CPU with rising latency is waiting on something else, usually a database or a downstream call, and that is a completely different fix from a saturated server.

What should I watch on the machine generating the load?

The same metrics. A load generator that runs out of CPU or file descriptors quietly caps the traffic it produces, so the test flatlines and the target looks healthier than it is.

What am I looking for in the numbers?

The point where resource use and the load curve stop moving together. While they climb in step the system is coping; the moment they diverge you have found the bottleneck worth investigating.

Chris

Head of Content at LoadFocus

Chris leads content and oversees development across the services this blog covers. The guides and tool comparisons here come out of the same decisions that shape what ships.

Weekly tips right in your inbox.

Don’t miss out on the latest testing tips and tricks, tools and news related to software testing.

Already +20,000 members in the list.

The Load Testing Platform with AI That Explains Your Results

Load test websites and APIs from 25 cloud regions, monitor page speed and uptime, and get AI analysis that explains your results in plain English.

Cloud load testing, website speed and API monitoring in one place.

Hi there! Can I share a cool thing I'm working on with you?

A software testing newsletter that sparks joy.

Our goal is to create helpful content for software developers and testers.

If you're not into it, you can unsubscribe instantly.