|
| 1 | +package com.hubspot.jinjava.benchmarks; |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableList; |
| 4 | +import com.google.common.collect.ImmutableMap; |
| 5 | +import com.google.common.io.Resources; |
| 6 | +import com.hubspot.jinjava.Jinjava; |
| 7 | +import com.hubspot.jinjava.JinjavaConfig; |
| 8 | +import java.io.IOException; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.concurrent.TimeUnit; |
| 14 | +import org.openjdk.jmh.annotations.Benchmark; |
| 15 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 16 | +import org.openjdk.jmh.annotations.Fork; |
| 17 | +import org.openjdk.jmh.annotations.Measurement; |
| 18 | +import org.openjdk.jmh.annotations.Mode; |
| 19 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 20 | +import org.openjdk.jmh.annotations.Scope; |
| 21 | +import org.openjdk.jmh.annotations.Setup; |
| 22 | +import org.openjdk.jmh.annotations.State; |
| 23 | +import org.openjdk.jmh.annotations.Warmup; |
| 24 | +import org.openjdk.jmh.runner.Runner; |
| 25 | +import org.openjdk.jmh.runner.RunnerException; |
| 26 | +import org.openjdk.jmh.runner.options.Options; |
| 27 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 28 | + |
| 29 | +@State(Scope.Benchmark) |
| 30 | +@BenchmarkMode(Mode.Throughput) |
| 31 | +@OutputTimeUnit(TimeUnit.SECONDS) |
| 32 | +@Fork(1) |
| 33 | +@Warmup(iterations = 3, time = 3) |
| 34 | +@Measurement(iterations = 5, time = 3) |
| 35 | +public class JinjavaBenchmark { |
| 36 | + |
| 37 | + private Jinjava jinjava; |
| 38 | + private String simpleTemplate; |
| 39 | + private String complexTemplate; |
| 40 | + private Map<String, Object> simpleBindings; |
| 41 | + private Map<String, Object> complexBindings; |
| 42 | + |
| 43 | + @Setup |
| 44 | + public void setup() throws IOException { |
| 45 | + jinjava = new Jinjava(); |
| 46 | + |
| 47 | + simpleTemplate = |
| 48 | + Resources.toString( |
| 49 | + Resources.getResource("benchmarks/simple.jinja"), |
| 50 | + StandardCharsets.UTF_8 |
| 51 | + ); |
| 52 | + complexTemplate = |
| 53 | + Resources.toString( |
| 54 | + Resources.getResource("benchmarks/complex.jinja"), |
| 55 | + StandardCharsets.UTF_8 |
| 56 | + ); |
| 57 | + |
| 58 | + List<List<String>> table = new ArrayList<>(); |
| 59 | + for (int i = 0; i < 10; i++) { |
| 60 | + List<String> row = new ArrayList<>(); |
| 61 | + for (int j = 0; j < 5; j++) { |
| 62 | + row.add("cell_" + i + "_" + j); |
| 63 | + } |
| 64 | + table.add(row); |
| 65 | + } |
| 66 | + |
| 67 | + simpleBindings = |
| 68 | + ImmutableMap.of( |
| 69 | + "page_title", |
| 70 | + "Jinjava Benchmark", |
| 71 | + "navigation", |
| 72 | + ImmutableList.of( |
| 73 | + ImmutableMap.of("href", "index.html", "caption", "Index"), |
| 74 | + ImmutableMap.of("href", "downloads.html", "caption", "Downloads"), |
| 75 | + ImmutableMap.of("href", "products.html", "caption", "Products") |
| 76 | + ), |
| 77 | + "table", |
| 78 | + table |
| 79 | + ); |
| 80 | + |
| 81 | + List<Map<String, Object>> users = ImmutableList.of( |
| 82 | + ImmutableMap.of("href", "/user/john", "username", "John Doe"), |
| 83 | + ImmutableMap.of("href", "/user/jane", "username", "Jane Doe"), |
| 84 | + ImmutableMap.of("href", "/user/peter", "username", "Peter Somewhat") |
| 85 | + ); |
| 86 | + |
| 87 | + List<Map<String, Object>> articles = new ArrayList<>(); |
| 88 | + for (int i = 0; i < 20; i++) { |
| 89 | + articles.add( |
| 90 | + ImmutableMap |
| 91 | + .<String, Object>builder() |
| 92 | + .put("href", "/article/" + i) |
| 93 | + .put("title", "Article Title Number " + i) |
| 94 | + .put("user", users.get(i % users.size())) |
| 95 | + .put( |
| 96 | + "body", |
| 97 | + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. ".repeat(5) |
| 98 | + ) |
| 99 | + .put("pub_date", "2024-01-" + String.format("%02d", (i % 28) + 1)) |
| 100 | + .put( |
| 101 | + "tags", |
| 102 | + ImmutableList.of("tag" + (i % 3), "tag" + (i % 5), "tag" + (i % 7)) |
| 103 | + ) |
| 104 | + .build() |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + complexBindings = |
| 109 | + ImmutableMap.of( |
| 110 | + "page_title", |
| 111 | + "Jinjava Complex Benchmark", |
| 112 | + "navigation", |
| 113 | + ImmutableList.of( |
| 114 | + ImmutableMap.of("href", "index.html", "caption", "Index"), |
| 115 | + ImmutableMap.of("href", "about.html", "caption", "About"), |
| 116 | + ImmutableMap.of("href", "contact.html", "caption", "Contact") |
| 117 | + ), |
| 118 | + "users", |
| 119 | + users, |
| 120 | + "articles", |
| 121 | + articles |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + @Benchmark |
| 126 | + public String simpleTemplateBenchmark() { |
| 127 | + return jinjava.render(simpleTemplate, simpleBindings); |
| 128 | + } |
| 129 | + |
| 130 | + @Benchmark |
| 131 | + public String complexTemplateBenchmark() { |
| 132 | + return jinjava.render(complexTemplate, complexBindings); |
| 133 | + } |
| 134 | + |
| 135 | + @Benchmark |
| 136 | + public String simpleExpressionBenchmark() { |
| 137 | + return jinjava.render("Hello {{ name }}!", ImmutableMap.of("name", "World")); |
| 138 | + } |
| 139 | + |
| 140 | + @Benchmark |
| 141 | + public String filterChainBenchmark() { |
| 142 | + return jinjava.render( |
| 143 | + "{{ value|capitalize|replace('foo', 'bar')|trim }}", |
| 144 | + ImmutableMap.of("value", " foo hello world ") |
| 145 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + @Benchmark |
| 149 | + public Jinjava jinjavaCreationBenchmark() { |
| 150 | + return new Jinjava(); |
| 151 | + } |
| 152 | + |
| 153 | + @Benchmark |
| 154 | + public Jinjava jinjavaWithConfigCreationBenchmark() { |
| 155 | + return new Jinjava( |
| 156 | + JinjavaConfig.newBuilder().withMaxRenderDepth(20).withTrimBlocks(true).build() |
| 157 | + ); |
| 158 | + } |
| 159 | + |
| 160 | + public static void main(String[] args) throws RunnerException { |
| 161 | + Options options = new OptionsBuilder() |
| 162 | + .include(JinjavaBenchmark.class.getSimpleName()) |
| 163 | + .build(); |
| 164 | + new Runner(options).run(); |
| 165 | + } |
| 166 | +} |
0 commit comments