diff --git a/src/test/java/j2html/PerformanceTest.java b/src/test/java/j2html/PerformanceTest.java index 5d8f4a2..de33ee7 100644 --- a/src/test/java/j2html/PerformanceTest.java +++ b/src/test/java/j2html/PerformanceTest.java @@ -1,49 +1,59 @@ package j2html; -import java.util.concurrent.Callable; - -import org.apache.commons.lang3.StringEscapeUtils; -import org.junit.Test; - +import com.carrotsearch.junitbenchmarks.BenchmarkOptions; +import com.carrotsearch.junitbenchmarks.BenchmarkRule; +import com.carrotsearch.junitbenchmarks.Clock; import j2html.utils.EscapeUtil; +import org.apache.commons.lang3.StringEscapeUtils; +import org.junit.FixMethodOrder; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TestRule; +import org.junit.runners.MethodSorters; +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@BenchmarkOptions(callgc = false, benchmarkRounds = 10000, warmupRounds = 200, concurrency = 2, clock = Clock.NANO_TIME) public class PerformanceTest { + @Rule + public TestRule benchmarkRun = new BenchmarkRule(); + private String shortTestString = "\n" - + "

Heading!

\n" - + " \n" - + ""; + + "

Heading!

\n" + + " \n" + + ""; // syntax-highlighted getting started example from j2html.com: private String longTestString = - "
import static j2html.TagCreator.*;\n"
-                    + "\n"
-                    + "public class Main {\n"
-                    + "    public static void main(String[] args) {\n"
-                    + "        body().with(\n"
-                    + "                h1(\"Heading!\").withClass(\"example\"),\n"
-                    + "                img().withSrc(\"img/hello.png\")\n"
-                    + "        ).render();\n"
-                    + "    }\n"
-                    + "}\n"
-                    + "
"; + "
import static j2html.TagCreator.*;\n"
+            + "\n"
+            + "public class Main {\n"
+            + "    public static void main(String[] args) {\n"
+            + "        body().with(\n"
+            + "                h1(\"Heading!\").withClass(\"example\"),\n"
+            + "                img().withSrc(\"img/hello.png\")\n"
+            + "        ).render();\n"
+            + "    }\n"
+            + "}\n"
+            + "
"; @Test - public void test_escaper_performnce() throws Exception { - timeEscaper("SimpleEscaper#short", () -> EscapeUtil.escape(shortTestString)); - timeEscaper("SimpleEscaper#long", () -> EscapeUtil.escape(longTestString)); - timeEscaper("ApacheEscaper#short", () -> StringEscapeUtils.escapeHtml4(shortTestString)); - timeEscaper("ApacheEscaper#long", () -> StringEscapeUtils.escapeHtml4(longTestString)); + public void testSimpleEscaperShort() throws Exception { + EscapeUtil.escape(shortTestString); } - private void timeEscaper(String name, Callable escaper) throws Exception { - long startTime = System.nanoTime(); - for (int i = 0; i < 1000; i++) { - escaper.call(); - } - long endTime = System.nanoTime(); - long durationMs = (endTime - startTime) / 1000000; - System.out.println(String.format("%-21s%s", name + ":", +durationMs + "ms")); + @Test + public void testSimpleEscaperLong() throws Exception { + EscapeUtil.escape(longTestString); } + @Test + public void testApacheEscaperShort() throws Exception { + StringEscapeUtils.escapeHtml4(shortTestString); + } + + @Test + public void testApacheEscaperLong() throws Exception { + StringEscapeUtils.escapeHtml4(longTestString); + } }