Skip to content

Commit

Permalink
Factorize
Browse files Browse the repository at this point in the history
changelog_begin
changelog_end
  • Loading branch information
cocreature committed Jul 14, 2021
1 parent 76b0fe3 commit f6da4c0
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,32 @@

package com.daml.http.dbbackend

import scalaz.OneAnd
import scalaz.std.list._
import cats.instances.list._
import doobie.util.log.LogHandler
import doobie.implicits._
import com.daml.doobie.logging.Slf4jLogHandler
import com.daml.http.dbbackend.Queries.{DBContract, SurrogateTpId}
import com.daml.http.domain.{Party, TemplateId}
import com.daml.http.domain.TemplateId
import com.daml.testing.oracle, oracle.{OracleAround, User}
import org.openjdk.jmh.annotations._
import scala.concurrent.ExecutionContext
import spray.json._
import spray.json.DefaultJsonProtocol._

@State(Scope.Benchmark)
class ContractDaoBench extends OracleAround {
private implicit val logger: LogHandler = Slf4jLogHandler(getClass)
private val tpid = TemplateId("pkg", "M", "T")
private var surrogateTpid: SurrogateTpId = _
abstract class ContractDaoBenchmark extends OracleAround {

private var user: User = _
private var dao: ContractDao = _

@Param(Array("1000"))
var batchSize: Int = _
@Param(Array("10"))
var batches: Int = _
protected var dao: ContractDao = _
protected var surrogateTpId: SurrogateTpId = _

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

private def contract(
id: Int,
signatory: String,
): DBContract[SurrogateTpId, JsValue, JsValue, Seq[String]] = DBContract(
contractId = s"#$id",
templateId = surrogateTpid,
key = JsNull,
payload = JsObject(),
signatories = Seq(signatory),
observers = Seq.empty,
agreementText = "",
)

private def insertBatch(dao: ContractDao, signatory: String, offset: Int) = {
import dao.jdbcDriver._
val contracts: List[DBContract[SurrogateTpId, JsValue, JsValue, Seq[String]]] =
(0 until batchSize).map { i =>
val n = offset + i
contract(n, signatory)
}.toList
val inserted = dao
.transact(dao.jdbcDriver.queries.insertContracts[List, JsValue, JsValue](contracts))
.unsafeRunSync()
assert(inserted == batchSize)
}
@Param(Array("1000"))
var batchSize: Int = _

@Setup(Level.Trial)
def setup(): Unit = {
Expand All @@ -69,31 +41,43 @@ class ContractDaoBench extends OracleAround {

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

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

(0 until batches - 1).foreach { batch =>
insertBatch(dao, "Alice", batch * batchSize)
}

insertBatch(dao, "Bob", (batches - 1) * batchSize)
()
}

@TearDown(Level.Trial)
def teardown(): Unit =
dropUser(user.name)

@Benchmark
def run(): Unit = {
implicit val driver: SupportedJdbcDriver = dao.jdbcDriver
val result = dao
.transact(ContractDao.selectContracts(OneAnd(Party("Bob"), Set.empty), tpid, fr"1 = 1"))
protected def contract(
id: Int,
signatory: String,
): DBContract[SurrogateTpId, JsValue, JsValue, Seq[String]] = DBContract(
contractId = s"#$id",
templateId = surrogateTpId,
key = JsNull,
payload = JsObject(),
signatories = Seq(signatory),
observers = Seq.empty,
agreementText = "",
)

protected def insertBatch(signatory: String, 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)
}.toList
val inserted = dao
.transact(driver.queries.insertContracts[List, JsValue, JsValue](contracts))
.unsafeRunSync()
assert(result.size == batchSize)
assert(inserted == batchSize)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.http.dbbackend

import scalaz.OneAnd
import doobie.implicits._
import com.daml.http.domain.{Party}
import org.openjdk.jmh.annotations._

class QueryBenchmark extends ContractDaoBenchmark {
@Param(Array("10"))
var batches: Int = _

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

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

0 comments on commit f6da4c0

Please sign in to comment.