How do I calculate the tomcat thread pool size?

Source: Internet
Author: User
Tags throwable tomcat server

    • Background

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.

    • Goal

Determine the tomcat server thread pool size

    • Specific methods

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.