From 153dcb7d3821c128b4dcc6786427b0d7dd89d1d8 Mon Sep 17 00:00:00 2001 From: Moritz Kiefer Date: Wed, 14 Jul 2021 09:51:05 +0200 Subject: [PATCH] Add a benchmark for contract insertion in the JSON API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Unfortunately the results seem to match up with my initial benchmark in #10234 Benchmark (batchSize) (batches) (numContracts) Mode Cnt Score Error Units InsertBenchmark.run 1000 1 1000 avgt 5 336.674 ± 42.058 ms/op InsertBenchmark.run 1000 3 1000 avgt 5 787.086 ± 223.018 ms/op InsertBenchmark.run 1000 5 1000 avgt 5 1181.041 ± 317.017 ms/op InsertBenchmark.run 1000 7 1000 avgt 5 1531.185 ± 341.060 ms/op InsertBenchmark.run 1000 9 1000 avgt 5 1945.345 ± 436.352 ms/op Score should ideally be more or less constant but it goes up very significantly as the total ACS size changes fixes #10245 changelog_begin changelog_end --- .../http/dbbackend/ContractDaoBenchmark.scala | 2 +- .../daml/http/dbbackend/InsertBenchmark.scala | 54 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 ledger-service/http-json/src/bench/scala/com/daml/http/dbbackend/InsertBenchmark.scala diff --git a/ledger-service/http-json/src/bench/scala/com/daml/http/dbbackend/ContractDaoBenchmark.scala b/ledger-service/http-json/src/bench/scala/com/daml/http/dbbackend/ContractDaoBenchmark.scala index ed395ea60443..71a85a064120 100644 --- a/ledger-service/http-json/src/bench/scala/com/daml/http/dbbackend/ContractDaoBenchmark.scala +++ b/ledger-service/http-json/src/bench/scala/com/daml/http/dbbackend/ContractDaoBenchmark.scala @@ -3,7 +3,6 @@ package com.daml.http.dbbackend -import scalaz.std.list._ import cats.instances.list._ import doobie.util.log.LogHandler import com.daml.doobie.logging.Slf4jLogHandler @@ -12,6 +11,7 @@ import com.daml.http.domain.TemplateId import com.daml.testing.oracle, oracle.{OracleAround, User} import org.openjdk.jmh.annotations._ import scala.concurrent.ExecutionContext +import scalaz.std.list._ import spray.json._ import spray.json.DefaultJsonProtocol._ diff --git a/ledger-service/http-json/src/bench/scala/com/daml/http/dbbackend/InsertBenchmark.scala b/ledger-service/http-json/src/bench/scala/com/daml/http/dbbackend/InsertBenchmark.scala new file mode 100644 index 000000000000..1862933f728a --- /dev/null +++ b/ledger-service/http-json/src/bench/scala/com/daml/http/dbbackend/InsertBenchmark.scala @@ -0,0 +1,54 @@ +// 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 cats.instances.list._ +import com.daml.http.dbbackend.Queries.{DBContract, SurrogateTpId} +import org.openjdk.jmh.annotations._ +import scalaz.std.list._ +import spray.json._ +import spray.json.DefaultJsonProtocol._ + +class InsertBenchmark extends ContractDaoBenchmark { + @Param(Array("1", "3", "5", "7", "9")) + var batches: Int = _ + + @Param(Array("1000")) + var numContracts: Int = _ + + private var contracts: List[DBContract[SurrogateTpId, JsValue, JsValue, Seq[String]]] = _ + + private var contractCids: List[String] = _ + + @Setup(Level.Trial) + override def setup(): Unit = { + super.setup() + contracts = + (1 until numContracts + 1).map { i => + // Use negative cids to avoid collisions with other contracts + contract(-i, "Alice") + }.toList + + contractCids = contracts.map(_.contractId) + + (0 until batches).foreach { batch => + insertBatch("Alice", batch * batchSize) + } + () + } + + @TearDown(Level.Invocation) + def dropContracts: Unit = { + val deleted = dao.transact(dao.jdbcDriver.queries.deleteContracts(contractCids)).unsafeRunSync() + assert(deleted == numContracts) + } + + @Benchmark + def run(): Unit = { + val driver: SupportedJdbcDriver = dao.jdbcDriver + import driver._ + val inserted = dao.transact(driver.queries.insertContracts(contracts)).unsafeRunSync() + assert(inserted == numContracts) + } +}