Skip to content

Commit

Permalink
Renamed benchmarks and added a gnuplot script file to make plots
Browse files Browse the repository at this point in the history
  • Loading branch information
mbosecke committed Mar 2, 2015
1 parent 28871c8 commit fcd7aa6
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 50 deletions.
44 changes: 15 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
template-benchmark
================

JMH benchmark for the the most popular Java template engines.
JMH benchmark for the popular Java template engines:
1. Freemarker
2. Mustache
3. Pebble
4. Thymeleaf
5. Velocity

Running the benchmark
======================

1. Download the source code and build it (`mvn clean install`)
2. Run the benchmark suite with `java -jar target/benchmarks.jar`

Generating plot
===============
1. Run benchmark while exporting results to csv with `java -jar target/benchmarks.jar -rff results.csv -rf csv`
2. Use gnuplot to generate plot with `gnuplot benchmark.plot`. This will output `results.png`.

Rules of Template Engine Configuration
======================================
It is imperative that each template engine is configured in way to reflect real-world usage as opposed to it's *optimal* configuration. Typically this means an out-of-the-box configuration.
Expand All @@ -19,34 +32,7 @@ Interpreting the Results
The benchmarks measure throughput, given in "ops/time". The time unit used is seconds.
Generally, the score represents the number of templates rendered per second; the higher the score, the better.

Example Output
Example Results
===============
The following benchmark was run on a machine with the following stats:

* OS: CentOS 7
* Kernel: 3.10.0-123.el7.x86_64
* Memory: 8GB
* Processor: Intel(R) Core(TM) i5-3470 CPU @ 3.20GHz

````
# JMH 1.6.1 (released 4 days ago)
# VM invoker: /usr/java/jdk1.7.0_67/jre/bin/java
# VM options: <none>
# Warmup: 5 iterations, 1 s each
# Measurement: 10 iterations, 1 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
(...)
# Run complete. Total time: 00:06:23
Benchmark Mode Cnt Score Error Units
FreemarkerBenchmark.benchmark thrpt 50 14886.563 ± 128.597 ops/s
MoustacheBenchmark.benchmark thrpt 50 32821.125 ± 284.831 ops/s
PebbleBenchmark.benchmark thrpt 50 26547.355 ± 311.757 ops/s
ThymeleafBenchmark.benchmark thrpt 50 756.536 ± 16.601 ops/s
VelocityBenchmark.benchmark thrpt 50 18388.098 ± 59.178 ops/s
````

![Template Comparison](results.png)
30 changes: 30 additions & 0 deletions benchmark.plot
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Labels
set title 'Java Template Engine Performance Comparison'
set ylabel 'Templates rendered per second'
set xlabel 'Template Engine'
set xtics nomirror rotate by -45

# Ranges
set autoscale

# Input
set datafile separator ','

# Output
set terminal pngcairo enhanced font "Verdana,9"
set output 'results.png'
set grid
set key off
set boxwidth 0.8 relative

# box style
set style line 1 lc rgb '#5C91CD' lt 1
set style fill solid

# remove top and right borders
set style line 2 lc rgb '#808080' lt 1
set border 3 back ls 2
set tics nomirror

plot 'results.csv' every ::1 using 0:5:xticlabels(stringcolumn(1)[31:36]) with boxes ls 1,\
'results.csv' every ::1 using 0:($5 + 1500):(sprintf("%d",$5)) with labels
10 changes: 5 additions & 5 deletions results.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"Benchmark","Mode","Threads","Samples","Score","Score Error (99.9%)","Unit"
"com.mitchellbosecke.benchmark.FreemarkerBenchmark.benchmark","thrpt",1,50.000000,14963.742414,107.939165,"ops/s"
"com.mitchellbosecke.benchmark.MoustacheBenchmark.benchmark","thrpt",1,50.000000,32836.315928,135.504102,"ops/s"
"com.mitchellbosecke.benchmark.PebbleBenchmark.benchmark","thrpt",1,50.000000,27515.889592,324.357246,"ops/s"
"com.mitchellbosecke.benchmark.ThymeleafBenchmark.benchmark","thrpt",1,50.000000,788.701841,11.296145,"ops/s"
"com.mitchellbosecke.benchmark.VelocityBenchmark.benchmark","thrpt",1,50.000000,18830.615978,141.901540,"ops/s"
"com.mitchellbosecke.benchmark.Freemarker.benchmark","thrpt",1,50.000000,14923.957014,178.214593,"ops/s"
"com.mitchellbosecke.benchmark.Moustache.benchmark","thrpt",1,50.000000,38353.921882,292.072738,"ops/s"
"com.mitchellbosecke.benchmark.Pebble.benchmark","thrpt",1,50.000000,33269.274946,263.750858,"ops/s"
"com.mitchellbosecke.benchmark.Thymeleaf.benchmark","thrpt",1,50.000000,967.923884,11.037038,"ops/s"
"com.mitchellbosecke.benchmark.Velocity.benchmark","thrpt",1,50.000000,19896.369833,134.624876,"ops/s"
Binary file modified results.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class FreemarkerBenchmark extends BaseBenchmark {
public class Freemarker extends BaseBenchmark {

private Map<String, Object> context;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,32 @@
import java.io.Writer;
import java.util.Map;

import com.github.mustachejava.MustacheException;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Setup;

import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheException;
import com.github.mustachejava.MustacheFactory;

public class MoustacheBenchmark extends BaseBenchmark {
public class Mustache extends BaseBenchmark {

private Map<String, Object> context;

private Mustache template;
private com.github.mustachejava.Mustache template;

@Setup
public void setup() {
MustacheFactory mustacheFactory = new DefaultMustacheFactory() {
@Override
public void encode(String value, Writer writer) {
// Disable HTML escaping
try {
writer.write(value);
} catch (IOException e) {
throw new MustacheException(e);

@Override
public void encode(String value, Writer writer) {
// Disable HTML escaping
try {
writer.write(value);
} catch (IOException e) {
throw new MustacheException(e);
}
}
}
};
template = mustacheFactory.compile("templates/stocks.mustache.html");
this.context = getContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.mitchellbosecke.pebble.extension.escaper.EscaperExtension;
import com.mitchellbosecke.pebble.template.PebbleTemplate;

public class PebbleBenchmark extends BaseBenchmark {
public class Pebble extends BaseBenchmark {

private Map<String, Object> context;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import freemarker.template.TemplateException;

public class ThymeleafBenchmark extends BaseBenchmark {
public class Thymeleaf extends BaseBenchmark {

private TemplateEngine engine;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Setup;

public class VelocityBenchmark extends BaseBenchmark {
public class Velocity extends BaseBenchmark {

private VelocityContext context;

Expand Down

0 comments on commit fcd7aa6

Please sign in to comment.