HttpAsyncClient每秒发送的请求不能超过10个

问题描述 投票:0回答:1

为什么Apache HttpAsyncClient每秒仅发送10个请求。我配置有问题吗?这是我启动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();

而且,这是我的使用方式:

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

如何每秒获得更多请求?

java apache apache-httpasyncclient
1个回答
0
投票

Apache HttpClient的所有版本都可以轻松地每秒生成数万个请求。

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

性能问题可能与服务器代码或您的应用程序代码有关。

  1. 请勿这样做。太厉害了一个人使用的I / O调度线程不应超过系统上的CPU内核。除非有充分的理由,否则不要覆盖默认值。

    .setIoThreadCount(10000) // do not do this
    
  2. 如果手动设置连接管理器,则所有连接管理参数均无效。代码中的maxConnsPerRoutemaxConnections值均无效。您需要将它们直接应用到连接管理器。

    .setConnectionManager( connManager ) // if you do this
    .setMaxConnPerRoute( maxConnsPerRoute ) // this has no effect
    .setMaxConnTotal( maxConnections )
    
© www.soinside.com 2019 - 2024. All rights reserved.