11package tech.ydb.testdb
22
3- import jakarta.persistence.EntityManager
43import org.slf4j.LoggerFactory
54import org.springframework.beans.factory.annotation.Autowired
65import org.springframework.beans.factory.annotation.Value
76import org.springframework.boot.CommandLineRunner
87import org.springframework.boot.autoconfigure.SpringBootApplication
98import org.springframework.boot.runApplication
10- import java.time.Instant
11- import java.util.concurrent.ExecutorService
12- import java.util.concurrent.Executors
9+ import javax.sql.DataSource
10+ import java.util.Locale
11+ import kotlin.math.floor
1312import kotlin.concurrent.thread
1413
1514/* *
@@ -22,7 +21,7 @@ class TestDbApplication : CommandLineRunner {
2221 }
2322
2423 @Autowired
25- lateinit var entityManager : EntityManager
24+ lateinit var dataSource : DataSource
2625
2726 @Value(" \$ {workers.count}" )
2827 var workersCount: Int = 0
@@ -36,23 +35,29 @@ class TestDbApplication : CommandLineRunner {
3635 val t0 = System .nanoTime()
3736 val end = t0 + 10_000_000_000L
3837 var count = 0
39- while (System .nanoTime() < end) {
40- sink + = getFixedString(testString).length
41- count++
38+ dataSource.connection.use { connection ->
39+ connection.isReadOnly = true
40+ connection.prepareStatement(" SELECT ?" ).use { statement ->
41+ while (System .nanoTime() < end) {
42+ statement.setString(1 , testString)
43+ statement.executeQuery().use { rs ->
44+ if (rs.next()) {
45+ sink + = rs.getString(1 ).length
46+ }
47+ }
48+ count++
49+ }
50+ }
4251 }
4352 val elapsed = System .nanoTime() - t0
4453 val avgMs = elapsed.toDouble() / count / 1_000_000.0
4554 val opsPerSec = count * 1e9 / elapsed
46- log.info(" WorkerNum {}, avg={} ms/op, throughput={} ops/s" , it, avgMs, opsPerSec)
55+ val avgMsStr = String .format(Locale .US , " %.3f" , avgMs)
56+ val opsPerSecInt = floor(opsPerSec).toInt()
57+ log.info(" WorkerNum {}, avg={} ms/op, throughput={} ops/s" , it, avgMsStr, opsPerSecInt)
4758 }
4859 }
4960 }
50-
51- fun getFixedString (s : String ): String {
52- return entityManager.createNativeQuery(" SELECT ?1" )
53- .setParameter(1 , s)
54- .singleResult.toString()
55- }
5661}
5762
5863fun main (args : Array <String >) {
0 commit comments