Skip to content

Commit 42bb234

Browse files
committed
enable all the benchmarks
1 parent e8df536 commit 42bb234

2 files changed

Lines changed: 64 additions & 49 deletions

File tree

‎benchmarks/src/main/java/mustachejava/benchmarks/Main.java‎

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
import com.github.mustachejava.DefaultMustacheFactory;
44
import com.github.mustachejava.Mustache;
55
import com.github.mustachejava.reflect.SimpleObjectHandler;
6+
import com.github.mustachejava.util.HtmlEscaper;
67
import com.github.mustachejavabenchmarks.NullWriter;
7-
import org.openjdk.jmh.annotations.Scope;
8-
import org.openjdk.jmh.annotations.State;
8+
import org.openjdk.jmh.annotations.*;
99

1010
import java.io.IOException;
1111
import java.io.StringReader;
1212
import java.util.ArrayList;
1313
import java.util.List;
14+
import java.util.concurrent.TimeUnit;
1415

1516
/*
1617
@@ -37,6 +38,13 @@
3738
Main.benchJustEscapeOnce thrpt 20 102768645.605 ± 3764598.754 ops/s
3839
Main.benchJustEscapeTwo thrpt 20 81450001.026 ± 2270993.769 ops/s
3940
41+
Now purely an array lookup
42+
Benchmark Mode Cnt Score Error Units
43+
Main.benchJustEscapeClean thrpt 20 408133726.531 ± 41452124.506 ops/s
44+
Main.benchJustEscapeOldClean thrpt 20 136543848.645 ± 4981651.587 ops/s
45+
Main.benchJustEscapeOnce thrpt 20 216812988.639 ± 16074660.164 ops/s
46+
Main.benchJustEscapeTwo thrpt 20 101607565.357 ± 5789918.319 ops/s
47+
4048
Benchmark Mode Cnt Score Error Units
4149
Main.benchMustache thrpt 20 763256.970 ± 59474.781 ops/s
4250
@@ -79,45 +87,39 @@ public class Main {
7987
}
8088
}
8189

82-
// @Benchmark
83-
// @BenchmarkMode(Mode.Throughput)
84-
// @OutputTimeUnit(TimeUnit.SECONDS)
85-
// public void benchMustache() throws IOException {
86-
// normal.execute(nw, scope).close();
87-
// }
88-
//
89-
// @Benchmark
90-
// @BenchmarkMode(Mode.Throughput)
91-
// @OutputTimeUnit(TimeUnit.SECONDS)
92-
// public void benchMustacheSimple() throws IOException {
93-
// simple.execute(nw, scope).close();
94-
// }
95-
96-
97-
// @Benchmark
98-
// public void benchTheoreticalLimit() throws IOException {
99-
// nw.write("variable");
100-
// }
101-
//
102-
// @Benchmark
103-
// public void benchJustEscapeClean() {
104-
// HtmlEscaper.escape("variable", nw);
105-
// }
106-
//
107-
// @Benchmark
108-
// public void benchJustEscapeOnce() {
109-
// HtmlEscaper.escape("vari\"ble", nw);
110-
// }
111-
//
112-
// @Benchmark
113-
// public void benchJustEscapeTwo() {
114-
// HtmlEscaper.escape("va&ri\"ble", nw);
115-
// }
90+
@Benchmark
91+
@BenchmarkMode(Mode.Throughput)
92+
@OutputTimeUnit(TimeUnit.SECONDS)
93+
public void benchMustache() throws IOException {
94+
normal.execute(nw, scope).close();
95+
}
96+
97+
@Benchmark
98+
@BenchmarkMode(Mode.Throughput)
99+
@OutputTimeUnit(TimeUnit.SECONDS)
100+
public void benchMustacheSimple() throws IOException {
101+
simple.execute(nw, scope).close();
102+
}
103+
104+
@Benchmark
105+
public void benchJustEscapeClean() {
106+
HtmlEscaper.escape("variable", nw);
107+
}
108+
109+
@Benchmark
110+
public void benchJustEscapeOnce() {
111+
HtmlEscaper.escape("vari\"ble", nw);
112+
}
113+
114+
@Benchmark
115+
public void benchJustEscapeTwo() {
116+
HtmlEscaper.escape("va&ri\"ble", nw);
117+
}
116118

117119
public static void main(String[] args) throws IOException {
118-
// Main main = new Main();
119-
// while (true) {
120-
// main.benchMustacheSimple();
121-
// }
120+
Main main = new Main();
121+
while (true) {
122+
main.benchMustacheSimple();
123+
}
122124
}
123125
}

‎benchmarks/src/main/java/mustachejava/benchmarks/TweetBench.java‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,28 @@
1010
import java.io.IOException;
1111
import java.io.InputStream;
1212
import java.io.StringWriter;
13+
import java.util.ArrayList;
1314
import java.util.HashMap;
1415
import java.util.Iterator;
16+
import java.util.List;
1517
import java.util.concurrent.TimeUnit;
1618

19+
import static java.util.Collections.singletonList;
20+
1721
/**
1822
* Created by sam on 3/19/16.
1923
24+
Initial test 5b9fce4
2025
Benchmark Mode Cnt Score Error Units
2126
TweetBench.testCompilation thrpt 20 8070.287 ± 567.640 ops/s
2227
TweetBench.testExecution thrpt 20 389133.230 ± 15624.712 ops/s
2328
TweetBench.testExecutionWithStringWriter thrpt 20 157934.510 ± 41675.991 ops/s
2429
TweetBench.testJsonExecution thrpt 20 239396.118 ± 11455.727 ops/s
2530
31+
Added recursion and escaping optimization e8df5360
32+
Benchmark Mode Cnt Score Error Units
33+
TweetBench.testExecution thrpt 20 412346.447 ± 19794.264 ops/s
34+
2635
*/
2736
@State(Scope.Benchmark)
2837
public class TweetBench {
@@ -31,15 +40,14 @@ public class TweetBench {
3140
Mustache m = dmf.compile("tweet.mustache");
3241
Tweet tweet = new Tweet();
3342
NullWriter nullWriter = new NullWriter();
34-
InputStream json = TweetBench.class.getClassLoader().getResourceAsStream("tweet.json");
35-
MappingJsonFactory jf = new MappingJsonFactory();
36-
JsonNode jsonNode;
37-
JsonMap jsonMap;
43+
List<Object> tweetScope = new ArrayList<>(singletonList(tweet));
44+
List<Object> jsonScope;
3845

3946
public TweetBench() {
4047
try {
41-
jsonNode = jf.createJsonParser(json).readValueAsTree();
42-
jsonMap = new JsonMap(jsonNode);
48+
MappingJsonFactory jf = new MappingJsonFactory();
49+
InputStream json = TweetBench.class.getClassLoader().getResourceAsStream("tweet.json");
50+
jsonScope = new ArrayList<>(singletonList(new JsonMap(jf.createJsonParser(json).readValueAsTree())));
4351
} catch (IOException e) {
4452
throw new RuntimeException(e);
4553
}
@@ -57,22 +65,22 @@ public void testCompilation() {
5765
@BenchmarkMode(Mode.Throughput)
5866
@OutputTimeUnit(TimeUnit.SECONDS)
5967
public void testExecution() throws IOException {
60-
m.execute(nullWriter, tweet).close();
68+
m.execute(nullWriter, tweetScope).close();
6169
}
6270

6371
@Benchmark
6472
@BenchmarkMode(Mode.Throughput)
6573
@OutputTimeUnit(TimeUnit.SECONDS)
6674
public void testExecutionWithStringWriter() throws IOException {
6775
StringWriter writer = new StringWriter();
68-
m.execute(writer, tweet).close();
76+
m.execute(writer, tweetScope).close();
6977
}
7078

7179
@Benchmark
7280
@BenchmarkMode(Mode.Throughput)
7381
@OutputTimeUnit(TimeUnit.SECONDS)
7482
public void testJsonExecution() throws IOException {
75-
m.execute(nullWriter, jsonMap).close();
83+
m.execute(nullWriter, jsonScope).close();
7684
}
7785

7886
public static void main(String[] args) throws IOException {
@@ -88,6 +96,11 @@ public static void main(String[] args) throws IOException {
8896
sw = new StringWriter();
8997
m.execute(sw, new JsonMap(jsonNode)).close();
9098
System.out.println(sw);
99+
100+
TweetBench tb = new TweetBench();
101+
while (true) {
102+
tb.testExecution();
103+
}
91104
}
92105

93106
private static class JsonMap extends HashMap {

0 commit comments

Comments
 (0)