Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include kotlin-html-builder in benchmark #4

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ target/
.project
/.idea/*
.vscode
results.csv
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ JMH benchmark for popular Java template engines:
Running the benchmark
======================

1. Download the source code and build it (`mvn clean install`)
1. Download the source code and build it (`mvn clean package`)
2. (Optional) To run a benchmark for a single template, such as Mustache, use `java -jar target/benchmarks.jar Mustache`
3. Run the benchmark for `presentations` or `stocks` workload. E.g. for : `presentations`
```bash
Expand Down
13 changes: 12 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@
<hbs.version>4.3.1</hbs.version>
<rocker.version>1.4.0</rocker.version>

<kotlin.version>1.8.20</kotlin.version>
<kotlin.compiler.jvmTarget>21</kotlin.compiler.jvmTarget>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>

<kotlin.version>2.0.0</kotlin.version>
<kotlinx.html.version>0.8.0</kotlinx.html.version>
<kotlin.html.builder.version>0.4.0</kotlin.html.builder.version>
</properties>

<licenses>
Expand Down Expand Up @@ -233,6 +238,12 @@
<version>${kotlinx.html.version}</version>
</dependency>

<dependency>
<groupId>nu.staldal</groupId>
<artifactId>kotlin-html-builder</artifactId>
<version>${kotlin.html.builder.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/mitchellbosecke/benchmark/KotlinHtmlBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.mitchellbosecke.benchmark;

import com.mitchellbosecke.benchmark.model.Presentation;
import com.mitchellbosecke.benchmark.model.Stock;
import com.mitchellbosecke.benchmark.templates.PresentationsKotlin;
import com.mitchellbosecke.benchmark.templates.PresentationsKotlinHtmlBuilder;
import com.mitchellbosecke.benchmark.templates.StocksKotlin;
import com.mitchellbosecke.benchmark.templates.StocksKotlinHtmlBuilder;
import org.openjdk.jmh.annotations.Benchmark;

import java.io.IOException;
import java.util.Collection;
import java.util.List;

public class KotlinHtmlBuilder extends BaseBenchmark {

private List<Stock> stocks = Stock.dummyItems();
private final Collection<Presentation> presentations = Presentation.dummyItems();

@Benchmark
public String stocks(){
return StocksKotlinHtmlBuilder.Companion.stocksTemplate(stocks);
}

@Benchmark
public String presentations() throws IOException {
return PresentationsKotlinHtmlBuilder.Companion.presentationsTemplate(presentations);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.mitchellbosecke.benchmark.templates

import com.mitchellbosecke.benchmark.model.Presentation
import nu.staldal.kotlin.html.*

class PresentationsKotlinHtmlBuilder {
companion object {

fun presentationsTemplate(presentations : Collection<Presentation> ): String {
return htmlDoc(prettyPrint = false) {
html {
head {
meta("charset" to "utf-8")
meta("name" to "viewport", "content" to "width=device-width, initial-scale=1.0")
meta("httpEquiv" to "contentLanguage", "content" to "IE=Edge")
title {
text("JFall 2013 Presentations - htmlApi")
}
link(
"rel" to "stylesheet",
"href" to "/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css",
"media" to "screen"
)
}
body {
div("class" to "container") {
div("class" to "page-header") {
h1 { text("JFall 2013 Presentations - htmlApi") }
}

presentations.forEach {
div("class" to "panel panel-default") {
div("class" to "panel-heading") {
h3("class" to "panel-title") {
text(it.title + " - " + it.speakerName)
}
}
div("class" to "panel-body") {
text(it.summary)
}
}
}
}

script("src" to "/webjars/jquery/3.1.1/jquery.min.js")
script("src" to "/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js")
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.mitchellbosecke.benchmark.templates

import com.mitchellbosecke.benchmark.model.Stock
import nu.staldal.kotlin.html.*

class StocksKotlinHtmlBuilder {
companion object {

fun stocksTemplate(stocks : List<Stock> ): String {
return htmlDoc(prettyPrint = false) {
html {
head {
title { text("Stock Prices") }
meta("httpEquiv" to "contentType", "content" to "text/html; charset=UTF-8")
meta("httpEquiv" to "Content-Style-Type", "content" to "text/css")
meta("httpEquiv" to "Content-Script-Type", "content" to "text/javascript")
link("rel" to "shortcut icon", "href" to "/images/favicon.ico")
link("rel" to "stylesheet", "type" to "text/css", "href" to "/css/style.css", "media" to "all")
script("type" to "text/javascript", "src" to "/js/util.js")
style("type" to "text/css") {
unsafe("[body { color: #333333; line-height: 150%;}thead { font-weight: bold; background-color: #CCCCCC;}.odd { background-color: #FFCCCC;}.even { background-color: #CCCCFF;}.minus { color: #FF0000;}]]>")
}
}
body {
h1 { text("Stock Prices") }
table {
thead {
tr {
th { text("#") }
th { text("symbol") }
th { text("name") }
th { text("price") }
th { text("change") }
th { text("ratio") }
}
}
tbody {
var index = 0
stocks.forEach {
tr("class" to if (index % 2 == 0) "even" else "odd") {
td { index++ }
td { a("href" to it.url) { text(it.name) } }
td { strong { text(it.price.toString()) } }
td(*if (it.change < 0) arrayOf("class" to "minus") else arrayOf()) {
text(it.change.toString())
}
td(*if (it.ratio < 0) arrayOf("class" to "minus") else arrayOf()) {
text(it.ratio.toString())
}
}
}
}
}
}
}
}
}
}
}