Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend JSON API ContractDao query bench’s with different tpids #10309

Merged
merged 1 commit into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ abstract class ContractDaoBenchmark extends OracleAround {
private var user: User = _

protected var dao: ContractDao = _
protected var surrogateTpId: SurrogateTpId = _

protected val tpid = TemplateId("pkg", "M", "T")
protected implicit val ec: ExecutionContext = ExecutionContext.global
protected implicit val logger: LogHandler = Slf4jLogHandler(getClass)

Expand All @@ -40,13 +38,6 @@ abstract class ContractDaoBenchmark extends OracleAround {
import oracleDao.jdbcDriver

dao.transact(ContractDao.initialize).unsafeRunSync()

surrogateTpId = dao
.transact(
oracleDao.jdbcDriver.queries
.surrogateTemplateId(tpid.packageId, tpid.moduleName, tpid.entityName)
)
.unsafeRunSync()
}

@TearDown(Level.Trial)
Expand All @@ -56,23 +47,33 @@ abstract class ContractDaoBenchmark extends OracleAround {
protected def contract(
id: Int,
signatory: String,
tpid: SurrogateTpId,
): DBContract[SurrogateTpId, JsValue, JsValue, Seq[String]] = DBContract(
contractId = s"#$id",
templateId = surrogateTpId,
templateId = tpid,
key = JsNull,
payload = JsObject(),
signatories = Seq(signatory),
observers = Seq.empty,
agreementText = "",
)

protected def insertBatch(signatory: String, offset: Int) = {
protected def insertTemplate(tpid: TemplateId.RequiredPkg): SurrogateTpId = {
dao
.transact(
dao.jdbcDriver.queries
.surrogateTemplateId(tpid.packageId, tpid.moduleName, tpid.entityName)
)
.unsafeRunSync()
}

protected def insertBatch(signatory: String, tpid: SurrogateTpId, offset: Int) = {
val driver = dao.jdbcDriver
import driver._
val contracts: List[DBContract[SurrogateTpId, JsValue, JsValue, Seq[String]]] =
(0 until batchSize).map { i =>
val n = offset + i
contract(n, signatory)
contract(n, signatory, tpid)
}.toList
val inserted = dao
.transact(driver.queries.insertContracts[List, JsValue, JsValue](contracts))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package com.daml.http.dbbackend

import cats.instances.list._
import com.daml.http.dbbackend.Queries.{DBContract, SurrogateTpId}
import com.daml.http.domain.TemplateId
import org.openjdk.jmh.annotations._
import scalaz.std.list._
import spray.json._
Expand All @@ -21,18 +22,21 @@ class InsertBenchmark extends ContractDaoBenchmark {

private var contractCids: List[String] = _

private var tpid: SurrogateTpId = _

@Setup(Level.Trial)
override def setup(): Unit = {
super.setup()
tpid = insertTemplate(TemplateId("-pkg-", "M", "T"))
contracts = (1 until numContracts + 1).map { i =>
// Use negative cids to avoid collisions with other contracts
contract(-i, "Alice")
contract(-i, "Alice", tpid)
}.toList

contractCids = contracts.map(_.contractId)

(0 until batches).foreach { batch =>
insertBatch("Alice", batch * batchSize)
insertBatch("Alice", tpid, batch * batchSize)
}
()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,48 @@

package com.daml.http.dbbackend

import scalaz.OneAnd
import com.daml.http.dbbackend.Queries.SurrogateTpId
import com.daml.http.domain.{Party, TemplateId}
import doobie.implicits._
import com.daml.http.domain.{Party}
import org.openjdk.jmh.annotations._
import scalaz.OneAnd

class QueryBenchmark extends ContractDaoBenchmark {
@Param(Array("10"))
var batches: Int = _
@Param(Array("1", "5", "9"))
var extraParties: Int = _

@Param(Array("1", "5", "9"))
var extraTemplates: Int = _

private val tpid = TemplateId("-pkg-", "M", "T")
private var surrogateTpid: SurrogateTpId = _
val party = "Alice"

@Setup(Level.Trial)
override def setup(): Unit = {
super.setup()
(0 until batches - 1).foreach { batch =>
insertBatch("Alice", batch * batchSize)
surrogateTpid = insertTemplate(tpid)

val surrogateTpids = surrogateTpid :: (0 until extraTemplates)
.map(i => insertTemplate(TemplateId("-pkg-", "M", s"T$i")))
.toList

val parties: List[String] = party :: (0 until extraParties).map(i => s"p$i").toList

var offset = 0
parties.foreach { p =>
surrogateTpids.foreach { t =>
insertBatch(p, t, offset)
offset += batchSize
}
}
insertBatch("Bob", (batches - 1) * batchSize)
()
}

@Benchmark @BenchmarkMode(Array(Mode.AverageTime))
def run(): Unit = {
implicit val driver: SupportedJdbcDriver = dao.jdbcDriver
val result = dao
.transact(ContractDao.selectContracts(OneAnd(Party("Bob"), Set.empty), tpid, fr"1 = 1"))
.transact(ContractDao.selectContracts(OneAnd(Party(party), Set.empty), tpid, fr"1 = 1"))
.unsafeRunSync()
assert(result.size == batchSize)
}
Expand Down