Why is JSP slower than Beetl?

Source: Internet
Author: User

Why is JSP slower than Beetl?

Many people do not believe this fact. As a common frontend rendering technology, JSP is slower than Beetl. If you know these two technologies a little, you will analyze: JSP is compiled into a class, while Beetl is always interpreted and executed. JSP is certainly faster than Beetl. However, this is not the case. It has passed many performance tests and proved that Beetl is still fast. The following is the performance benchmark test result of the TEB template engine:

It can be seen that, representing the green Beetl, the performance is higher than that representing about two times the yellow of JSP.

Another post from osc: Success

Use jfinal + beetl templates and apache AB stress testing results

  • Time taken for tests: 0.656 seconds

  • Complete requests: 1000

  • Time per request: 32.813 [MS] (mean)

If beetl is not used, test results of apache AB are as follows:

  • Time taken for tests: 1.297 seconds

  • Complete requests: 1000

  • Time per request: 64.844 [MS] (mean)

The execution of compiled JSP files is slower than the execution of Beetl files. Basically, Beetl does not make performance optimization beyond the conventional performance, but is caused by insufficient performance optimization of JSP.

First, JSP is not good enough for static text processing..

If you look at the java code compiled by JSP (taking Tocmat7 as an example), you will find that JSP has not optimized static text output. For example, the next JSP code

 
  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
  2.     pageEncoding="UTF-8"%> 
  3. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
  4. <title>Test JSP</title> 
  5. <body> 
  6. <% 
  7. String a = "Test JSP"; 
  8. %> 
  9. <%=a %> 
  10. </body> 

Tomcat7 will be compiled

 
  1. out.write("
  2.       out.write("
  3.       out.write("<meta http-equiv=\"Content-Type\" 
  4.             content=\"text/html; charset=ISO-8859-1\">\r\n"); 
  5.       out.write("<title>Test JSP</title>\r\n"); 
  6.       out.write("
  7.       out.write("<body>\r\n"); 
  8. String a = "Test JSP"; 
  9.       out.write('\r'); 
  10.       out.write('\n'); 
  11.       out.print(a ); 
  12.       out.write("\r\n"); 
  13.       out.write("</body>\r\n"); 
  14.       out.write("

It can be seen that for static text, JSP will call the out. write method multiple times, and the time-consuming mechanism such as flush detection will be performed for each call within the write method. Therefore, a better way is to merge static text into one-time output, which is better

// Expected JSP format

 
  1. out.write("
  2. String a = "Test JSP"; 
  3. out.write("\r\n“); 
  4. out.print(a ); 
  5. out.write("\r\n</body>\r\n

Second, even if the JSP implementation has changed the above, there is still room for optimization for static text processing.. This is because of the binary data transmitted over the Internet, so there will be a process of converting static text into byte [] output. This is a CPU-consuming process, that is, the write operation in JSP, there are still a lot of internal code, and, with JSP rendering again and again, encoding is a repetition, experiments prove that this greatly reduces the JSP performance. The following pseudo code can be used to verify

 
  1. Public static void main (String [] args) throws Exception {
  2. String text = "
  3. {
  4. // Simulate jsp
  5. Long start = System. currentTimeMillis ();
  6. For (int I = 0; I <1000000; I ++ ){
  7. Byte [] bs = text. getBytes ("UTF-8 ");
  8. Write (bs );
  9. }
  10. Long end = System. currentTimeMillis ();
  11. System. out. println ("jsp total =" + (end-start ));
  12. }
  13.  
  14. {
  15. // Simulate beetl
  16. Long start = System. currentTimeMillis ();
  17. Byte [] bs = text. getBytes ("UTF-8 ");
  18. For (int I = 0; I <1000000; I ++ ){
  19. Write (bs );
  20. }
  21. Long end = System. currentTimeMillis ();
  22. System. out. println ("beetl total =" + (end-start ));
  23. }
  24.  
  25. }
  26.  
  27. Public static void write (byte [] bs ){
  28.  
  29. }

The output is:

  • Jsp total = 228

  • Beetl total = 3

It can be seen that Beetl pre-encodes static text into binary, which will improve the performance a lot. In general, JSP always has more static text than JSP Code.

Third, JSP is not perfect in JSTL, leading to poor performance..

Because JSP is based on the Java language and the language itself is OO, it is not suitable for template scenarios in many places. Therefore, JSTL is naturally used to make up for the shortcomings of JSP, later, many projects basically used JSTL to write templates. However, the performance of JSTL is more problematic. For example, the next simple JSTL judgment

 
  1. <c:choose> 
  2.    <c:when test="${param.newFlag== '1' || param.newFlag== '2'}">  
  3.     <th>1 or 2  <font color="Red">*</font> 
  4.    </c:when>  
  5. </c:choose> 

In my initial imagination, I thought jsp would at least be compiled into the following code:

// Expect JSP to be compiled into the following code

 
  1. if(request.getParameter("newFlag").equals("1") 
  2.    ||request.getParameter("newFlag").equals("2")){ 
  3.  
  4.     out.print(...) 
  5. } 

But this is not the case. For the above JSTL, compile

 
  1. // Actually compiled JSP code
  2. Out. write (java. lang. String)
  3. Org. apache. jasper. runtime. PageContextImpl. proprietaryEvaluate (
  4. "$ {Param. newFlag = '1' | param. newFlag = '2 '}",
  5. Java. lang. String. class,
  6. (Javax. servlet. jsp. PageContext) _ jspx_page_context, null, false ));

That is to say, JSP does not translate as expected into java code, but dynamically explains and executes the test condition. In this way, performance is not bad.

To sum up, JSP is much slower than Beetl in the benchmark test and actual test because it has not been actively optimized in the static text output. The execution of explanations like JSTL is also greatly dragged back by JSP, while Beetl avoids these problems.

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.