java httpclient 4.x performance guide to resolve issue -
there nice article http://hc.apache.org/httpclient-3.x/performance.html related http performance, pooling, e.t.c. can't find same latest 4.x version. did see it? met perf issues under heavy load , resolve them. i'm using 4.1 version. here profiler output:
26% org.apache.http.impl.client.closeablehttpclient.execute(multiple parameter matches) :26,107,40 26% org.apache.http.impl.client.closeablehttpclient.execute(org.apache.http.client.methods.httpurirequest, org.apache.http.protocol.httpcontext) :82,46 26% org.apache.http.impl.client.abstracthttpclient.doexecute(org.apache.http.httphost, org.apache.http.httprequest, org.apache.http.protocol.httpcontext) :882,818 26% org.apache.http.impl.client.abstracthttpclient.createhttpcontext() :301 26% org.apache.http.impl.client.abstracthttpclient.getconnectionmanager() :484 26% org.apache.http.impl.client.abstracthttpclient.createclientconnectionmanager() :321 26% org.apache.http.impl.conn.schemeregistryfactory.createdefault() :52 26% org.apache.http.conn.ssl.sslsocketfactory.getsocketfactory() :168 26% org.apache.http.conn.ssl.sslcontexts.createdefault() :58 26% javax.net.ssl.sslcontext.init(javax.net.ssl.keymanager[], javax.net.ssl.trustmanager[], java.security.securerandom) :283 26% sun.security.ssl.sslcontextimpl.engineinit(javax.net.ssl.keymanager[], javax.net.ssl.trustmanager[], java.security.securerandom) :83,92 26% javax.net.ssl.trustmanagerfactory.init(java.security.keystore) :250 26% sun.security.ssl.trustmanagerfactoryimpl.engineinit(java.security.keystore) :51 26% sun.security.ssl.trustmanagerfactoryimpl.getcacertskeystore(java.lang.string) :221 26% java.security.keystore.load(java.io.inputstream, char[]) :1214 26% sun.security.provider.javakeystore$jks.engineload(java.io.inputstream, char[]) :55 26% sun.security.provider.javakeystore.engineload(java.io.inputstream, char[]) :723,747 26% java.security.cert.certificatefactory.generatecertificate(java.io.inputstream) :339 26% sun.security.provider.x509factory.enginegeneratecertificate(java.io.inputstream) :93 26% sun.security.provider.x509factory.getfromcache(sun.security.util.cache, byte[]) :203
i have 4 methods sending data via http using httpclient , each of these methods consume 25% of total time. rest processing takes millis. looks i'm using httpclient in wrong way.
edit: see oleg answers + read https://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html answers realated questions
main parts are: nice way build pooling manager
poolinghttpclientconnectionmanager cm = new poolinghttpclientconnectionmanager(); // increase max total connection 200 cm.setmaxtotal(200); // increase default max connection per route 20 cm.setdefaultmaxperroute(20); // increase max connections localhost:80 50 httphost localhost = new httphost("locahost", 80); cm.setmaxperroute(new httproute(localhost), 50); closeablehttpclient httpclient = httpclients.custom() .setconnectionmanager(cm) .build();
a way use httpclient concurrently
//while httpclient instances thread safe , can shared //between multiple threads of execution, highly recommended //each thread maintains own dedicated instance of httpcontext . static class getthread extends thread { private final closeablehttpclient httpclient; private final httpcontext context; private final httpget httpget; public getthread(closeablehttpclient httpclient, httpget httpget) { this.httpclient = httpclient; this.context = httpclientcontext.create(); this.httpget = httpget; } @override public void run() { try { closeablehttpresponse response = httpclient.execute( httpget, context); try { httpentity entity = response.getentity(); } { response.close(); } } catch (clientprotocolexception ex) { // handle protocol errors } catch (ioexception ex) { // handle i/o errors } } }
the main recommendation still same 3.1
please re-use httpclient instance! httpclient instances expensive. throwing away not throw away instance itself, throw away ssl context, connection manager, , persistent connections might kept-alive connection manager.
Comments
Post a Comment