From 986370897a0f19ab39472e4da942a851c16b820f Mon Sep 17 00:00:00 2001 From: KirillKurdyukov Date: Tue, 9 Sep 2025 17:48:59 +0300 Subject: [PATCH 1/3] Test Db Application --- .../src/main/java/tech/ydb/example/Main.java | 2 + jdbc/spring-test-db/pom.xml | 143 ++++++++++++++++++ .../tech/ydb/testdb/TestDbApplication.kt | 49 ++++++ .../src/main/resources/application.properties | 4 + 4 files changed, 198 insertions(+) create mode 100644 jdbc/spring-test-db/pom.xml create mode 100644 jdbc/spring-test-db/src/main/kotlin/tech/ydb/testdb/TestDbApplication.kt create mode 100644 jdbc/spring-test-db/src/main/resources/application.properties diff --git a/auth/service_account_credentials/src/main/java/tech/ydb/example/Main.java b/auth/service_account_credentials/src/main/java/tech/ydb/example/Main.java index 8edf12e..9ccc42f 100644 --- a/auth/service_account_credentials/src/main/java/tech/ydb/example/Main.java +++ b/auth/service_account_credentials/src/main/java/tech/ydb/example/Main.java @@ -13,6 +13,7 @@ public final class Main { public static void main(String[] args) { + long nano = System.nanoTime(); if (args.length != 2) { System.err.println("Usage: java -jar ydb-service-account-example "); return; @@ -38,5 +39,6 @@ public static void main(String[] args) { } } } + System.out.println("ms: " + (System.nanoTime() - nano) / 1_000_000); } } diff --git a/jdbc/spring-test-db/pom.xml b/jdbc/spring-test-db/pom.xml new file mode 100644 index 0000000..49cddc1 --- /dev/null +++ b/jdbc/spring-test-db/pom.xml @@ -0,0 +1,143 @@ + + 4.0.0 + + tech.ydb.jdbc.examples + ydb-jdbc-examples + 1.1.0-SNAPSHOT + + + spring-test-db + Spring Data Test Db Example + + 17 + 1.9.22 + 1.4.0 + 3.2.1 + + + + org.springframework.boot + spring-boot-starter-data-jpa + ${spring.boot.version} + + + org.springframework.data + spring-data-commons + ${spring.boot.version} + + + org.jetbrains.kotlin + kotlin-reflect + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin.version} + + + tech.ydb.dialects + hibernate-ydb-dialect + ${hibernate.ydb.dialect.version} + + + tech.ydb.jdbc + ydb-jdbc-driver + + + com.github.javafaker + javafaker + 1.0.2 + + + org.jetbrains.kotlinx + kotlinx-coroutines-core + 1.7.3 + + + org.postgresql + postgresql + 42.7.3 + + + org.testcontainers + postgresql + 1.19.1 + test + + + + org.springframework.boot + spring-boot-starter-test + ${spring.boot.version} + test + + + tech.ydb.test + ydb-junit5-support + test + + + + ${project.basedir}/src/main/kotlin + ${project.basedir}/src/test/kotlin + + + org.apache.maven.plugins + maven-surefire-plugin + + + true + + + + + org.springframework.boot + spring-boot-maven-plugin + ${spring.boot.version} + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + compile + compile + + compile + + + + test-compile + + test-compile + + + + + 17 + + -Xjsr305=strict + + + spring + jpa + + + + + org.jetbrains.kotlin + kotlin-maven-allopen + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-maven-noarg + ${kotlin.version} + + + + + + \ No newline at end of file diff --git a/jdbc/spring-test-db/src/main/kotlin/tech/ydb/testdb/TestDbApplication.kt b/jdbc/spring-test-db/src/main/kotlin/tech/ydb/testdb/TestDbApplication.kt new file mode 100644 index 0000000..5112837 --- /dev/null +++ b/jdbc/spring-test-db/src/main/kotlin/tech/ydb/testdb/TestDbApplication.kt @@ -0,0 +1,49 @@ +package tech.ydb.testdb + +import jakarta.persistence.EntityManager +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.CommandLineRunner +import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.boot.runApplication +import java.time.Instant + +/** + * @author Kirill Kurdyukov + */ +@SpringBootApplication +class TestDbApplication : CommandLineRunner { + companion object { + private val log = LoggerFactory.getLogger(TestDbApplication::class.java) + } + + @Autowired + lateinit var entityManager: EntityManager + + override fun run(vararg args: String?) { + val testString = String(ByteArray(1024) { 'a'.code.toByte() }) + + val start = Instant.now() + val end = start.plusSeconds(10) + var step = start + var count = 0 + while (step < end) { + getFixedString(testString) + count++ + step = Instant.now() + } + val duration = step.toEpochMilli() - start.toEpochMilli() + + log.info("Throughput = {}", duration.takeIf { it != 0L }?.let { count * 1000.0 / it } ?: 0.0) + } + + fun getFixedString(s: String): String { + return entityManager.createNativeQuery("SELECT ?1") + .setParameter(1, s) + .singleResult.toString() + } +} + +fun main(args: Array) { + runApplication(*args) +} \ No newline at end of file diff --git a/jdbc/spring-test-db/src/main/resources/application.properties b/jdbc/spring-test-db/src/main/resources/application.properties new file mode 100644 index 0000000..73bd993 --- /dev/null +++ b/jdbc/spring-test-db/src/main/resources/application.properties @@ -0,0 +1,4 @@ +spring.jpa.properties.hibernate.dialect=tech.ydb.hibernate.dialect.YdbDialect + +spring.datasource.driver-class-name=tech.ydb.jdbc.YdbDriver +spring.datasource.url=jdbc:ydb:grpc://localhost:2136/local From 60daaf333f06343682ad878579a0362601d635af Mon Sep 17 00:00:00 2001 From: KirillKurdyukov Date: Tue, 9 Sep 2025 18:00:53 +0300 Subject: [PATCH 2/3] Test Db Application --- .../src/main/java/tech/ydb/example/Main.java | 2 -- .../src/main/kotlin/tech/ydb/testdb/TestDbApplication.kt | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/auth/service_account_credentials/src/main/java/tech/ydb/example/Main.java b/auth/service_account_credentials/src/main/java/tech/ydb/example/Main.java index 9ccc42f..8edf12e 100644 --- a/auth/service_account_credentials/src/main/java/tech/ydb/example/Main.java +++ b/auth/service_account_credentials/src/main/java/tech/ydb/example/Main.java @@ -13,7 +13,6 @@ public final class Main { public static void main(String[] args) { - long nano = System.nanoTime(); if (args.length != 2) { System.err.println("Usage: java -jar ydb-service-account-example "); return; @@ -39,6 +38,5 @@ public static void main(String[] args) { } } } - System.out.println("ms: " + (System.nanoTime() - nano) / 1_000_000); } } diff --git a/jdbc/spring-test-db/src/main/kotlin/tech/ydb/testdb/TestDbApplication.kt b/jdbc/spring-test-db/src/main/kotlin/tech/ydb/testdb/TestDbApplication.kt index 5112837..7c0cf79 100644 --- a/jdbc/spring-test-db/src/main/kotlin/tech/ydb/testdb/TestDbApplication.kt +++ b/jdbc/spring-test-db/src/main/kotlin/tech/ydb/testdb/TestDbApplication.kt @@ -34,7 +34,7 @@ class TestDbApplication : CommandLineRunner { } val duration = step.toEpochMilli() - start.toEpochMilli() - log.info("Throughput = {}", duration.takeIf { it != 0L }?.let { count * 1000.0 / it } ?: 0.0) + log.info("Average time = {}ms", 1.0 * duration / count ) } fun getFixedString(s: String): String { From d21c9febb46c3077bd853e39ebd357113a8be16c Mon Sep 17 00:00:00 2001 From: KirillKurdyukov Date: Tue, 9 Sep 2025 18:08:11 +0300 Subject: [PATCH 3/3] Test Db Application --- .../tech/ydb/testdb/TestDbApplication.kt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/jdbc/spring-test-db/src/main/kotlin/tech/ydb/testdb/TestDbApplication.kt b/jdbc/spring-test-db/src/main/kotlin/tech/ydb/testdb/TestDbApplication.kt index 7c0cf79..f8bbee8 100644 --- a/jdbc/spring-test-db/src/main/kotlin/tech/ydb/testdb/TestDbApplication.kt +++ b/jdbc/spring-test-db/src/main/kotlin/tech/ydb/testdb/TestDbApplication.kt @@ -23,18 +23,18 @@ class TestDbApplication : CommandLineRunner { override fun run(vararg args: String?) { val testString = String(ByteArray(1024) { 'a'.code.toByte() }) - val start = Instant.now() - val end = start.plusSeconds(10) - var step = start + var sink = 0 + val t0 = System.nanoTime() + val end = t0 + 10_000_000_000L var count = 0 - while (step < end) { - getFixedString(testString) + while (System.nanoTime() < end) { + sink += getFixedString(testString).length count++ - step = Instant.now() } - val duration = step.toEpochMilli() - start.toEpochMilli() - - log.info("Average time = {}ms", 1.0 * duration / count ) + val elapsed = System.nanoTime() - t0 + val avgMs = elapsed.toDouble() / count / 1_000_000.0 + val opsPerSec = count * 1e9 / elapsed + log.info("avg={} ms/op, throughput={} ops/s", avgMs, opsPerSec) } fun getFixedString(s: String): String {