44
Shootout! Template engines on the JVM Jeroen Reijn | @jreijn | #jfall13

Shootout! template engines on the jvm

  • Upload
    nljug

  • View
    1.076

  • Download
    3

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Shootout! template engines on the jvm

Shootout! Template engines on the JVM

Jeroen Reijn | @jreijn | #jfall13

Page 2: Shootout! template engines on the jvm

About me

• Architect @ Hippo

• Worked on web based platforms since 2001 with PHP, XSLT, JSP, Velocity and Freemarker

• Blogger at http://blog.jeroenreijn.com

Page 3: Shootout! template engines on the jvm

Session agenda

• General overview

• Interesting template engines

• Benchmark project

• Performance results

Page 4: Shootout! template engines on the jvm

Template engine?

• What is a Template ?

• What is a Template Engine ?

• How many Java Template Engines ?

• Java-Source.net : 21

• Wikipedia: 17

Page 5: Shootout! template engines on the jvm

Most commonly used

• JSP

• Freemarker

• Velocity

Page 6: Shootout! template engines on the jvm

Overview

Page 7: Shootout! template engines on the jvm

Selection criteria

• Project activity

• Framework support

• Active Community

• Brings added value

• IDE support

Page 8: Shootout! template engines on the jvm

Most interesting

• Thymeleaf

• Mustache

• Jade

• Scalate

Page 9: Shootout! template engines on the jvm

Thymeleaf

Page 10: Shootout! template engines on the jvm

Overview

• Available at http://www.thymeleaf.org/

• Java, DOM based template engine

• XML / XHTML / HTML5

• Full Spring MVC integration

• ‘Natural templating’

Page 11: Shootout! template engines on the jvm

Natural templating

• From Wikipedia:

“The template can be a document as valid as the final result, the engine syntax doesn't

break the document's structure”

Page 12: Shootout! template engines on the jvm

JSP in browser

Page 13: Shootout! template engines on the jvm

Thymeleaf in browser

Page 14: Shootout! template engines on the jvm
Page 15: Shootout! template engines on the jvm

Variables & Loops

• Variables

• Loops

Page 16: Shootout! template engines on the jvm

Fragments

• Include fragment

• Define fragment based on th:fragment

Page 17: Shootout! template engines on the jvm

{{ Mustache }}

Page 18: Shootout! template engines on the jvm

Overview

• Available at https://mustache.github.com

• “logic-less” because there are no control statements (i.e. if, else, loops, etc)

• Tags are specified using {{ }} (looks like a mustache)

• Implemented by many languages: Java, Ruby, JavaScript, Python, Erlang, PHP, Objective-C, .NET, C++, Go, ColdFusion, Scala, etc.

Page 19: Shootout! template engines on the jvm

Overview

• Java implementaions:

• JMustache

• Mustache.java

• Not web specific

• Spring MVC integration as an add-on

Page 20: Shootout! template engines on the jvm

Variables

{“greeting” : “Hello”,

“message” : “World”}

{{greeting}} {{message}}

Data Template

Hello World

Output

Page 21: Shootout! template engines on the jvm

Sections

{

"repo": [

{ "name": "resque" },

{ "name": "hub" },

{ "name": "rip" },

]

}

{{#repo}} <li>{{name}}</li>{{/repo}}

Data Template

<li>resque</li><li>hub</li><li>rip</li>

Output

Page 22: Shootout! template engines on the jvm

Fragments

base.mustache:<h2>Presentations</h2>{{#presentations}} {{> presentation}}{{/presentations}}

presentation.mustache:<h3>{{title}}</h3><p>{{startTime}}</p>

Template

Page 23: Shootout! template engines on the jvm

Jade

Page 24: Shootout! template engines on the jvm

Jade

• Available at http://jade-lang.com

• Node.js Template engine

• Java Implementation: Jade4j

• Compact notation

Page 25: Shootout! template engines on the jvm

Syntax

Page 26: Shootout! template engines on the jvm

Result

Page 27: Shootout! template engines on the jvm

Variables & Iterations

• Variables

• Iterations

Page 28: Shootout! template engines on the jvm

Fragments

Page 29: Shootout! template engines on the jvm
Page 30: Shootout! template engines on the jvm

Overview

• Available at http://scalate.fusesource.org/

• Scala based Template Engine (ScalaTE)

• Templates are compiled as Scala objects

• Equivalent to using JSP, JSTL, JSP EL & Tiles / Sitemesh

• Integration available for Spring, Play, Lift, ...

Page 31: Shootout! template engines on the jvm

Template languages

• SSP (Scala Server Pages)

• SCAML (based on Ruby’s HAML)

• Mustache

• Jade

Page 32: Shootout! template engines on the jvm

Scaml

• Based on HAML (HTML abstraction markup language)

• Integrates Scala code

• Well formatted markup

• Similar to Jade

Page 33: Shootout! template engines on the jvm

Variables & Loops

• Fragment support is possible

• Inline Scala code

Page 34: Shootout! template engines on the jvm

Benchmark project

Page 35: Shootout! template engines on the jvm

Benchmark project

• Simple Spring MVC application

• Multiple view resolvers

• Page that show a list of presentations

• Uses page layouts / fragments

• Uses i18n where possible

Page 36: Shootout! template engines on the jvm

Spring MVC

Page 37: Shootout! template engines on the jvm

public class Presentation { private Long id; private String title; private String speakerName; private String summary; private String room; private Date startTime; private Date endTime;}

Page 38: Shootout! template engines on the jvm
Page 40: Shootout! template engines on the jvm

Results!

Page 41: Shootout! template engines on the jvm

Performance

12,50s

16,00s

19,50s

23,00s

26,50s

30,00s

Total time taken for 25.000 requests (less is better)

Thymeleaf - v2.1.0Jade4j - v0.3.17Scalate - v1.6.1Mustache - v1.8Freemarker - v2.3.19Velocity - v1.7JSP - 2.1

Page 42: Shootout! template engines on the jvm

Findings

• Thymeleaf used more memory and was slower then the commonly used

• Scalate was a lot slower due to the layout mechanism more than 100 seconds for 25.000 requests.

• Mustache performs really well

Page 43: Shootout! template engines on the jvm

Tips• If you want designers to own your templates

consider Mustache or Thymeleaf

• If you want developers to own your templates:

• If you want to generate HTML / XML and DRY? => Jade

• otherwise: JSP, Freemarker, etc