Skip to content

Commit 5895862

Browse files
Update keep-alive doc
1 parent 858329e commit 5895862

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

docs/guide/protocols/http/keepAlive.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#### Keep alive
22

3-
Jmeter-java-dsl maintains a persistent http connection for subsequent requests to the same server.
4-
It is done by sending `Connection: keep-alive` header.
5-
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`.
3+
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.
64

5+
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`.
6+
7+
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 virtual user will hold its socket open for the duration of the test, consuming the gateway’s connection pool.
78

89
```java
910
import static us.abstracta.jmeter.javadsl.JmeterDsl.*;
@@ -14,10 +15,13 @@ public class PerformanceTest {
1415
public void test1() throws Exception {
1516
testPlan(
1617
threadGroup(1, 10,
18+
//Disable keep-alive for this sampler: sends Connection: close header and close the connection after this request
1719
httpSampler("https://myservice1.com")
18-
.useKeepAlive(false),
20+
.useKeepAlive(false),
21+
// Explicitly enable keep-alive for this sampler: sends Connection: keep-alive header and keeps the connection open
1922
httpSampler("https://myservice2.com")
2023
.useKeepAlive(true),
24+
//No keep-alive configuration, so it will use the default keep-alive behavior (keep-alive enabled)
2125
httpSampler("https://myservice3.com")
2226
)
2327
).run();
@@ -26,11 +30,16 @@ public class PerformanceTest {
2630
@Test
2731
public void test2() throws Exception {
2832
testPlan(
33+
//Disable keep-alive globally for all samplers in this test plan
2934
httpDefaults()
3035
.useKeepAlive(false),
3136
threadGroup(1, 10,
37+
// These samplers inherit keep-alive disabled from httpDefaults()
3238
httpSampler("https://myservice1.com"),
33-
httpSampler("https://myservice2.com")
39+
httpSampler("https://myservice2.com"),
40+
// Explicitly enable keep-alive for this sampler overriding the global setting
41+
httpSampler("https://myservice3.com")
42+
.useKeepAlive(true)
3443
)
3544
).run();
3645
}

jmeter-java-dsl/src/main/java/us/abstracta/jmeter/javadsl/http/DslHttpDefaults.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ public DslHttpDefaults followRedirects(boolean enable) {
192192

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

0 commit comments

Comments
 (0)