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 or URL you want to load, the number of virtual users and the test duration

  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 +x 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
  5. Start your load test from LoadFocus.com

  6. Watch the response time and throughput charts on the test results page while the test runs

  7. After the test ends, get the result from the stats script on your server (in our example log.txt)

  8. To chart the result file, run the script with -c true so it writes CSV, then open the file in a spreadsheet or any charting tool and plot CPU and memory against time

  9. Now, by putting the LoadFocus charts next to the results from cpu_mem_stats.sh, you can correlate how your server behaves under load, which tells you whether the machine needs to be upgraded or is enough to support the load your clients will generate.

What CPU utilization means in a load test

CPU utilization is the percentage of time the processor spends executing work rather than sitting idle. It is one of four resources worth watching on the server during a performance test, together with memory, disk and network.

Read it as server-wide load, not as the cost of one request. As you add virtual users, utilization rises with throughput. If it climbs to 80 to 100 percent while throughput stops growing, the CPU is the bottleneck. If response times rise while CPU stays low, the server is waiting on something else, usually a database, a lock or a downstream call.

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.