Last active
September 12, 2023 12:58
-
-
Save QAInsights/6c86b458f4ab4c02fb482f3af041fe81 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.http.client.methods.HttpGet | |
import org.apache.http.impl.client.CloseableHttpClient | |
import org.apache.http.impl.client.HttpClients | |
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager | |
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy | |
import org.apache.http.util.EntityUtils | |
// Define the desired number of connections | |
def maxConnectionsPerRoute = 1 | |
def maxTotalConnections = 1 | |
// Create a connection manager with the specified number of connections | |
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager() | |
connectionManager.setDefaultMaxPerRoute(maxConnectionsPerRoute) | |
connectionManager.setMaxTotal(maxTotalConnections) | |
//log.info("${connectionManager}") | |
def customKeepAliveStrategy = new DefaultConnectionKeepAliveStrategy() { | |
@Override | |
public long getKeepAliveDuration(org.apache.http.HttpResponse response, org.apache.http.protocol.HttpContext context) { | |
// Set the keep-alive timeout to 60 seconds (in milliseconds) | |
return 60 * 1000 | |
} | |
} | |
//log.info("Keep-Alive Timeout: " + customKeepAliveStrategy.getKeepAliveDuration(null, null) + " milliseconds") | |
// Create a HttpClient instance using the connection manager | |
CloseableHttpClient httpClient = HttpClients.custom() | |
.setConnectionManager(connectionManager) | |
.setKeepAliveStrategy(customKeepAliveStrategy) | |
.build() | |
// Set the HttpClient as a variable to be used in the sampler | |
vars.putObject("customHttpClient", httpClient) | |
// Retrieve the custom HttpClient from the variable | |
def customHttpClient = vars.getObject("customHttpClient") as CloseableHttpClient | |
//log.info("${customHttpClient}") | |
// Get the context's connection manager, which contains connection data | |
//def cm = customHttpClient.getConnectionManager() | |
// Create an HTTP request | |
def httpGet = new HttpGet(args[0] + "://" + args[1]) | |
// Execute the request using the custom HttpClient | |
def response = customHttpClient.execute(httpGet) | |
// Get the connection number for the executed request | |
// Print the HTTP response content | |
//log.info("Response Content:\n${EntityUtils.toString(response.getEntity())}") | |
log.info("Active Connections: ${args[2]}" + connectionManager.getTotalStats().getLeased()) | |
log.info("Available Connections: ${args[2]} " + connectionManager.getTotalStats().getAvailable()) | |
response.close() // Close the response | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment