Skip to content

Commit 3b4302a

Browse files
committed
Review and port some of the changes in FrameworkBenchmarks/frameworks/Java/vertx/ from commit 057c25b to commit 1838aa5, and fix some typos
Some changes are not ported. There is about 5% performance improvement in the JSON test as tested on my device.
1 parent d084060 commit 3b4302a

File tree

4 files changed

+58
-28
lines changed

4 files changed

+58
-28
lines changed

frameworks/Kotlin/vertx-web-kotlinx/src/main/kotlin/Main.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,26 @@ import java.util.function.Supplier
77
import java.util.logging.Logger
88

99
const val SERVER_NAME = "Vert.x-Web Kotlinx Benchmark server"
10+
val numProcessors = CpuCoreSensor.availableProcessors()
1011

1112
val logger = Logger.getLogger("Vert.x-Web Kotlinx Benchmark")
1213
suspend fun main(args: Array<String>) {
1314
val hasDb = args.getOrNull(0)?.toBooleanStrictOrNull()
1415
?: throw IllegalArgumentException("Specify the first `hasDb` Boolean argument")
1516

1617
logger.info("$SERVER_NAME starting...")
17-
val vertx = Vertx.vertx(vertxOptionsOf(preferNativeTransport = true))
18+
val vertx = Vertx.vertx(
19+
vertxOptionsOf(
20+
eventLoopPoolSize = numProcessors, preferNativeTransport = true, disableTCCL = true
21+
)
22+
)
1823
vertx.exceptionHandler {
1924
logger.info("Vertx exception caught: $it")
2025
it.printStackTrace()
2126
}
2227
vertx.deployVerticle(
2328
Supplier { MainVerticle(hasDb) },
24-
deploymentOptionsOf(instances = CpuCoreSensor.availableProcessors())
29+
deploymentOptionsOf(instances = numProcessors)
2530
).coAwait()
2631
logger.info("$SERVER_NAME started.")
2732
}

frameworks/Kotlin/vertx-web-kotlinx/src/main/kotlin/MainVerticle.kt

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import io.netty.channel.unix.Errors.NativeIoException
2+
import io.vertx.core.MultiMap
23
import io.vertx.core.buffer.Buffer
34
import io.vertx.core.http.HttpHeaders
45
import io.vertx.core.http.HttpServer
@@ -18,11 +19,9 @@ import io.vertx.sqlclient.Row
1819
import io.vertx.sqlclient.RowSet
1920
import io.vertx.sqlclient.Tuple
2021
import kotlinx.coroutines.Dispatchers
21-
import kotlinx.datetime.TimeZone
2222
import kotlinx.datetime.UtcOffset
2323
import kotlinx.datetime.format.DateTimeComponents
2424
import kotlinx.datetime.format.format
25-
import kotlinx.datetime.toLocalDateTime
2625
import kotlinx.html.*
2726
import kotlinx.html.stream.appendHTML
2827
import kotlinx.io.buffered
@@ -34,14 +33,21 @@ import kotlinx.serialization.json.io.encodeToSink
3433
import kotlin.time.Clock
3534

