jreijn/spring-comparing-template-engines

Improving Thymeleaf results

danielfernandez opened this issue · 3 comments

At first sight, there are a couple of modifications you can do in order to improve your Thymeleaf results.

  • The first time it is executed, Thymeleaf needs to perform a good amount of initializations. Question: It is not clear from your README... are you first executing some un-benchmarked requests against your URLs in order to allow it to initialize?
  • Executing not only one request, but in fact a number of them, will also help the JVM to warm up and compile some methods into native and enhance your results. For example, these are the results with your same ab -n 10000 -c 4 command in three subsequent executions against the thymeleaf URL:
Time taken for tests:   10.756 seconds
Time taken for tests:   8.159 seconds
Time taken for tests:   5.394 seconds
  • I see you include the ConditionalCommentsDialect dialect into your template engine, even if it isn't needed (your templates do not include conditional comments). This dialect actually reduces a bit the performace of Thymeleaf, because in order to execute it needs not only to check element (i.e. tag) nodes for processors, but also text and comment nodes. And there are a lot of text comments in templates. So removing this dialect if you don't need it should improve a bit your performance.

Thanks for the figures! I will tell you if I find further points of improvement.

Thank you so much for the comment and improvements! I've tried to incorporate all your feedback and I've also switched to using ApacheBench, which seemed to give even better results (not sure why yet), but still Thymeleaf is a bit slower on my 2009 Macbook Pro then the rest. Since most of this is CPU bound I guess, the time will be a lot less on faster machines. I assume that Thymeaf is a bit slower then the rest, mostly because of it being DOM based.

I've updated the README as well. Before doing the measurements I always did at least 2 dry runs with the same configuration as I would do to measure the results. The average number of seconds is measured over 5 consecutive runs.

I must have read over the ConditionalCommentsDialect. It seems indeed that it was not necessary to have the dependency and the additional overhead within the templates.

In general my view of Thymeleaf is that it's a great template engine. It's still young, but adds tremendous value for Java developers working on web applications.

Yup, Thymeleaf is slower than (at least) JSP, Velocity and FreeMarker, I can confirm that.

From our perspective, it is a technology of a different nature (markup-conscious template engine vs. text processor), and allows developers to work at a slightly higher level. So trading a bit of performance at the view layer --which normally takes only a tiny % of the execution time of web applications-- in exchange for a better development and maintenance experience usually looks like a good deal. It's a bit what you do when you use Hibernate instead of coding JDBC queries, for example.

The updated figures in your README look feasible to me. We normally see a 2x-3x increase compared to JSP, though this depends a lot on the specific scenario. My worries were more focused on the comparison chart in your presentation here http://es.slideshare.net/jreijn/comparing-templateenginesjvm, which show an approx 6x increase compared to JSP and looked really weird to my eye. Maybe you were not using Spring there? when not in a Spring environment, thymeleaf uses OGNL instead of SpringEL, and OGNL is much slower executing expressions...

Anyway, in case you are interested, we are planning to mostly rewrite our parsing and DOM processing system for Thymeleaf 3.0, with the aim of (among other things) improving performance.

In my presentation I was using the results coming from this specific application with Spring MVC. I tried to keep the examples as close as possible together, so they all use i18n, fragments, iteration, etc.

My personal perspective is that Thymeleaf is not that much slower. Maybe it looks this way from the slidedeck, but if you look closely at the slide with the chart you might see it starts at 12.50 seconds and not at 0. I thought this made it a bit more clear what the difference was between Freemarker, Velocity, JSP and Mustache, since they were very close to each other. I explained this during my talk, but it might be easy to overlook from the slidedeck.

I really like the way Thymeleaf works and I will keep a very close eye on Thymeleaf as a replacement of Freemarker or JSP as the view technology used by Hippo CMS. I will definitely keep an eye on what's going on with Thymeleaf 3.0