Skip to content

Commit

Permalink
benchmarks setup
Browse files Browse the repository at this point in the history
  • Loading branch information
whyoleg committed May 25, 2024
1 parent ca5cce8 commit 0f89b16
Show file tree
Hide file tree
Showing 21 changed files with 1,161 additions and 370 deletions.
116 changes: 0 additions & 116 deletions benchmarks/build.gradle.kts

This file was deleted.

67 changes: 67 additions & 0 deletions benchmarks/rsocket-java/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2015-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import rsocketbuild.*

plugins {
id("rsocketbuild.multiplatform-base")
alias(libs.plugins.kotlin.plugin.allopen)
alias(libs.plugins.kotlinx.benchmark)
}

kotlin {
jvmTarget()

sourceSets {
jvmMain.dependencies {
implementation(projects.benchmarksShared)

implementation(libs.kotlinx.coroutines.reactor)
implementation(libs.rsocket.java.transport.local)
implementation(libs.rsocket.java.transport.netty)
}
}
}

allOpen {
annotation("org.openjdk.jmh.annotations.State")
}

benchmark {
targets {
register("jvm") {
this as kotlinx.benchmark.gradle.JvmBenchmarkTarget
jmhVersion = libs.versions.jmh.get()
}
}
configurations {
configureEach {
reportFormat = "text"
}
named("main") {
// all params
param("payloadSize", "0", "64", "1024", "131072", "1048576")
}
register("localPayloadSize") {
include("LocalRSocketJavaBenchmark")
param("payloadSize", "0", "64")
}
register("tcpPayloadSize") {
include("TcpRSocketJavaBenchmark")
param("payloadSize", "0", "64")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2015-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.rsocket.kotlin.benchmarks.java

import io.rsocket.transport.*
import io.rsocket.transport.local.*
import kotlinx.benchmark.*
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*

@BenchmarkMode(Mode.Throughput)
@Warmup(iterations = 3, time = 1)
@Measurement(iterations = 3, time = 1)
@State(Scope.Benchmark)
class LocalRSocketJavaBenchmark : RSocketJavaBenchmark() {
@Param("0")
override var payloadSize: Int = 0

override val serverTransport: ServerTransport<*> = LocalServerTransport.create("local")
override val clientTransport: ClientTransport = LocalClientTransport.create("local")
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2022 the original author or authors.
* Copyright 2015-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,32 +14,52 @@
* limitations under the License.
*/

package io.rsocket.kotlin.benchmarks
package io.rsocket.kotlin.benchmarks.java

import io.rsocket.*
import io.rsocket.core.*
import io.rsocket.frame.decoder.*
import io.rsocket.transport.local.*
import io.rsocket.kotlin.benchmarks.*
import io.rsocket.transport.*
import io.rsocket.util.*
import kotlinx.benchmark.*
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.reactive.*
import org.reactivestreams.*
import reactor.core.publisher.*
import kotlin.random.*

class RSocketJavaBenchmark : RSocketBenchmark<Payload>() {
abstract class RSocketJavaBenchmark : RSocketBenchmark<Payload, Blackhole>() {
protected abstract val clientTransport: ClientTransport
protected abstract val serverTransport: ServerTransport<*>

lateinit var client: RSocket
lateinit var server: Closeable
private lateinit var payload: Payload
private lateinit var payloadMono: Mono<Payload>
private lateinit var payloadsFlux: Flux<Payload>
private lateinit var payloadsFlow: Flow<Payload>
private lateinit var client: RSocket
private lateinit var server: Closeable

lateinit var payload: Payload
lateinit var payloadMono: Mono<Payload>
lateinit var payloadsFlux: Flux<Payload>
lateinit var payloadsFlow: Flow<Payload>
override fun createPayload(size: Int): Payload = if (size == 0) EmptyPayload.INSTANCE else ByteBufPayload.create(
ByteArray(size / 2).also { Random.nextBytes(it) },
ByteArray(size / 2).also { Random.nextBytes(it) }
)

override fun createPayloadCopy(): Payload = payload.retain()

override fun releasePayload(payload: Payload) {
payload.release()
}

override fun consumePayload(bh: Blackhole, value: Payload) = bh.consume(value)

override suspend fun doRequestResponse(): Payload = client.requestResponse(payload.retain()).awaitSingle()
override fun doRequestStream(): Flow<Payload> = client.requestStream(payload.retain()).asFlow()
override fun doRequestChannel(): Flow<Payload> = client.requestChannel(payloadsFlow.asPublisher()).asFlow()

@Setup
override fun setup() {
payload = createPayload(payloadSize)

payloadMono = Mono.fromSupplier(payload::retain)
payloadsFlux = Flux.range(0, 5000).map { payload.retain() }
payloadsFlow = flow { repeat(5000) { emit(payload.retain()) } }
Expand All @@ -61,33 +81,27 @@ class RSocketJavaBenchmark : RSocketBenchmark<Payload>() {
})
}
.payloadDecoder(PayloadDecoder.ZERO_COPY)
.bind(LocalServerTransport.create("server"))
.bind(serverTransport)
.block()!!

client = RSocketConnector.create()
.payloadDecoder(PayloadDecoder.ZERO_COPY)
.connect(LocalClientTransport.create("server"))
.connect(clientTransport)
.block()!!
}

@TearDown
override fun cleanup() {
client.dispose()
server.dispose()
}

override fun createPayload(size: Int): Payload = if (size == 0) EmptyPayload.INSTANCE else ByteBufPayload.create(
ByteArray(size / 2).also { Random.nextBytes(it) },
ByteArray(size / 2).also { Random.nextBytes(it) }
)

override fun releasePayload(payload: Payload) {
payload.release()
}

override suspend fun doRequestResponse(): Payload = client.requestResponse(payload.retain()).awaitSingle()

override suspend fun doRequestStream(): Flow<Payload> = client.requestStream(payload.retain()).asFlow()
@Benchmark
override fun requestResponseBlocking(bh: Blackhole) = super.requestResponseBlocking(bh)

override suspend fun doRequestChannel(): Flow<Payload> = client.requestChannel(payloadsFlow.asPublisher()).asFlow()
@Benchmark
override fun requestResponseParallel(bh: Blackhole) = super.requestResponseParallel(bh)

@Benchmark
override fun requestResponseConcurrent(bh: Blackhole) = super.requestResponseConcurrent(bh)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2015-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.rsocket.kotlin.benchmarks.java

import io.rsocket.transport.*
import io.rsocket.transport.local.*
import io.rsocket.transport.netty.client.*
import io.rsocket.transport.netty.server.*
import kotlinx.benchmark.*
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*

@BenchmarkMode(Mode.Throughput)
@Warmup(iterations = 3, time = 1)
@Measurement(iterations = 3, time = 1)
@State(Scope.Benchmark)
class TcpRSocketJavaBenchmark : RSocketJavaBenchmark() {
@Param("0")
override var payloadSize: Int = 0

override val serverTransport: ServerTransport<*> = TcpServerTransport.create(9000)
override val clientTransport: ClientTransport = TcpClientTransport.create(9000)
}
Loading

0 comments on commit 0f89b16

Please sign in to comment.