From 9986e7a2d2d45e24a9f0e3644db285a7f6afd51c Mon Sep 17 00:00:00 2001 From: Hubert Van De Walle Date: Sat, 25 Apr 2020 14:17:52 +0200 Subject: [PATCH 1/2] Add HikaryCP to pool db connections --- .gitignore | 1 + api/http/test.http | 35 ++---------------------- api/pom.xml | 5 ++++ api/src/features/ConfigurationFeature.kt | 16 +++++------ 4 files changed, 17 insertions(+), 40 deletions(-) diff --git a/.gitignore b/.gitignore index 65b00dc..1c226f9 100644 --- a/.gitignore +++ b/.gitignore @@ -118,3 +118,4 @@ dist # Service worker sw.* +*.private.env.json diff --git a/api/http/test.http b/api/http/test.http index f80a543..c32cf85 100644 --- a/api/http/test.http +++ b/api/http/test.http @@ -1,10 +1,10 @@ # Register a new user -POST http://localhost:8081/login +POST http://localhost:8081/user/login Content-Type: application/json { - "username": "hubert", - "password": "test" + "username": "{{username}}", + "password": "{{password}}" } > {% @@ -23,32 +23,3 @@ client.test("Request executed successfully", function() { client.assert(response.status === 200, "Response status is not 200"); }); %} - -### Create a note -POST http://localhost:8081/notes/tortue -Content-Type: application/json -Authorization: Bearer {{token}} - -{ - "tags": [ - "Dev", - "Server" - ] -} - -> {% -client.test("Request executed successfully", function() { - client.assert(response.status === 201, "Response status is not 200"); -}); -%} - -### Read a note -GET http://localhost:8081/notes/babar -Content-Type: application/json -Authorization: Bearer {{token}} - -> {% -client.test("Request executed successfully", function() { - client.assert(response.status === 200, "Response status is not 200"); -}); -%} diff --git a/api/pom.xml b/api/pom.xml index 4dc8a84..622aaba 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -128,6 +128,11 @@ jbcrypt ${jbcrypt_version} + + com.zaxxer + HikariCP + 3.4.2 + ${project.basedir}/src diff --git a/api/src/features/ConfigurationFeature.kt b/api/src/features/ConfigurationFeature.kt index b914ee9..8632233 100644 --- a/api/src/features/ConfigurationFeature.kt +++ b/api/src/features/ConfigurationFeature.kt @@ -1,11 +1,12 @@ package be.vandewalleh.features import be.vandewalleh.auth.SimpleJWT +import com.zaxxer.hikari.HikariConfig +import com.zaxxer.hikari.HikariDataSource import io.ktor.application.* import org.kodein.di.Kodein import org.kodein.di.generic.bind import org.kodein.di.generic.instance -import org.mariadb.jdbc.MariaDbDataSource import javax.sql.DataSource /** @@ -18,15 +19,14 @@ fun Application.configurationFeature() { val host = property("database.host").getString() val port = property("database.port").getString() val name = property("database.name").getString() - val user = property("database.user").getString() - val password = property("database.password").getString() - val url = "jdbc:mariadb://$host:$port/$name" - - MariaDbDataSource(url).apply { - this.user = user - setPassword(password) + val hikariConfig = HikariConfig().apply { + jdbcUrl = "jdbc:mariadb://$host:$port/$name" + username = this@with.property("database.user").getString() + password = this@with.property("database.password").getString() } + + HikariDataSource(hikariConfig) } val simpleJwt = SimpleJWT(environment.config.property("jwt.secret").getString()) From 17d2344dcf4caa91a2607621470a9e1c90f954d9 Mon Sep 17 00:00:00 2001 From: Hubert Van De Walle Date: Sat, 25 Apr 2020 14:39:09 +0200 Subject: [PATCH 2/2] Add performance benchmark to notesRetrieve operation --- api/pom.xml | 15 +++++++ api/resources/logback.xml | 2 +- api/test/NotesRetrievePerformanceTest.kt | 50 ++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 api/test/NotesRetrievePerformanceTest.kt diff --git a/api/pom.xml b/api/pom.xml index 622aaba..b88d1de 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -49,6 +49,16 @@ true + + jitpack + https://jitpack.io + + true + + + true + + @@ -118,6 +128,11 @@ ktorm-support-mysql ${ktorm_version} + + com.github.hekeki + huckleberry + 0.0.2-beta + org.flywaydb flyway-core diff --git a/api/resources/logback.xml b/api/resources/logback.xml index bdbb64e..e573f7d 100644 --- a/api/resources/logback.xml +++ b/api/resources/logback.xml @@ -4,7 +4,7 @@ %d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - + diff --git a/api/test/NotesRetrievePerformanceTest.kt b/api/test/NotesRetrievePerformanceTest.kt new file mode 100644 index 0000000..6998bee --- /dev/null +++ b/api/test/NotesRetrievePerformanceTest.kt @@ -0,0 +1,50 @@ +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 = "test" + password = "test" +} + +val dataSource = HikariDataSource(hikariConfig) + +val db = Database.Companion.connect(dataSource) + +val kodein = Kodein { + bind() with singleton { db } + bind() with singleton { UserService(this.kodein) } + bind() with singleton { NotesService(this.kodein) } +} + +val notesService by kodein.instance() + +@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)) + } + +}