Is your Java template taking forever to render?

Nehal shrivashtava
Naukri Engineering
Published in
5 min readAug 3, 2023

This blog is written by Simran Gupta and Nehal shrivashtava

So you are building or working on a site and you heard somewhere that server-side rendering is faster than client-side rendering. You selected a template engine, implemented it and now your response improved a little bit. You, your manager, and your team are happy.

But but but…

Do you know, you might improve response time (and chances of your promotion) even more! Yes, you heard it right, with the right Template engine selection you can make your template render faster than a cheetah on Red Bull!!

Ok, I know nobody gonna believe me so to prove it, I conducted a POC comparing and selecting the most popular Java template engines, JTwig, Thymeleaf, Freemarker, and Pebble, based on criteria such as popularity, performance, features, syntax, framework integration, extensibility, and documentation.

First of all, I created 4 APIs for each of these 4 template engines that simply process the template.

Overall Flow

//Service to compile :
//This function process the templates with given variable and value and returns the final data.
public String evaluateThymeleaf() {
String data = ...; //template data
Context myContext = new Context();
myContext.setVariable("variable1", "Value1");
myContext.setVariable("variable2", "Value2");

TemplateEngineConfig obj = new TemplateEngineConfig();
SpringTemplateEngine templateEngine = obj.templateEngine();
return templateEngine.process(data, myContext);
}

//Sample Template data
{
"totalItemsCount":${variable1},
"title": ${variable2}
}

//Final output (Redered template)
{
"totalItemsCount":Value1,
"title": Value2
}

1: Jmeter Test

Tools used:

  • Jmeter (for calculating & comparing Response time).
  • VisualVm (for computing & comparing CPU utilization and heap size utilization).
Jmeter test Results
Response times recorded in Jmeter for different template engines (in milliseconds)
Average time response comparison
  • No. Of Req. is the number of times we called the API for that particular template.
  • Concurrency is the number of users hitting that API at the same time.
  • The time calculated is in ms.
  • The ramp-up period is set to 0 in Jmeter.

VisualVm Screen Shots while performing the above tests -

Thymeleaf
Jtwig
Freemarker
Pebble

2: Execution Time Analysis (Desi way)

Why use a foreign device? In this test, Method Execution time is calculated i.e actual time taken to execute the method in Java which renders the template.

//Like this
//System.currentTimeMillis() is placed right above and right below to the actual method which renders the template.
@GetMapping("/process-thymeleaf")
String getThymeleafData() {
Long inTime = System.currentTimeMillis();
String result = evaluationServices.evaluateThymeleaf();
Long outTime = System.currentTimeMillis();

try {
FileWriter fw = new FileWriter("/home/timeDump.txt", true);
fw.write((outTime - inTime) + "\n");
fw.close();
} catch (Exception e) {
}
return result;
}

and here is the result received :

Method execution time results
Method execution time for rendering templates (in milliseconds)

Practical Impact

In one of the systems, we had considerable use of templates as multiple widgets are created based on template configuration thus we required an efficient JAVA templating engine for rendering purposes.

We started facing significant CPU spikes and multiple outages in the services. The leading cause for this was -

  • JTwig performance — JTwig could not handle concurrent requests as a result, we saw an increase in CPU utilization, and response time increased by 40–50%.
  • A lot of business logic with multiple conditional/loops syntax was implemented in templates that increased the template parsing time.
  • Moreover, continuous GC was getting triggered.

We saw the need for JTwig engine replacement as it was impacting the service. Post this performance analysis on multiple engines, we migrated to the Pebble engine and saw significant results in terms of CPU, Response time:

  • We have seen that the response time of the service w.r.t to branding instances reduced by 500%. Earlier, the average RT was around 400–450ms, and we see a drastic drop here, thus the average RT is 70–80ms now.
July 2022
drop in response time after the change in the template engine
  • We have also seen a drop in CPU utilization, at peak time, it would take around 6–7 cores of CPU so we had to use more resources to deploy our services, at present the value is below one core.
  • In our service, we open executor threads internally to power multiple templates for the same request, so executor threads get stuck as we reach the max pool size. CPU utilization reduction helped us to solve this issue as well.

Conclusion:
Pebble’s average response time is lower than other template engines in comparison. Also, if currently you are using Jtwig then the syntax of Pebble is almost similar to Jtwig so switching to Pebble is easier.
You can have Freemarker as a backup.
But I am sure, this analysis will help you to make a smart choice. Check these links to find out where these templating engines are being used!

Useful links

Codebase: https://github.com/nehal-backspace/POC_templateEngine

Who is using Freemarker: DriveNow Car Hire | Apache NetBeans | Full list

Who is using Thymeleaf: Thymeleaf users

Who is using Pebble: Pebble users

Extensive comparison: Github repo

Groovy & Mustache comparison: https://springhow.com/spring-boot-template-engines-comparison/

--

--