Skip to content

Commit

Permalink
Merge pull request #257 from takapi327/enhancement/2024-07-Additional…
Browse files Browse the repository at this point in the history
…-benchmarks

Enhancement/2024 07 additional benchmarks
  • Loading branch information
takapi327 authored Aug 19, 2024
2 parents d4ef4ff + 8c8bc61 commit 6af0034
Show file tree
Hide file tree
Showing 48 changed files with 8,039 additions and 1,104 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ target/
.vscode/
metals.sbt

**/benchmark/**/*.json
**/benchmark/**/*.svg
3 changes: 3 additions & 0 deletions benchmark/src/main/scala/benchmark/Model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
package benchmark

import ldbc.query.builder.Table
import ldbc.query.builder.formatter.Naming

given Naming = Naming.PASCAL

case class Model1(
c1: Int
Expand Down
42 changes: 0 additions & 42 deletions benchmark/src/main/scala/benchmark/caseclass/CompileCreate.scala

This file was deleted.

101 changes: 0 additions & 101 deletions benchmark/src/main/scala/benchmark/caseclass/RuntimeCreate.scala

This file was deleted.

91 changes: 91 additions & 0 deletions benchmark/src/main/scala/benchmark/connector/jdbc/Batch.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* Copyright (c) 2023-2024 by Takahiko Tominaga
* This software is licensed under the MIT License (MIT).
* For more information see LICENSE or https://opensource.org/licenses/MIT
*/

package benchmark.connector.jdbc

import java.util.concurrent.TimeUnit

import scala.compiletime.uninitialized

import com.mysql.cj.jdbc.MysqlDataSource

import org.openjdk.jmh.annotations.*

import cats.effect.*
import cats.effect.unsafe.implicits.global

import ldbc.sql.Connection

@BenchmarkMode(Array(Mode.Throughput))
@OutputTimeUnit(TimeUnit.SECONDS)
@State(Scope.Benchmark)
class Batch:

@volatile
var connection: Resource[IO, Connection[IO]] = uninitialized

@volatile
var values: String = uninitialized

@volatile
var records: List[(Int, String)] = List.empty

@Setup
def setupDataSource(): Unit =
val ds = new MysqlDataSource()
ds.setServerName("127.0.0.1")
ds.setPortNumber(13306)
ds.setDatabaseName("benchmark")
ds.setUser("ldbc")
ds.setPassword("password")
ds.setRewriteBatchedStatements(true)

val datasource = jdbc.connector.MysqlDataSource[IO](ds)

connection = Resource.make(datasource.getConnection)(_.close())

values = (1 to len).map(_ => "(?, ?)").mkString(",")

records = (1 to len).map(num => (num, s"record$num")).toList

@Param(Array("10", "100", "1000", "2000", "4000"))
var len: Int = uninitialized

@Benchmark
def statement(): Unit =
connection
.use { conn =>
for
statement <- conn.createStatement()
_ <- records.foldLeft(IO.unit) {
case (acc, (id, value)) =>
acc *>
statement.addBatch(s"INSERT INTO jdbc_statement_test (c1, c2) VALUES ${ records
.map { case (v1, v2) => s"($v1, '$v2')" }
.mkString(", ") }")
}
_ <- statement.executeBatch()
yield ()
}
.unsafeRunSync()

@Benchmark
def prepareStatement(): Unit =
connection
.use { conn =>
for
statement <- conn.prepareStatement(s"INSERT INTO jdbc_prepare_statement_test (c1, c2) VALUES (?, ?)")
_ <- records.foldLeft(IO.unit) {
case (acc, (id, value)) =>
acc *>
statement.setInt(1, id) *>
statement.setString(2, value) *>
statement.addBatch()
}
_ <- statement.executeBatch()
yield ()
}
.unsafeRunSync()
146 changes: 146 additions & 0 deletions benchmark/src/main/scala/benchmark/connector/jdbc/Insert.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/**
* Copyright (c) 2023-2024 by Takahiko Tominaga
* This software is licensed under the MIT License (MIT).
* For more information see LICENSE or https://opensource.org/licenses/MIT
*/

package benchmark.connector.jdbc

import java.util.concurrent.TimeUnit
import java.time.*

import scala.compiletime.uninitialized

import com.mysql.cj.jdbc.MysqlDataSource

import org.openjdk.jmh.annotations.*

import cats.effect.*
import cats.effect.unsafe.implicits.global

import ldbc.sql.Connection

@BenchmarkMode(Array(Mode.Throughput))
@OutputTimeUnit(TimeUnit.SECONDS)
@State(Scope.Benchmark)
class Insert:

@volatile
var connection: Resource[IO, Connection[IO]] = uninitialized

@volatile
var values: String = uninitialized

@volatile
var records: List[
(
Short,
Int,
Int,
Int,
Long,
Float,
Double,
BigDecimal,
String,
String,
Boolean,
LocalDate,
LocalTime,
LocalDateTime,
LocalDateTime
)
] = List.empty

@Setup
def setupDataSource(): Unit =
val ds = new MysqlDataSource()
ds.setServerName("127.0.0.1")
ds.setPortNumber(13306)
ds.setDatabaseName("benchmark")
ds.setUser("ldbc")
ds.setPassword("password")

val datasource = jdbc.connector.MysqlDataSource[IO](ds)

connection = Resource.make(datasource.getConnection)(_.close())

values = (1 to len).map(_ => "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)").mkString(",")

records = (1 to len)
.map(num =>
(
1.toShort,
1,
1,
1,
1L,
1.1f,
1.1,
BigDecimal.decimal(1.1),
s"varchar $num",
s"text $num",
true,
LocalDate.now(),
LocalTime.now(),
LocalDateTime.now(),
LocalDateTime.now()
)
)
.toList

@Param(Array("10", "100", "1000", "2000", "4000"))
var len: Int = uninitialized

@Benchmark
def statement(): Unit =
connection
.use { conn =>
for
statement <- conn.createStatement()
_ <-
statement.executeUpdate(
s"INSERT INTO jdbc_statement_test (c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15) VALUES ${ records
.map {
case (v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) =>
s"($v1, $v2, $v3, $v4, $v5, $v6, $v7, $v8, '$v9', '$v10', $v11, '$v12', '$v13', '${ v14.toString
.replace("T", " ") }', '${ v15.toString.replace("T", " ") }')"
}
.mkString(",") }"
)
yield ()
}
.unsafeRunSync()

