diff --git a/docs/guide/protocols/http/keepAlive.md b/docs/guide/protocols/http/keepAlive.md index e1e7c276..d0fe6595 100644 --- a/docs/guide/protocols/http/keepAlive.md +++ b/docs/guide/protocols/http/keepAlive.md @@ -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.*; @@ -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(); @@ -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(); } diff --git a/jmeter-java-dsl/src/main/java/us/abstracta/jmeter/javadsl/http/DslHttpDefaults.java b/jmeter-java-dsl/src/main/java/us/abstracta/jmeter/javadsl/http/DslHttpDefaults.java index 1b6dce4e..49d85825 100644 --- a/jmeter-java-dsl/src/main/java/us/abstracta/jmeter/javadsl/http/DslHttpDefaults.java +++ b/jmeter-java-dsl/src/main/java/us/abstracta/jmeter/javadsl/http/DslHttpDefaults.java @@ -192,6 +192,8 @@ public DslHttpDefaults followRedirects(boolean enable) { /** * Specifies if by default HTTP connection should be kept alive. + *

+ * 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.