Why JSP is slower than beetl

Source: Internet
Author: User
Tags benchmark

Transferred from: http://my.oschina.net/xiandafu/blog/475740

JSP is pre-compiled into class, and then the template rendering is much slower than beetl, the article from the JSP static text processing, and JSTL to achieve low efficiency to show that the JSP is actually twice times slower than Beetl, freemarker than beetl slower than 6 times times more . This can also be demonstrated from objective benchmarking and third-party performance testing.

Many people do not believe this fact, as the front-end common rendering technology, JSP is slower than beetl. If you know a little bit about these two technologies, you will analyze: JSP is compiled into class, and beetl always interprets execution. JSP will certainly be faster than beetl. However, this is not the case, with many performance tests proving that the beetl is still fast, as shown in the TEB template engine performance benchmark test results:


As you can see, the green, representing Beetl, is about twice times more powerful than the yellow that represents the JSP. There is also a post from osc:http://my.oschina.net/tangcoffee/blog/303865, pressure test JSP and Beetl, proving that beetl performance is twice times the JSP, the following is the interception of some of the data

Using Jfinal+beetl template, Apache AB pressure test results

    • Time taken for tests:0.656 seconds
    • Complete requests:1000
    • Time per request:32.813 [MS] (mean)

Beetl,apache AB test results are not used:

    • Time taken for tests:1.297 seconds
    • Complete requests:1000
    • Time per request:64.844 [MS] (mean)

What exactly is going on, making the JSP execution of the compiled execution slower than the beetl of the interpretation execution. Basically, Beetl does not exceed the normal performance optimization, but the performance optimization of the JSP itself is not the result.

First: The JSP is not good enough for static text processing . If you look at the Java code behind the JSP compilation (take TOCMAT7 as an example), you will find that the JSP does not optimize the static text output. such as the next JSP code
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 " pageencoding=" UTF-8 "%>=" Test JSP "  ; %><%=a%></body>

TOMCAT7 will be compiled into

 out.write (");      Out.write ( "); Out.write ( "<meta http-equiv=\" content-type\ "content=\" text/html;      Charset=iso-8859-1\ ">\r\n" );      Out.write ( "<title>test jsp</title>\r\n" );      Out.write ( "); Out.write ( "<body>\r\n" );      String a  = "Test JSP" ;      Out.write ( ' \ R ' );      Out.write ( \ n ' );      Out.print (a);      Out.write ( \ r \ n );      Out.write ( "</body>\r\n" ); Out.write ( "

As can be seen, for static text, JSP will call the Out.write method multiple times, and the Write method inside, each call, will do flush detection and other time-consuming mechanism . Therefore, the better way should be to merge static text one-time output, should be the following is better point

// expect the JSP to look out.write ("; o Ut.write("\ r \ n");Out.print (a); Out.write ("\r\n</body>\r\n
second, even if the JSP implementation has made the above changes, static text processing and optimization of space。 This is because the Internet transmits the binary,so there will be a process of turning static text into byte[] output, which is a CPU-intensive process, that is, the write operation in the JSP, and a lot of code inside ., and, as the JSP renders again and again, the encoding is repeated once, and the experiment proves that this greatly reduces the performance of the JSP. The following pseudo-code can be used to verify
 Public Static voidMain (string[] args)throwsException {String text= "; {   //Analog JSP   LongStart =System.currenttimemillis ();  for(inti=0;i<1000000;i++){    byte[] bs = text.getbytes ("UTF-8");   Write (BS); }   LongEnd =System.currenttimemillis (); System.out.println ("JSP total=" + (end-start)); }    {   //Analog Beetl   LongStart =System.currenttimemillis (); byte[] bs = text.getbytes ("UTF-8");  for(inti=0;i<1000000;i++) {write (BS); }   LongEnd =System.currenttimemillis (); System.out.println ("Beetl total=" + (end-start)); }   }    Public Static voidWritebyte[] BS) {   }

The output is:

    JSP total=228    beetlTotal =3
Visible BeetlPre-coding static text into binary will improve performance。 And usually JSP, always static text more than JSP code.
third, JSP in Jstl do not perfect, also lead to poor performance。 Because JSP is based on the Java language, the language itself is oo, many places are not suitable for the use of template scenes, so naturally use jstl to compensate for the shortcomings of JSP, which is also a lot of projects are basically used Jstl to write templates. However, the performance of Jstl is more problematic. Like the next simple jstl judgment.
<c:choose>   <c:when test= "${param.newflag== ' 1 ' | | param.newflag== ' 2 '}" >      <th>1 or 2  <font color= "Red" >*</font>   </c:when>  </c:choose>

In my original imagination, I thought that JSP would compile at least the following code:

// expect the JSP to compile into the following code if (Request.getparameter ("Newflag"). Equals ("1")   | | Request.getparameter ("Newflag"). Equals ("2")) {     out.print (...)}

But this is not the case, for the above Jstl, compiled into

// actually the JSP compiled code out.write (java.lang.String)   org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate ("${ param.newflag== ' 1 ' | | param.newflag== ' 2 '} ", java.lang.String. class  nullfalse));

That is, the JSP does not pre-compile as expected Java code, but the dynamic interpretation of the execution of the test condition, so that the performance is not bad to blame it.

To sum up, the reason why the JSP in the benchmark test or the actual test, is much slower than beetl, because he does not have static text output to do a positive optimization. The execution of the explanation like Jstl also greatly drags the JSP back, and Beetl avoids these problems.

Why JSP is slower than beetl

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.