瀏覽代碼

http4k - Revert db connection pool change, remove asyncdb implementation as not performant (#4414)

* Revert db connection pool change, remove asyncdb implementation as clearly not performant.

* convert mapNotNull to map as is not required
David Denton 6 年之前
父節點
當前提交
19b02044ff

+ 0 - 15
frameworks/Kotlin/http4k/apache-asyncdb/build.gradle

@@ -1,15 +0,0 @@
-dependencies {
-    compile project(":core")
-    compile "org.http4k:http4k-server-apache:$http4k_version"
-}
-
-apply plugin: 'application'
-mainClassName = "Http4kApacheAsyncDbServerKt"
-apply plugin: 'com.github.johnrengelman.shadow'
-
-shadowJar {
-    baseName = "http4k-$project.name-benchmark"
-    classifier = null
-    version = null
-    mergeServiceFiles()
-}

+ 0 - 5
frameworks/Kotlin/http4k/apache-asyncdb/src/main/kotlin/Http4kApacheAsyncDbServer.kt

@@ -1,5 +0,0 @@
-import org.http4k.server.ApacheServer
-
-fun main(args: Array<String>) {
-    Http4kBenchmarkServer(ReactivePostgresDatabase("tfb-database")).start(ApacheServer(9000))
-}

+ 0 - 21
frameworks/Kotlin/http4k/benchmark_config.json

@@ -86,27 +86,6 @@
         "notes": "",
         "versus": "servlet"
       },
