Skip to content

Testing

The test pyramid, load and stress testing with Apache Bench and JMeter, the C10K problem and code coverage gates with JaCoCo.

5 min readUpdated

Automated tests come in layers, from many fast unit tests to a few slow system tests. This note summarises the test pyramid, explains the difference between load and stress tests, shows how to run a quick load test with Apache Bench (ab) and read its output, and sets up code coverage with a minimum threshold using JaCoCo in Maven.

Follow the test pyramid

The automated testing pyramid recommends many tests at the bottom, where they are cheap and fast, and fewer towards the top, where they are slow and costly to maintain. In the diagram below, the technology-related layers (unit, component, integration, system and acceptance testing) sit under the business-related ones (alpha and beta testing); time, expense and coverage grow towards the top.

Automated testing pyramid: unit, component, integration, system and acceptance testing as technology-related layers, alpha and beta testing as business-related layers at the top

In a typical layered Java application, the layers map like this:

  • System level: regression, system, stress and performance tests.
  • Controller level: integration tests.
  • Service level: unit tests.

Load and stress tests

A load test measures how a system performs under an expected load. It checks that a website or web application can handle a given number of simultaneous users, and is often part of capacity planning: making sure the system can grow to a specified level of traffic.

A stress test overloads a system to find its breaking point. It pushes the system beyond its intended capacity to find the components that slow down first, the bottlenecks, and the possible points of failure.

Choose a tool

  • Apache JMeter: load tests functional behaviour and measures performance. One JMeter instance handles a limited number of virtual users (around 800 was a common rule of thumb; the real limit depends on the machine and the test plan).
  • BlazeMeter: a commercial continuous testing platform. It runs JMeter scripts on many distributed JMeter instances, which makes it suitable for stress tests.
  • Loader.io: a hosted load testing service with a free plan, for testing web applications and APIs with thousands of concurrent connections.
  • ab: the Apache HTTP server benchmarking tool, for quick tests from the command line.

Run a load test with ab

ab is preinstalled on macOS. On Debian and Ubuntu it is part of the apache2-utils package.

This sends 100 requests to the URL with a concurrency of 10, which is roughly 10 users making requests at the same time. -k keeps connections alive between requests:

Terminal window
ab -k -n 100 -c 10 [url]

To test an API that needs authentication, add a header with -H. This sends 1000 requests from 10 concurrent clients:

Terminal window
ab -k -n 1000 -c 10 -H "Authorization: Bearer <token>" http://api_url/

If ab reports a large number of failed requests, the page is probably dynamic. ab counts a response as failed when its length differs from the first response, and dynamic pages return a different Content-Length each time. Use -l to accept variable document lengths:

-l Accept variable document length (use this for dynamic pages)

What the numbers usually show

Measurements like these typically give 4000 to 7000 transactions per second for a single application service. In practice the application tier is rarely what limits scalability; the database usually is. If the database is not scaled, starting more application servers only sends more load to the same backend, which tends to make things worse.

The C10K problem

The C10K problem is the challenge of serving ten thousand concurrent clients from one server. A simple test with ab sends 10,000 HTTPS requests with a concurrency of 16:

Terminal window
ab -n 10000 -c 16 -l https://domain.com:443/index.html

Example output:

Concurrency Level: 16
Time taken for tests: 403.432 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 131810000 bytes
HTML transferred: 129120000 bytes
Requests per second: 24.79 [#/sec] (mean)
Time per request: 645.492 [ms] (mean)
Time per request: 40.343 [ms] (mean, across all concurrent requests)
Transfer rate: 319.06 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 436 487 66.4 477 1854
Processing: 137 157 65.0 149 2022
Waiting: 136 150 34.4 146 1168
Total: 576 644 106.7 627 2822
Percentage of the requests served within a certain time (ms)
50% 627
66% 641
75% 652
80% 656
90% 669
95% 716
98% 841
99% 1012
100% 2822 (longest request)

In this run, connecting took about three quarters of each request (487 ms of 644 ms on average), because without -k every request opens a new TLS connection. The server itself spent about 150 ms per request. The percentile table shows that half of the requests finished within 627 ms and the slowest took 2.8 seconds.

Measure code coverage with JaCoCo

JaCoCo is a code coverage library for Java. Its Maven plugin attaches an agent to the test run and writes a report.

The basic setup: prepare-agent sets up the agent before the tests run, and report writes the HTML report to target/site/jacoco in the prepare-package phase:

pom.xml
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.16</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>

Version 0.8.16 was current in November 2026; use the latest release.

Fail the build below a coverage threshold

The check goal fails the build when coverage is below a limit. This configuration requires at least 50 % line coverage for every class except test classes. The limit can be written as a ratio (0.50) or a percentage (50%):

pom.xml
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.16</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>default-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<!-- requires a line coverage minimum of 50% for every class except test classes -->
<element>CLASS</element>
<excludes>
<exclude>*Test</exclude>
</excludes>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>50%</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>

The check goal runs in the verify phase by default, so mvn verify enforces the threshold.

References

Related notes

3 min read

SonarQube

Run SonarQube locally in Docker and analyse a Maven project with the SonarScanner for Maven.

  • Observability & Testing
  • Developer Tools
1 min read

APM

Application performance monitoring for Java servers: install the Glowroot agent on GlassFish, Payara or a Spring Boot application.

  • Observability & Testing
3 min read

ELK Stack

Run Elasticsearch, Logstash and Kibana in Docker, create a Kibana data view and ship Java logs to Logstash with Logback.

  • Observability & Testing

Type to search the whole site.