SimpleNotes/api/test/NotesRetrievePerformanceTest.kt

51 lines
1.5 KiB
Kotlin

import be.vandewalleh.services.NotesService
import be.vandewalleh.services.UserService
import com.hekeki.huckleberry.Benchmark
import com.hekeki.huckleberry.BenchmarkRunner
import com.hekeki.huckleberry.BenchmarkTest
import com.hekeki.huckleberry.TimeUnit
import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import me.liuwj.ktorm.database.*
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import org.kodein.di.Kodein
import org.kodein.di.generic.bind
import org.kodein.di.generic.instance
import org.kodein.di.generic.singleton
val hikariConfig = HikariConfig().apply {
jdbcUrl = "jdbc:mariadb://localhost:3306/notes"
username = "notes"
password = "notes"
}
val dataSource = HikariDataSource(hikariConfig)
val db = Database.Companion.connect(dataSource)
val kodein = Kodein {
bind<Database>() with singleton { db }
bind<UserService>() with singleton { UserService(this.kodein) }
bind<NotesService>() with singleton { NotesService(this.kodein) }
}
val notesService by kodein.instance<NotesService>()
@Benchmark(threads = 1, iterations = 30, warmup = true, warmupIterations = 1000, timeUnit = TimeUnit.MILLIS)
class RetrieveNotesBenchmarkTest : BenchmarkTest {
override fun execute() {
notesService.getNotes(15)
}
@Test
fun compute() {
val benchmarkResult = BenchmarkRunner(RetrieveNotesBenchmarkTest::class.java).run()
assertTrue(benchmarkResult.median(2, TimeUnit.MILLIS))
assertTrue(benchmarkResult.maxTime(4, TimeUnit.MILLIS))
}
}