It involves using Tomcat as a server in our daily development, but how much of a thread pool should we set up? And according to what principles to design this thread pool?
Next, I will introduce how I design and calculate.
Determine the tomcat server thread pool size
It is well known that Tomcat accepts a request post-processing process that involves CPU and IO time. When IO waits, the CPU passively discards execution, and other threads can take advantage of this time slice to operate.
So we can adopt the general rules of server IO optimization:
Thread size = ((thread IO time + thread CPU)/thread CPU hours) * CPU Cores
Example: Thread io time is 100ms (IO operation such as database query, synchronous remote call, etc.), thread CPU time 10ms, server physical machine core number is 4. By the above formula, the size we calculated is ((100 + 10)/10) = 44. Theoretically we have a basis, but how do we know the thread IO time and CPU time during the actual calculation? This involves the actual coding process of how to monitor the processing time.
Below I introduce the practice in my project
1. Using Java to implement the built-in filter interface, we can get the total time consumed by a request
public class Moniterfilter implements Filter {private static final Logger Logger = Loggerfactory.getlogger (moniterfilt Er.class); @Override public void DoFilter (ServletRequest request, servletresponse response, Filterchain chain) throws IOException, servletexception {Long start = System.currenttimemillis (); HttpServletRequest HttpRequest = (httpservletrequest) request; HttpServletResponse HttpResponse = (httpservletresponse) response; String uri = Httprequest.getrequesturi (); String params = getquerystring (HttpRequest); try {chain.dofilter (HttpRequest, HttpResponse); } finally {long cost = System.currenttimemillis ()-Start; Logger.info ("Access URL [{}}], cost time [{}] ms)", URI, params, cost); } private String getquerystring (HttpServletRequest req) {StringBuilder buffer = new StringBuilder ("?"); enumeration<string> Emparams = Req.getparameternameS (); try {while (emparams.hasmoreelements ()) {String SParam = emparams.nextelement (); String svalues = Req.getparameter (SParam); Buffer.append (SParam). Append ("="). Append (Svalues). Append ("&"); } return buffer.substring (0, Buffer.length ()-1); } catch (Exception e) {logger.error ("Get Post arguments Error", buffer.tostring ()); } return ""; } @Override public void Destroy () {} @Override public void init (Filterconfig filterconfig) throws Servlete Xception {}}
2. Monitor thread io time by adding facets (jdk,cglib)
public class Daointerceptor implements Methodinterceptor { private static final Logger Logger = Loggerfactory.getlogg ER (daointerceptor.class); @Override public Object invoke (Methodinvocation invocation) throws Throwable { StopWatch watch = new StopWatch () ; Watch.start (); Object result = null; Throwable t = null; try { result = Invocation.proceed (); } catch (Throwable e) { T = e = = null? Null:e.getcause (); throw e; } finally { watch.stop (); Logger.info ("({}ms)", Watch.gettotaltimemillis ()); } return result;} }
The above code can be used to calculate the corresponding time, so as to calculate the size of the outgoing thread. But we're done here?
In fact, the calculated value is only in the theoretical case, we still need to use the pressure measurement tool (Jmeter) to test the line server, while dynamically fine-tuning based on the QPS value of the thread pool size just calculated.
If the article also has practical significance for everyone, please recommend.
Http://www.cnblogs.com/tylercao/p/4733238.html
How do I calculate the tomcat thread pool size?