Skip to content

Instantly share code, notes, and snippets.

@QAInsights
Last active September 12, 2023 12:58
Show Gist options
  • Save QAInsights/6c86b458f4ab4c02fb482f3af041fe81 to your computer and use it in GitHub Desktop.
Save QAInsights/6c86b458f4ab4c02fb482f3af041fe81 to your computer and use it in GitHub Desktop.

Revisions

  1. QAInsights revised this gist Sep 12, 2023. No changes.
  2. QAInsights revised this gist Sep 12, 2023. 1 changed file with 32 additions and 28 deletions.
    60 changes: 32 additions & 28 deletions httpconnections_jmeter.groovy
    Original file line number Diff line number Diff line change
    @@ -1,35 +1,33 @@
    import org.apache.hc.core5.ssl.SSLContexts
    import org.apache.hc.core5.ssl.SSLContextBuilder
    import org.apache.hc.client5.HttpClients
    import org.apache.hc.core5.ssl.SSLConnectionSocketFactoryBuilder
    import org.apache.hc.core5.ssl.TrustSelfSignedStrategy
    import org.apache.hc.core5.ssl.PrivateKeyStrategy
    import org.apache.hc.core5.ssl.KeyStoreType
    import org.apache.hc.core5.ssl.SSLContext
    import org.apache.hc.core5.util.TimeValue
    import org.apache.hc.core5.pool.PoolStats
    import org.apache.hc.core5.ssl.SSLHost
    import org.apache.hc.core5.ssl.TLS
    import org.apache.hc.core5.ssl.PrivateKeyDetails
    import org.apache.hc.core5.ssl.KeyMaterial
    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(
    60, // Default connection time to live
    TimeValue.ofSeconds(60) // Validate after inactivity
    )

    connectionManager.setMaxTotal(maxTotalConnections)
    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
    }
    }

    // Create a custom keep-alive strategy
    def customKeepAliveStrategy = (response, context) -> 60 * 1000 // Set the keep-alive timeout to 60 seconds (in milliseconds)
    //log.info("Keep-Alive Timeout: " + customKeepAliveStrategy.getKeepAliveDuration(null, null) + " milliseconds")

    // Create an HttpClient instance

    // Create a HttpClient instance using the connection manager
    CloseableHttpClient httpClient = HttpClients.custom()
    .setConnectionManager(connectionManager)
    .setKeepAliveStrategy(customKeepAliveStrategy)
    @@ -40,19 +38,25 @@ 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("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())

    // Get connection pool stats
    def poolStats = connectionManager.getTotalStats()
    log.info("Active Connections: ${args[2]}" + poolStats.getLeased())
    log.info("Available Connections: ${args[2]} " + poolStats.getAvailable())

    response.close() // Close the response

  3. QAInsights revised this gist Sep 12, 2023. 1 changed file with 28 additions and 32 deletions.
    60 changes: 28 additions & 32 deletions httpconnections_jmeter.groovy
    Original file line number Diff line number Diff line change
    @@ -1,33 +1,35 @@
    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

    import org.apache.hc.core5.ssl.SSLContexts
    import org.apache.hc.core5.ssl.SSLContextBuilder
    import org.apache.hc.client5.HttpClients
    import org.apache.hc.core5.ssl.SSLConnectionSocketFactoryBuilder
    import org.apache.hc.core5.ssl.TrustSelfSignedStrategy
    import org.apache.hc.core5.ssl.PrivateKeyStrategy
    import org.apache.hc.core5.ssl.KeyStoreType
    import org.apache.hc.core5.ssl.SSLContext
    import org.apache.hc.core5.util.TimeValue
    import org.apache.hc.core5.pool.PoolStats
    import org.apache.hc.core5.ssl.SSLHost
    import org.apache.hc.core5.ssl.TLS
    import org.apache.hc.core5.ssl.PrivateKeyDetails
    import org.apache.hc.core5.ssl.KeyMaterial

    // 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}")
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(
    60, // Default connection time to live
    TimeValue.ofSeconds(60) // Validate after inactivity
    )

    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")
    connectionManager.setMaxTotal(maxTotalConnections)
    connectionManager.setDefaultMaxPerRoute(maxConnectionsPerRoute)

    // Create a custom keep-alive strategy
    def customKeepAliveStrategy = (response, context) -> 60 * 1000 // Set the keep-alive timeout to 60 seconds (in milliseconds)

    // Create a HttpClient instance using the connection manager
    // Create an HttpClient instance
    CloseableHttpClient httpClient = HttpClients.custom()
    .setConnectionManager(connectionManager)
    .setKeepAliveStrategy(customKeepAliveStrategy)
    @@ -38,25 +40,19 @@ 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())
    log.info("Response Content:\n${EntityUtils.toString(response.getEntity())}")

    // Get connection pool stats
    def poolStats = connectionManager.getTotalStats()
    log.info("Active Connections: ${args[2]}" + poolStats.getLeased())
    log.info("Available Connections: ${args[2]} " + poolStats.getAvailable())

    response.close() // Close the response

  4. QAInsights revised this gist Sep 12, 2023. 1 changed file with 14 additions and 4 deletions.
    18 changes: 14 additions & 4 deletions httpconnections_jmeter.groovy
    Original file line number Diff line number Diff line change
    @@ -5,9 +5,10 @@ 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 = 10
    def maxTotalConnections = 10
    def maxConnectionsPerRoute = 1
    def maxTotalConnections = 1

    // Create a connection manager with the specified number of connections
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager()
    @@ -22,6 +23,7 @@ def customKeepAliveStrategy = new DefaultConnectionKeepAliveStrategy() {
    return 60 * 1000
    }
    }

    //log.info("Keep-Alive Timeout: " + customKeepAliveStrategy.getKeepAliveDuration(null, null) + " milliseconds")


    @@ -38,7 +40,7 @@ vars.putObject("customHttpClient", httpClient)
    def customHttpClient = vars.getObject("customHttpClient") as CloseableHttpClient
    //log.info("${customHttpClient}")
    // Get the context's connection manager, which contains connection data
    def cm = customHttpClient.getConnectionManager()
    //def cm = customHttpClient.getConnectionManager()

    // Create an HTTP request
    def httpGet = new HttpGet(args[0] + "://" + args[1])
    @@ -49,4 +51,12 @@ 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("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

  5. QAInsights revised this gist Sep 10, 2023. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions httpconnections_jmeter.groovy
    Original file line number Diff line number Diff line change
    @@ -22,6 +22,8 @@ def customKeepAliveStrategy = new DefaultConnectionKeepAliveStrategy() {
    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()
  6. QAInsights revised this gist Sep 10, 2023. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion httpconnections_jmeter.groovy
    Original file line number Diff line number Diff line change
    @@ -15,10 +15,18 @@ 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
    }
    }

    // Create a HttpClient instance using the connection manager
    CloseableHttpClient httpClient = HttpClients.custom()
    .setConnectionManager(connectionManager)
    .setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy())
    .setKeepAliveStrategy(customKeepAliveStrategy)
    .build()

    // Set the HttpClient as a variable to be used in the sampler
  7. QAInsights created this gist Sep 10, 2023.
    42 changes: 42 additions & 0 deletions httpconnections_jmeter.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    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 = 10
    def maxTotalConnections = 10

    // Create a connection manager with the specified number of connections
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager()
    connectionManager.setDefaultMaxPerRoute(maxConnectionsPerRoute)
    connectionManager.setMaxTotal(maxTotalConnections)
    //log.info("${connectionManager}")

    // Create a HttpClient instance using the connection manager
    CloseableHttpClient httpClient = HttpClients.custom()
    .setConnectionManager(connectionManager)
    .setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy())
    .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())}")