-      "apache-asyncdb": {
-        "orm": "Raw",
-        "database_os": "Linux",
-        "db_url": "/db",
-        "fortune_url": "/fortunes",
-        "query_url": "/queries?queries=",
-        "update_url": "/updates?queries=",
-        "database": "Postgres",
-        "json_url": "/json",
-        "plaintext_url": "/plaintext",
-        "port": 9000,
-        "approach": "Realistic",
-        "classification": "Micro",
-        "framework": "http4k",
-        "language": "Kotlin",
-        "platform": "apache-httpcore",
-        "webserver": "None",
-        "os": "Linux",
-        "notes": "",
-        "versus": "servlet"
-      },
       "ktorcio": {
         "orm": "Raw",
         "database_os": "Linux",

+ 0 - 1
frameworks/Kotlin/http4k/build.gradle

@@ -40,7 +40,6 @@ allprojects {
 dependencies {
     compile project(":core")
     compile project(":apache")
-    compile project(":apache-asyncdb")
     compile project(":jetty")
     compile project(":ktorcio")
     compile project(":netty")

+ 12 - 100
frameworks/Kotlin/http4k/core/src/main/kotlin/Database.kt

@@ -1,16 +1,12 @@
 import com.fasterxml.jackson.databind.JsonNode
 import com.zaxxer.hikari.HikariConfig
 import com.zaxxer.hikari.HikariDataSource
-import io.reactiverse.pgclient.PgClient.pool
-import io.reactiverse.pgclient.PgPool
-import io.reactiverse.pgclient.PgPoolOptions
-import io.reactiverse.pgclient.Tuple
 import org.http4k.format.Jackson.number
 import org.http4k.format.Jackson.obj
 import java.sql.Connection
 import java.sql.PreparedStatement
+import java.sql.ResultSet
 import java.util.Random
-import java.util.concurrent.CompletableFuture
 import javax.sql.DataSource
 
 interface Database {
@@ -45,21 +41,13 @@ class PostgresDatabase private constructor(private val dataSource: DataSource) :
     }
 
     override fun fortunes() = withConnection {
-        val original = withStatement("select * from fortune") {
-            with(executeQuery()) {
-                mutableListOf<Fortune>().apply {
-                    while (next()) {
-                        add(Fortune(getInt(1), getString(2)))
-                    }
-                }
-            }
-        }
+        val original = withStatement("select * from fortune") { executeQuery().toResultsList { Fortune(getInt(1), getString(2)) } }
         (original + Fortune(0, "Additional fortune added at request time.")).sortedBy { it.message }
     }
 
     companion object {
-        operator fun invoke(host: String): Database {
-            val dataSource = HikariConfig().run {
+        operator fun invoke(host: String): Database =
+            PostgresDatabase(HikariConfig().run {
                 username = "benchmarkdbuser"
                 password = "benchmarkdbpass"
                 jdbcUrl = "jdbc:postgresql://$host:5432/hello_world?" +
@@ -79,11 +67,9 @@ class PostgresDatabase private constructor(private val dataSource: DataSource) :
                     "maintainTimeStats=false&" +
                     "useServerPrepStmts=true&" +
                     "cacheRSMetadata=true"
-                maximumPoolSize = Runtime.getRuntime().availableProcessors() * 2
+                maximumPoolSize = 100
                 HikariDataSource(this)
-            }
-            return PostgresDatabase(dataSource)
-        }
+            })
     }
 
     private inline fun <T> withConnection(fn: Connection.() -> T): T = dataSource.connection.use(fn)
@@ -93,91 +79,17 @@ class PostgresDatabase private constructor(private val dataSource: DataSource) :
     private fun Connection.findWorld(id: Int) =
         withStatement("SELECT * FROM world WHERE id = ?") {
             setInt(1, id)
-            with(executeQuery()) {
-                next()
+            executeQuery().toResultsList {
                 obj("id" to number(getInt("id")), "randomNumber" to number(getInt("randomNumber")))
-            }
-        }
-}
-
-class ReactivePostgresDatabase private constructor(private val db: PgPool) : Database {
-    companion object {
-        operator fun invoke(hostName: String): Database {
-            val options = PgPoolOptions().apply {
-                database = "hello_world"
-                host = hostName
-                port = 5432
-                user = "benchmarkdbuser"
-                password = "benchmarkdbpass"
-                maxSize = Runtime.getRuntime().availableProcessors() * 2
-                cachePreparedStatements = true
-            }
-            return ReactivePostgresDatabase(pool(PgPoolOptions(options)))
-        }
-    }
-
-    override fun findWorld(): JsonNode {
-        val deferred = CompletableFuture<JsonNode>()
-        db.preparedQuery("SELECT id, randomnumber from WORLD where id=$1", Tuple.of(randomWorld())) {
-            with(it.result().first()) {
-                deferred.complete(obj("id" to number(getInteger(0)), "randomNumber" to number(getInteger(1))))
-            }
+            }.first()
         }
-        return deferred.get()
-    }
-
-    override fun findWorlds(count: Int): List<JsonNode> {
-        val deferred = CompletableFuture<List<JsonNode>>()
-        val worlds = mutableListOf<JsonNode>()
-
-        (1..count).forEach {
-            db.preparedQuery("SELECT id, randomnumber from WORLD where id=$1", Tuple.of(randomWorld())) {
-                with(it.result().first()) {
-                    worlds.add(obj("id" to number(getInteger(0)), "randomNumber" to number(getInteger(1))))
-                }
-                if (worlds.size == count) deferred.complete(worlds)
-            }
-        }
-
-        return deferred.get()
-    }
-
-    override fun updateWorlds(count: Int): List<JsonNode> {
-        val deferred = CompletableFuture<List<JsonNode>>()
-        val worlds = mutableListOf<Tuple>()
-        (1..count).forEach {
-            db.preparedQuery("SELECT id from WORLD where id=$1", Tuple.of(randomWorld())) { ar ->
-                with(ar.result().first()) {
-                    worlds.add(Tuple.of(getInteger(0), randomWorld()))
 
-                    if (worlds.size == count) {
-                        db.preparedBatch("UPDATE world SET randomnumber=$1 WHERE id=$2", worlds) {
-                            deferred.complete(worlds.map {
-                                obj("id" to number(it.getInteger(0)), "randomNumber" to number(it.getInteger(1)))
-                            })
-                        }
-                    }
-                }
+    private inline fun <T> ResultSet.toResultsList(fn: ResultSet.() -> T): List<T> =
+        mutableListOf<T>().apply {
+            while (next()) {
+                add(fn(this@toResultsList))
             }
         }
-        return deferred.get()
-    }
-
-    override fun fortunes(): List<Fortune> {
-        val deferred = CompletableFuture<List<Fortune>>()
-        val fortunes = mutableListOf<Fortune>()
-
-        db.preparedQuery("SELECT id, message from FORTUNE") {
-            with(it.result().iterator()) {
-                while (hasNext()) {
-                    with(next()) { fortunes.add(Fortune(getInteger(0), getString(1))) }
-                }
-                deferred.complete(fortunes + Fortune(0, "Additional fortune added at request time."))
-            }
-        }
-
-        return deferred.get().sortedBy { it.message }
-    }
 }
 
 private fun randomWorld() = Random().nextInt(9999) + 1

+ 2 - 2
frameworks/Kotlin/http4k/core/src/main/kotlin/WorldRoutes.kt

@@ -1,7 +1,7 @@
+
 import org.http4k.core.Body
 import org.http4k.core.Method.GET
 import org.http4k.core.Response
-import org.http4k.core.Status.Companion.NOT_FOUND
 import org.http4k.core.Status.Companion.OK
 import org.http4k.core.with
 import org.http4k.format.Jackson.array
@@ -23,7 +23,7 @@ object WorldRoutes {
     }.defaulted("queries", 1)
 
     fun queryRoute(db: Database) = "/db" bind GET to {
-        db.findWorld()?.let { Response(OK).with(jsonBody of it) } ?: Response(NOT_FOUND)
+        let { Response(OK).with(jsonBody of db.findWorld()) }
     }
 
     fun multipleRoute(db: Database) = "/queries" bind GET to {

+ 0 - 14
frameworks/Kotlin/http4k/http4k-apache-asyncdb.dockerfile

@@ -1,14 +0,0 @@
-FROM gradle:5.1.0-jdk11
-USER root
-WORKDIR /http4k
-COPY build.gradle build.gradle
-COPY settings.gradle settings.gradle
-COPY apache apache
-COPY apache-asyncdb apache-asyncdb
-COPY core core
-COPY jetty jetty
-COPY ktorcio ktorcio
-COPY netty netty
-COPY undertow undertow
-RUN gradle --quiet build apache-asyncdb:shadowJar
-CMD ["java", "-server", "-XX:+UseNUMA", "-XX:+UseParallelGC", "-XX:+AggressiveOpts", "-XX:+AlwaysPreTouch", "-jar", "apache-asyncdb/build/libs/http4k-apache-asyncdb-benchmark.jar"]

+ 0 - 1
frameworks/Kotlin/http4k/http4k-apache.dockerfile

@@ -4,7 +4,6 @@ WORKDIR /http4k
 COPY build.gradle build.gradle
 COPY settings.gradle settings.gradle
 COPY apache apache
-COPY apache-asyncdb apache-asyncdb
 COPY core core
 COPY jetty jetty
 COPY ktorcio ktorcio

+ 0 - 1
frameworks/Kotlin/http4k/http4k-ktorcio.dockerfile

@@ -4,7 +4,6 @@ WORKDIR /http4k
 COPY build.gradle build.gradle
 COPY settings.gradle settings.gradle
 COPY apache apache
-COPY apache-asyncdb apache-asyncdb
 COPY core core
 COPY jetty jetty
 COPY ktorcio ktorcio

+ 0 - 1
frameworks/Kotlin/http4k/http4k-netty.dockerfile

@@ -4,7 +4,6 @@ WORKDIR /http4k
 COPY build.gradle build.gradle
 COPY settings.gradle settings.gradle
 COPY apache apache
-COPY apache-asyncdb apache-asyncdb
 COPY core core
 COPY jetty jetty
 COPY ktorcio ktorcio

+ 0 - 1
frameworks/Kotlin/http4k/http4k-undertow.dockerfile

@@ -4,7 +4,6 @@ WORKDIR /http4k
 COPY build.gradle build.gradle
 COPY settings.gradle settings.gradle
 COPY apache apache
-COPY apache-asyncdb apache-asyncdb
 COPY core core
 COPY jetty jetty
 COPY ktorcio ktorcio

+ 0 - 1
frameworks/Kotlin/http4k/http4k.dockerfile

@@ -4,7 +4,6 @@ WORKDIR /http4k
 COPY build.gradle build.gradle
 COPY settings.gradle settings.gradle
 COPY apache apache
-COPY apache-asyncdb apache-asyncdb
 COPY core core
 COPY jetty jetty
 COPY ktorcio ktorcio

+ 0 - 1
frameworks/Kotlin/http4k/settings.gradle

@@ -1,7 +1,6 @@
 rootProject.name = 'http4k-benchmark'
 include 'core'
 include 'apache'
-include 'apache-asyncdb'
 include 'jetty'
 include 'ktorcio'
 include 'netty'