Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions docs/guide/protocols/http/keepAlive.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#### Keep alive

Jmeter-java-dsl maintains a persistent http connection for subsequent requests to the same server.
It is done by sending `Connection: keep-alive` header.
If you want to disable keep-alive logic and force a server to close connection after each request then use`.useKeepAlive(false)` in a given `httpSampler` or `httpDefaults`.
By default, **JMeter Java DSL** uses a persistent HTTP connection (sending the `Connection: keep-alive` header) to reuse the same socket for multiple requests to the same server.

To force server to close the connection after each request, you can disable keep-alive with `.useKeepAlive(false)` either on an individual `httpSampler` or globally on `httpDefaults`.

This can be useful when you’re load-testing an API that enforces a strict limit on the number of simultaneous sockets per client. If you leave keep-alive enabled, each thread will hold its socket open for the duration of the test, consuming the server's connection resources.

```java
import static us.abstracta.jmeter.javadsl.JmeterDsl.*;
Expand All @@ -14,10 +15,13 @@ public class PerformanceTest {
public void test1() throws Exception {
testPlan(
threadGroup(1, 10,
//Disable keep-alive for this sampler: sends Connection: close header and close the connection after this request
httpSampler("https://myservice1.com")
.useKeepAlive(false),
.useKeepAlive(false),
// Explicitly enable keep-alive for this sampler: sends Connection: keep-alive header and keeps the connection open
httpSampler("https://myservice2.com")
.useKeepAlive(true),
//No keep-alive configuration, so it will use the default keep-alive behavior (keep-alive enabled)
httpSampler("https://myservice3.com")
)
).run();
Expand All @@ -26,11 +30,16 @@ public class PerformanceTest {
@Test
public void test2() throws Exception {
testPlan(
//Disable keep-alive globally for all samplers in this test plan
httpDefaults()
.useKeepAlive(false),
threadGroup(1, 10,
// These samplers inherit keep-alive disabled from httpDefaults()
httpSampler("https://myservice1.com"),
httpSampler("https://myservice2.com")
httpSampler("https://myservice2.com"),
// Explicitly enable keep-alive for this sampler overriding the global setting
httpSampler("https://myservice3.com")
.useKeepAlive(true)
)
).run();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ public DslHttpDefaults followRedirects(boolean enable) {

/**
* Specifies if by default HTTP connection should be kept alive.
* <p>
* This can be overwritten by {@link DslHttpSampler#useKeepAlive(boolean)}.
*
* @param enable sets either to enable or disable persistent connection
* @return the config element for further configuration or usage.
Expand Down
Loading