Skip to content

Instantly share code, notes, and snippets.

@Tapac
Created August 12, 2021 17:34
Show Gist options
  • Save Tapac/f9093fa5c6d9125557b21a5448a5aff7 to your computer and use it in GitHub Desktop.
Save Tapac/f9093fa5c6d9125557b21a5448a5aff7 to your computer and use it in GitHub Desktop.
Perf test patch
Index: src/main/kotlin/ExposedTest.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/kotlin/ExposedTest.kt b/src/main/kotlin/ExposedTest.kt
--- a/src/main/kotlin/ExposedTest.kt (revision f98b59de55a6b70d25ce74a3ca2d6d07bcf87d66)
+++ b/src/main/kotlin/ExposedTest.kt (revision a9b34936c5070c4a2d7ce0949443e0a6097d7e2a)
@@ -21,17 +21,21 @@
val url =
"jdbc:mysql://10.0.0.21:3306/test?user=root&password=password&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false"
val database = Database.connect(url, user = "root", password = "password")
- val connection = DriverManager.getConnection(url)
+ // warm
+ resetTable(database)
+ testNative(DriverManager.getConnection(url), log = false) //insert time: 11086ms ,select time: 4312ms
resetTable(database)
- testNative(connection) //insert time: 11086ms ,select time: 4312ms
+ testNative(DriverManager.getConnection(url)) //insert time: 11086ms ,select time: 4312ms
resetTable(database)
- testNative2(connection)// insert time: 9324ms select time: 2529ms
+ testNative2(DriverManager.getConnection(url))// insert time: 9324ms select time: 2529ms
resetTable(database)
testExposed(database) // insert time: 37041ms ,select time: 27878ms
+ resetTable(database)
+ testExposed2(database) // insert time: 37041ms ,select time: 27878ms
}
fun resetTable(database: Database) {
@@ -43,18 +47,19 @@
fun testExposed(database: Database) {
var sw = Stopwatch.createStarted()
- repeat(10000) { i ->
- transaction(database) {
+ transaction(database) {
+ repeat(10000) { i ->
TestLongTable.insert {
it[key] = i
it[value] = true
}
+ commit()
}
}
println("exposed > insert time: ${sw.elapsed(TimeUnit.MILLISECONDS)}ms")
sw = Stopwatch.createStarted()
- repeat(10000) { i ->
- transaction(database) {
+ transaction(database) {
+ repeat(10000) { i ->
TestLongTable.select {
TestLongTable.key eq i
}.firstOrNull()?.get(TestLongTable.value)
@@ -64,27 +69,61 @@
}
-fun testNative(connection: Connection) {
+fun testNative(connection: Connection, log: Boolean = true) {
connection.autoCommit = false
var sw = Stopwatch.createStarted()
repeat(10000) {
- connection.createStatement().execute("INSERT INTO test_table (`key`,`value`) VALUES ($it, true)")
+ val prepareStatement = connection.prepareStatement("INSERT INTO test_table (`key`,`value`) VALUES (?, ?)")
+ prepareStatement.apply {
+ setInt(1, it)
+ setBoolean(2, true)
+ }.executeUpdate()
+ prepareStatement.close()
connection.commit()
}
- println("native > insert time: ${sw.elapsed(TimeUnit.MILLISECONDS)}ms")
+ if (log)
+ println("native > insert time: ${sw.elapsed(TimeUnit.MILLISECONDS)}ms")
sw = Stopwatch.createStarted()
repeat(10000) {
- connection.createStatement().executeQuery("SELECT * FROM `test_table` WHERE `key`=$it").apply {
+ val prepareStatement = connection.prepareStatement("SELECT * FROM `test_table` WHERE `key`=?")
+ prepareStatement.setInt(1, it)
+ prepareStatement.executeQuery().apply {
if (next()) {
getBoolean("value")
}
+ close()
}
+ prepareStatement.close()
connection.commit()
}
- println("native > select time: ${sw.elapsed(TimeUnit.MILLISECONDS)}ms")
+ if (log)
+ println("native > select time: ${sw.elapsed(TimeUnit.MILLISECONDS)}ms")
}
+fun testExposed2(database: Database) {
+ var sw = Stopwatch.createStarted()
+ transaction(database) {
+ repeat(10000) {
+ exec("INSERT INTO test_table (`key`,`value`) VALUES ($it, true)")
+ }
+ }
+ println("exposed2 > insert time: ${sw.elapsed(TimeUnit.MILLISECONDS)}ms")
+ sw = Stopwatch.createStarted()
+ transaction(database) {
+ repeat(10000) {
+ exec("SELECT * FROM `test_table` WHERE `key`=$it") {
+ if (it.next()) {
+ it.getBoolean("value")
+ }
+ }
+
+ }
+ }
+ println("exposed2 > select time: ${sw.elapsed(TimeUnit.MILLISECONDS)}ms")
+
+}
+
fun testNative2(connection: Connection) {
connection.autoCommit = true
var sw = Stopwatch.createStarted()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment