Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to configure OkHttp Client to be able to send as many HTTP requests per second as possible.

I have the following configuration:

public static OkHttpClient getInstance() {
    if (instance == null) {
        synchronized (lock) {
            instance = new OkHttpClient.Builder()
                    .addInterceptor(createHttpBodyLoggingInterceptor())
                    .addInterceptor(createHttpBasicLoggingInterceptor())
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .dispatcher(createDispatcher())
                    .connectionPool(createConnectionPool())
                    .build();
        }
    }
    return instance;
}

private static Dispatcher createDispatcher() {
    final Dispatcher dispatcher = new Dispatcher(Executors.newCachedThreadPool());
    dispatcher.setMaxRequests(64);
    dispatcher.setMaxRequestsPerHost(64);
    return dispatcher;
}

private static ConnectionPool createConnectionPool() {
    return new ConnectionPool(64, 10_000, TimeUnit.MILLISECONDS);
}

I have simple performance/integration test, which executes multiple asynchronous requests to the server. One instance of the HTTP client is executing requests to the one instance of HTTP server on the same machine.

I know that value 64 is default value for maxRequests. I tried to increase this value as well as other values (e.g. maxRequestsPerHost or number of connections in ConnectionPool), but no matter what I set above these value (e.g. 100 or 1000), I'm able to process only around ~45 requests per second. Other requests are failing and I'm getting SocketTimeoutException. Do you know if it's possible to configure OkHttp to be able to process more requests than 45 per second? Or there are software or hardware limitations, which prevent that?

I'll appreciate any answers or ideas.

Regards, Piotr


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
2.4k views
Welcome To Ask or Share your Answers For Others

1 Answer

The fact you are getting AROUND 45 requests per second probably means that it`s a hardware limitation. Also, the AROUND is probably due to network connectivity or something of the same nature.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...