Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

java - Max possible requests per second performed with OkHttp client

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...