From 6f6051b7f60a4e324d200ddc3545926a45efca79 Mon Sep 17 00:00:00 2001 From: Evgeniy Ivanov Date: Wed, 10 Sep 2025 13:18:25 +0200 Subject: [PATCH] Reuse prepared query, increase pool size, cleanup --- .../tech/ydb/testdb/TestDbApplication.kt | 35 +++++++++++-------- .../src/main/resources/application.properties | 4 ++- 2 files changed, 23 insertions(+), 16 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 26e1527..575214a 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 @@ -1,15 +1,14 @@ package tech.ydb.testdb -import jakarta.persistence.EntityManager import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Value import org.springframework.boot.CommandLineRunner import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication -import java.time.Instant -import java.util.concurrent.ExecutorService -import java.util.concurrent.Executors +import javax.sql.DataSource +import java.util.Locale +import kotlin.math.floor import kotlin.concurrent.thread /** @@ -22,7 +21,7 @@ class TestDbApplication : CommandLineRunner { } @Autowired - lateinit var entityManager: EntityManager + lateinit var dataSource: DataSource @Value("\${workers.count}") var workersCount: Int = 0 @@ -36,23 +35,29 @@ class TestDbApplication : CommandLineRunner { val t0 = System.nanoTime() val end = t0 + 10_000_000_000L var count = 0 - while (System.nanoTime() < end) { - sink += getFixedString(testString).length - count++ + dataSource.connection.use { connection -> + connection.isReadOnly = true + connection.prepareStatement("SELECT ?").use { statement -> + while (System.nanoTime() < end) { + statement.setString(1, testString) + statement.executeQuery().use { rs -> + if (rs.next()) { + sink += rs.getString(1).length + } + } + count++ + } + } } val elapsed = System.nanoTime() - t0 val avgMs = elapsed.toDouble() / count / 1_000_000.0 val opsPerSec = count * 1e9 / elapsed - log.info("WorkerNum {}, avg={} ms/op, throughput={} ops/s", it, avgMs, opsPerSec) + val avgMsStr = String.format(Locale.US, "%.3f", avgMs) + val opsPerSecInt = floor(opsPerSec).toInt() + log.info("WorkerNum {}, avg={} ms/op, throughput={} ops/s", it, avgMsStr, opsPerSecInt) } } } - - fun getFixedString(s: String): String { - return entityManager.createNativeQuery("SELECT ?1") - .setParameter(1, s) - .singleResult.toString() - } } fun main(args: Array) { diff --git a/jdbc/spring-test-db/src/main/resources/application.properties b/jdbc/spring-test-db/src/main/resources/application.properties index 6e31a7a..0e59f44 100644 --- a/jdbc/spring-test-db/src/main/resources/application.properties +++ b/jdbc/spring-test-db/src/main/resources/application.properties @@ -3,4 +3,6 @@ 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 -workers.count=100 \ No newline at end of file +workers.count=100 +spring.datasource.hikari.maximum-pool-size=200 +spring.datasource.hikari.minimum-idle=100 \ No newline at end of file