From ef43af72a3fcc1faed8e8089ef1171d9dff17ebf Mon Sep 17 00:00:00 2001 From: tipsy Date: Mon, 19 Oct 2015 20:22:52 +0200 Subject: [PATCH] Java 7 support, cleanup --- pom.xml | 2 +- src/main/java/j2html/tags/ContainerTag.java | 15 +-- .../java/performancetester/RenderSpeed.java | 97 ------------------- 3 files changed, 4 insertions(+), 110 deletions(-) delete mode 100644 src/test/java/performancetester/RenderSpeed.java diff --git a/pom.xml b/pom.xml index 7819d36..b616ed2 100644 --- a/pom.xml +++ b/pom.xml @@ -40,7 +40,7 @@ - 1.8 + 1.7 4.12 3.4 diff --git a/src/main/java/j2html/tags/ContainerTag.java b/src/main/java/j2html/tags/ContainerTag.java index 9c08527..2a43d13 100644 --- a/src/main/java/j2html/tags/ContainerTag.java +++ b/src/main/java/j2html/tags/ContainerTag.java @@ -45,7 +45,9 @@ public class ContainerTag extends Tag { */ public ContainerTag with(List children) { if (children != null) { - children.forEach(this::with); + for (Tag child : children) { + this.with(child); + } } return this; } @@ -114,17 +116,6 @@ public class ContainerTag extends Tag { */ @Override public String render() { - //very slow -// return renderOpenTag() + children.stream().map(Tag::render).collect(Collectors.joining()) + renderCloseTag(); - - //pretty fast -// StringBuilder rendered = new StringBuilder(renderOpenTag()); -// children.forEach(rendered::append); -// rendered.append(renderCloseTag()); -// return rendered.toString(); - - - //fastest StringBuilder rendered = new StringBuilder(renderOpenTag()); if (children != null && children.size() > 0) { for (Tag child : children) { diff --git a/src/test/java/performancetester/RenderSpeed.java b/src/test/java/performancetester/RenderSpeed.java deleted file mode 100644 index 41d2eab..0000000 --- a/src/test/java/performancetester/RenderSpeed.java +++ /dev/null @@ -1,97 +0,0 @@ -package performancetester; - -import j2html.tags.ContainerTag; -import org.junit.Test; - -import java.util.concurrent.TimeUnit; -import java.util.function.IntConsumer; -import java.util.stream.IntStream; -import java.util.stream.LongStream; - -import static j2html.TagCreator.*; -import static org.junit.Assert.assertEquals; - -public class RenderSpeed { - - /** - * This test class is only used during development of j2html, to see if changes to data-types and rendering - * techniques have a positive or negative impact - */ - - public int iterations = 100000; - public int precision = 10; - - @Test - public void testFullPageRenderSpeed() { - ContainerTag complexTestTag = html().with(body().with(header(), main().with(p("Main stuff...")), footer().condWith(1 == 1, p("Conditional with!")))); - String expectedResult = "

Main stuff...

Conditional with!

"; - assertEquals(complexTestTag.render(), (expectedResult)); - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); - measureAvgMethodTime(this::fullPageRenderTest, iterations, precision, methodName); - } - - @Test - public void testAttributeRenderSpeed() { - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); - measureAvgMethodTime(this::attributeRenderTest, iterations, precision, methodName); - } - - public void attributeRenderTest(int i) { - tag("div " + i) - .attr("just", "adding") - .attr("some", "pretty") - .attr("random", "attributes") - .attr("to", "this") - .attr("div", "here").render(); - } - - public String fullPageRenderTest(int i) { - return html().with( - head().with( - title().withText("Test " + i) - ), - body().with( - header().with( - h1("Test Header") - ), - main().with( - h2("Test Form"), - div().with( - input().withType("email").withName("email").withPlaceholder("Email"), - input().withType("password").withName("password").withPlaceholder("Password"), - input().withType("password").withName("repeatPassword").withPlaceholder("Repeat password"), - input().withType("password").withName("repeatPasswordAgain").withPlaceholder("Repeat password again"), - button().withType("submit").withText("Login") - ), - div().withText( - "" - ) - ), - footer().withText("Test Footer"), - script().withSrc("/testScript.js") - ) - ).render(); - } - - /** - * Please tell me if there is a proper way to do this :D - * - * @param method the method to measure - * @param iterations the number of times to run the method - * @param precision the number of times to run the timer - * @param methodName the name of the method (only used for output) - */ - private void measureAvgMethodTime(IntConsumer method, int iterations, int precision, String methodName) { - double averageTime = LongStream.range(0, precision).map(l -> measureMethodTime(method, iterations)).average().getAsDouble(); - System.out.println("\n" + methodName + ": " + averageTime + "ms was avg runtime per " + String.format("%,d", iterations) + " iterations (ran " + precision + " times) \n"); - } - - private long measureMethodTime(IntConsumer method, int iterations) { - long startTime = System.nanoTime(); - IntStream.range(0, iterations).parallel().forEach(method); - long endTime = System.nanoTime(); - System.out.print(TimeUnit.NANOSECONDS.toMillis(endTime - startTime) + " "); - return TimeUnit.NANOSECONDS.toMillis(endTime - startTime); - } - -}