@Benchmark
def prepareStatement(): Unit =
connection
.use { conn =>
for
statement <-
conn.prepareStatement(
s"INSERT INTO jdbc_prepare_statement_test (c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15) VALUES $values"
)
_ <- records.zipWithIndex.foldLeft(IO.unit) {
case (acc, ((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15), index)) =>
acc *>
statement.setShort(index * 15 + 1, v1) *>
statement.setInt(index * 15 + 2, v2) *>
statement.setInt(index * 15 + 3, v3) *>
statement.setInt(index * 15 + 4, v4) *>
statement.setLong(index * 15 + 5, v5) *>
statement.setFloat(index * 15 + 6, v6) *>
statement.setDouble(index * 15 + 7, v7) *>
statement.setBigDecimal(index * 15 + 8, v8.bigDecimal) *>
statement.setString(index * 15 + 9, v9) *>
statement.setString(index * 15 + 10, v10) *>
statement.setBoolean(index * 15 + 11, v11) *>
statement.setDate(index * 15 + 12, v12) *>
statement.setTime(index * 15 + 13, v13) *>
statement.setTimestamp(index * 15 + 14, v14) *>
statement.setTimestamp(index * 15 + 15, v15)
}
_ <- statement.executeUpdate()
yield ()
}
.unsafeRunSync()
Loading

0 comments on commit 6af0034

Please sign in to comment.