3635
class MainVerticle(val hasDb: Boolean) : CoroutineVerticle(), CoroutineRouterSupport {
36+
object HttpHeaderValues {
37+
val vertxWeb = HttpHeaders.createOptimized("Vert.x-Web")
38+
val applicationJson = HttpHeaders.createOptimized("application/json")
39+
val textHtmlCharsetUtf8 = HttpHeaders.createOptimized("text/html; charset=utf-8")
40+
val textPlain = HttpHeaders.createOptimized("text/plain")
41+
}
42+
3743
// `PgConnection`s as used in the "vertx" portion offers better performance than `PgPool`s.
3844
lateinit var pgConnection: PgConnection
3945
lateinit var date: String
4046
lateinit var httpServer: HttpServer
4147

4248
lateinit var selectWorldQuery: PreparedQuery<RowSet<Row>>
4349
lateinit var selectFortuneQuery: PreparedQuery<RowSet<Row>>
44-
lateinit var updateWordQuery: PreparedQuery<RowSet<Row>>
50+
lateinit var updateWorldQuery: PreparedQuery<RowSet<Row>>
4551

4652
fun setCurrentDate() {
4753
date = DateTimeComponents.Formats.RFC_1123.format {
@@ -61,18 +67,20 @@ class MainVerticle(val hasDb: Boolean) : CoroutineVerticle(), CoroutineRouterSup
6167
user = "benchmarkdbuser",
6268
password = "benchmarkdbpass",
6369
cachePreparedStatements = true,
64-
pipeliningLimit = 100000
70+
pipeliningLimit = 256
6571
)
6672
).coAwait()
6773

6874
selectWorldQuery = pgConnection.preparedQuery(SELECT_WORLD_SQL)
6975
selectFortuneQuery = pgConnection.preparedQuery(SELECT_FORTUNE_SQL)
70-
updateWordQuery = pgConnection.preparedQuery(UPDATE_WORLD_SQL)
76+
updateWorldQuery = pgConnection.preparedQuery(UPDATE_WORLD_SQL)
7177
}
7278

7379
setCurrentDate()
7480
vertx.setPeriodic(1000) { setCurrentDate() }
75-
httpServer = vertx.createHttpServer(httpServerOptionsOf(port = 8080))
81+
httpServer = vertx.createHttpServer(
82+
httpServerOptionsOf(port = 8080, http2ClearTextEnabled = false, strictThreadMode = true)
83+
)
7684
.requestHandler(Router.router(vertx).apply { routes() })
7785
.exceptionHandler {
7886
// wrk resets the connections when benchmarking is finished.
@@ -98,15 +106,17 @@ class MainVerticle(val hasDb: Boolean) : CoroutineVerticle(), CoroutineRouterSup
98106
}
99107

100108
@Suppress("NOTHING_TO_INLINE")
101-
inline fun HttpServerResponse.putCommonHeaders() {
102-
putHeader(HttpHeaders.SERVER, "Vert.x-Web")
103-
putHeader(HttpHeaders.DATE, date)
109+
inline fun MultiMap.addCommonHeaders() {
110+
add(HttpHeaders.SERVER, HttpHeaderValues.vertxWeb)
111+
add(HttpHeaders.DATE, date)
104112
}
105113

106114
@Suppress("NOTHING_TO_INLINE")
107-
inline fun HttpServerResponse.putJsonResponseHeader() {
108-
putCommonHeaders()
109-
putHeader(HttpHeaders.CONTENT_TYPE, "application/json")
115+
inline fun HttpServerResponse.addJsonResponseHeaders() {
116+
headers().run {
117+
addCommonHeaders()
118+
add(HttpHeaders.CONTENT_TYPE, HttpHeaderValues.applicationJson)
119+
}
110120
}
111121

112122

@@ -122,7 +132,7 @@ class MainVerticle(val hasDb: Boolean) : CoroutineVerticle(), CoroutineRouterSup
122132
) =
123133
coHandlerUnconfined {
124134
it.response().run {
125-
putJsonResponseHeader()
135+
addJsonResponseHeaders()
126136

127137
/*
128138
// approach 1
@@ -202,20 +212,23 @@ class MainVerticle(val hasDb: Boolean) : CoroutineVerticle(), CoroutineRouterSup
202212
}
203213

204214
it.response().run {
205-
putCommonHeaders()
206-
putHeader(HttpHeaders.CONTENT_TYPE, "text/html; charset=utf-8")
215+
headers().run {
216+
addCommonHeaders()
217+
add(HttpHeaders.CONTENT_TYPE, HttpHeaderValues.textHtmlCharsetUtf8)
218+
}
207219
end(htmlString)/*.coAwait()*/
208220
}
209221
}
210222

223+
// Some changes to this part in the `vertx` portion in #9142 are not ported.
211224
get("/updates").jsonResponseCoHandler(Serializers.worlds) {
212225
val queries = it.request().getQueries()
213226
val worlds = selectRandomWorlds(queries)
214227
val updatedWorlds = worlds.map { it.copy(randomNumber = randomIntBetween1And10000()) }
215228

216229
// Approach 1
217230
// The updated worlds need to be sorted first to avoid deadlocks.
218-
updateWordQuery
231+
updateWorldQuery
219232
.executeBatch(updatedWorlds.sortedBy { it.id }.map { Tuple.of(it.randomNumber, it.id) }).coAwait()
220233

221234
/*
@@ -230,8 +243,10 @@ class MainVerticle(val hasDb: Boolean) : CoroutineVerticle(), CoroutineRouterSup
230243

231244
get("/plaintext").coHandlerUnconfined {
232245
it.response().run {
233-
putCommonHeaders()
234-
putHeader(HttpHeaders.CONTENT_TYPE, "text/plain")
246+
headers().run {
247+
addCommonHeaders()
248+
add(HttpHeaders.CONTENT_TYPE, HttpHeaderValues.textPlain)
249+
}
235250
end("Hello, World!")/*.coAwait()*/
236251
}
237252
}

frameworks/Kotlin/vertx-web-kotlinx/vertx-web-kotlinx-postgresql.dockerfile

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,22 @@ RUN gradle --no-daemon installDist
1010
EXPOSE 8080
1111

1212
CMD export JAVA_OPTS=" \
13+
--enable-native-access=ALL-UNNAMED \
14+
--sun-misc-unsafe-memory-access=allow \
15+
--add-opens=java.base/java.lang=ALL-UNNAMED \
1316
-server \
1417
-XX:+UseNUMA \
1518
-XX:+UseParallelGC \
19+
-XX:+UnlockDiagnosticVMOptions \
20+
-XX:+DebugNonSafepoints \
21+
-Djava.lang.Integer.IntegerCache.high=10000 \
1622
-Dvertx.disableMetrics=true \
17-
-Dvertx.disableH2c=true \
1823
-Dvertx.disableWebsockets=true \
19-
-Dvertx.flashPolicyHandler=false \
20-
-Dvertx.threadChecks=false \
2124
-Dvertx.disableContextTimings=true \
22-
-Dvertx.disableTCCL=true \
2325
-Dvertx.disableHttpHeadersValidation=true \
26+
-Dvertx.cacheImmutableHttpResponseHeaders=true \
27+
-Dvertx.internCommonHttpRequestHeadersToLowerCase=true \
28+
-Dio.netty.noUnsafe=false \
2429
-Dio.netty.buffer.checkBounds=false \
2530
-Dio.netty.buffer.checkAccessible=false \
2631
" && \

frameworks/Kotlin/vertx-web-kotlinx/vertx-web-kotlinx.dockerfile

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,22 @@ RUN gradle --no-daemon installDist
1010
EXPOSE 8080
1111

1212
CMD export JAVA_OPTS=" \
13+
--enable-native-access=ALL-UNNAMED \
14+
--sun-misc-unsafe-memory-access=allow \
15+
--add-opens=java.base/java.lang=ALL-UNNAMED \
1316
-server \
1417
-XX:+UseNUMA \
1518
-XX:+UseParallelGC \
19+
-XX:+UnlockDiagnosticVMOptions \
20+
-XX:+DebugNonSafepoints \
21+
-Djava.lang.Integer.IntegerCache.high=10000 \
1622
-Dvertx.disableMetrics=true \
17-
-Dvertx.disableH2c=true \
1823
-Dvertx.disableWebsockets=true \
19-
-Dvertx.flashPolicyHandler=false \
20-
-Dvertx.threadChecks=false \
2124
-Dvertx.disableContextTimings=true \
22-
-Dvertx.disableTCCL=true \
2325
-Dvertx.disableHttpHeadersValidation=true \
26+
-Dvertx.cacheImmutableHttpResponseHeaders=true \
27+
-Dvertx.internCommonHttpRequestHeadersToLowerCase=true \
28+
-Dio.netty.noUnsafe=false \
2429
-Dio.netty.buffer.checkBounds=false \
2530
-Dio.netty.buffer.checkAccessible=false \
2631
" && \

0 commit comments

Comments
 (0)