Java和Clojure开源的 HTTP 客户端

jopen 9年前

  • JDK’s URLConnection uses traditional thread-blocking I/O.
  • Apache HTTP Client uses traditional thread-blocking I/O with thread-pools.
  • Apache Async HTTP Client uses NIO.
  • Jersey is a ReST client/server framework; the client API can use several HTTP client backends including URLConnection and Apache HTTP Client.
  • OkHttp uses traditional thread-blocking I/O with thread-pools.
  • Retrofit turns your HTTP API into a Java interface and can use several HTTP client backends including Apache HTTP Client.
  • Grizzly is network framework with low-level HTTP support; it was using NIO but it switched to AIO .
  • Netty is a network framework with HTTP support (low-level), multi-transport, includes NIO and native (the latter uses epoll on Linux).
  • Jetty Async HTTP Client uses NIO.
  • Async HTTP Client wraps either Netty, Grizzly or JDK’s HTTP support.
  • clj-http wraps the Apache HTTP Client.
  • http-kit is an async subset of clj-http implemented partially in Java directly on top of NIO.
  • http async client wraps the Async HTTP Client for Java.

以下是负载测试的结果:

HTTP Client Load Test (colored is best) Regular (thread-blocking) Apache (4.4.1) Comsat (fiber-blocking) Apache (async 4.1) Regular (thread-blocking) OkHttp 2.4.0 Comsat (fiber-blocking) OkHttp 2.4.0 Regular (thread-blocking) Jersey 2.19 w/JDK connector Comsat (fiber-blocking) Jersey 2.19 w/JDK connector
AHC blocking (BIO) AHC async (NIO) + Quasar fibers OkHttp blocking (BIO) OkHttp async (BIO) + Quasar fibers Jersey blocking Jersey async + Quasar fibers
Long-lived concurrent 41k (maximum rate possible) Max 16715 41k 16358 16608 16713 16713
Error OOM - thread - OOM - thread OOM - thread OOM - thread OOM - thread
Time (s) 8.8 8.8 16.6 16.7 16.5 20.2
Heap max (MiB) N/A 702 N/A N/A N/A N/A
Heap avg (MiB) 246
Threads max 16
Throughput with target rate 1k (response after 1s) Time max (ms) 7139 1138 10209 1301 6341 4370
Time avg (ms) 2359 1002 3031 1008 1902 1477
Heap max (MiB) 227 110 125 119 330 342
Heap avg (MiB) 61 29.8 34.9 30 76.8 73.7
Threads max 4000+ 15 4000+ 1900+ 4300+ 2600+
Throughput with target rate 10k (response after 100ms) Time max (ms) 4898 4085 7939 7079 45198 14512

Time avg (ms) 2479 2717 3423 2125 25885 7594

Heap max (MiB) 338 192 179 165 495 489

Heap avg (MiB) 91.3 67.2 40.9 38.5 147 155

Threads max 7500+ 16 4900+ 3900+ 11000+ 6900+
Throughput with target rate 100k (immediate response) Time max (ms) 6937 3590 4668 1793 9303 9840

Time avg (ms) 1468 1821 1287 826 1659 3442

Heap max (MiB) 226 188 130 113 354 398

Heap avg (MiB) 62.2 66 36.1 33.2 79.5 122

Threads max 3500+ 16 2600+ 2000+ 4000+ 4000+
Notes



OkHttp doesn’t use NIO but regular blocking I/O under the hood.
Jersey uses one thread per connection even in the async case
详见:https://dzone.com/articles/high-concurrency-http-clients-on-the-jvm