0

why Apache HttpAsyncClient just send 10 request per second. Am i config something wrong? it my way to start an asyncClient:

PoolingNHttpClientConnectionManager connManager = null;
    try {
        if (client != null && client.isRunning()) {
            client.close();
        }

        TrustStrategy acceptingTrustStrategy = (certificate, authType) -> true;
        SSLContext sslContext = SSLContexts.custom()
                .loadTrustMaterial(null, acceptingTrustStrategy).build();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
                SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        Registry<ConnectionSocketFactory> socketFactoryRegistry =
                RegistryBuilder.<ConnectionSocketFactory> create().register("https", sslsf).build();

        Registry<SchemeIOSessionStrategy> socketRegistry = RegistryBuilder.<SchemeIOSessionStrategy>create()
                .register("http", NoopIOSessionStrategy.INSTANCE)
                .register("https", new SSLIOSessionStrategy(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER))
                .build();

        connManager = new PoolingNHttpClientConnectionManager(
                new DefaultConnectingIOReactor( IOReactorConfig.custom()
                        .setConnectTimeout( connectionTimeout * 1000 ) //connectTimeout
                        .setSoTimeout( readTimeout * 1000 ) //readTimeout
                        .setIoThreadCount(10000)
                        .build() ), socketRegistry );
    } catch (Exception e) {}

    client = HttpAsyncClients.custom()
            .setMaxConnPerRoute( maxConnsPerRoute )
            .setConnectionManager( connManager )
            .setMaxConnTotal( maxConnections )
            .setKeepAliveStrategy( DefaultConnectionKeepAliveStrategy.INSTANCE )
            .build();

    client.start();

and, it's my way to use it:

for(int i = 0; i < 100; i++) {
     client.execute(request.getHttpPost(), null);
}

How can I achieve more request per second???

2 Answers2

2

All versions of Apache HttpClient can easily generate tens thousand of requests per second.

https://github.com/ok2c/httpclient-benchmark/wiki

The performance issue likely lies either with the server code or with your application code.

  1. Do not do this. This is awful. One should not be using more I/O dispatch threads than there are CPU cores on the system. Do not override the default value unless you have a very strong reason to do so.

    .setIoThreadCount(10000) // do not do this
    
  2. If one manually sets a connection manager all connection management parameters have no effect. Both maxConnsPerRoute and maxConnections values in your code have no effect. You need to apply them directly to the connection manager.

    .setConnectionManager( connManager ) // if you do this
    .setMaxConnPerRoute( maxConnsPerRoute ) // this has no effect
    .setMaxConnTotal( maxConnections )
    
ok2c
  • 26,450
  • 5
  • 63
  • 71
  • Thank you for your response. just one question: in topic 'https://stackoverflow.com/questions/34505271/how-does-httpasyncclient-4-work', they said that: By default HttpAsyncClient permits only two concurrent connections to the same host per RFC 2616 specification. Is it correct? because i send all request to the same host. – hossein sargazi moghaddam Dec 09 '19 at 08:33
  • That is correct. HttpAsyncClient 4.1.x uses only two concurrent connections by default. If you like to increase that number you can do it through `ConnPoolControl` interface implemented by the connection manager. – ok2c Dec 09 '19 at 08:37
  • Thank you so much for your help. base on your suggestion, i can send more request per specific route and it's solved my problem. – hossein sargazi moghaddam Dec 10 '19 at 07:58
1

A lot of thanks to Okta, based on his guidance, I add this codes to my project, and the problem solved:

connManager.setMaxTotal(10);
connManager.setDefaultMaxPerRoute(10);
HttpHost host = new HttpHost("75.10.91.11", 8443, "https");
connManager.setMaxPerRoute(new HttpRoute(host), 10);

You can find fully explanation in baeldung.com