From 6b04ca5fd5351f53b1ef46002574a73389f242b1 Mon Sep 17 00:00:00 2001 From: Kamil Bozek Date: Thu, 21 Oct 2021 16:31:33 +0200 Subject: [PATCH] Generating random payload of configurable size in ledger-api-bench-tool command submission CHANGELOG_BEGIN - [Integration Kit] - ledger-api-bench-tool can generate test contracts with configurable payload size. CHANGELOG_END --- .../generating/ContractProducer.scala | 33 +++++++++++++------ .../generating/ContractSetDescriptor.scala | 3 +- .../generating/DescriptorParser.scala | 5 ++- .../services/PartyManagementService.scala | 1 - .../generating/DescriptorParserSpec.scala | 9 +++-- .../test-common/src/main/daml/model/Foo.daml | 1 + 6 files changed, 36 insertions(+), 16 deletions(-) diff --git a/ledger/ledger-api-bench-tool/src/main/scala/com/daml/ledger/api/benchtool/generating/ContractProducer.scala b/ledger/ledger-api-bench-tool/src/main/scala/com/daml/ledger/api/benchtool/generating/ContractProducer.scala index a06e78f3d5c2..b9d8a2fcfbdb 100644 --- a/ledger/ledger-api-bench-tool/src/main/scala/com/daml/ledger/api/benchtool/generating/ContractProducer.scala +++ b/ledger/ledger-api-bench-tool/src/main/scala/com/daml/ledger/api/benchtool/generating/ContractProducer.scala @@ -7,20 +7,21 @@ import akka.actor.ActorSystem import akka.stream.Materializer import akka.stream.scaladsl.Source import com.daml.ledger.api.benchtool.infrastructure.TestDars -import com.daml.ledger.api.v1.commands.Commands -import com.daml.ledger.client.binding.Primitive.Party import com.daml.ledger.api.benchtool.services.LedgerApiServices import com.daml.ledger.api.benchtool.util.SimpleFileReader +import com.daml.ledger.api.v1.commands.Commands import com.daml.ledger.client.binding.Primitive +import com.daml.ledger.client.binding.Primitive.Party import com.daml.ledger.resources.{ResourceContext, ResourceOwner} +import com.daml.ledger.test.model.Foo.Foo1 import org.slf4j.LoggerFactory +import scalaz.syntax.tag._ import java.io.File -import scala.concurrent.{ExecutionContext, Future} -import scala.util.{Failure, Success, Try} -import scalaz.syntax.tag._ -import com.daml.ledger.test.model.Foo.Foo1 +import java.nio.charset.StandardCharsets import scala.concurrent.duration._ +import scala.concurrent.{ExecutionContext, Future} +import scala.util.{Failure, Random, Success, Try} case class ContractProducer(services: LedgerApiServices) { private val logger = LoggerFactory.getLogger(getClass) @@ -79,10 +80,17 @@ case class ContractProducer(services: LedgerApiServices) { } yield () } - private def createContract(index: Int, party: Party)(implicit - ec: ExecutionContext + private def randomPayload(sizeBytes: Int, random: Random): String = + new String(random.nextBytes(sizeBytes), StandardCharsets.UTF_8) + + private def createContract(index: Int, party: Party, payload: String)( + implicit ec: ExecutionContext ): Future[Int] = { - val createCommand = Foo1(signatory = party, observers = List(party)).create.command + val createCommand = Foo1( + signatory = party, + observers = List(party), + payload = payload, + ).create.command val commands = new Commands( ledgerId = services.ledgerId, applicationId = applicationId, @@ -105,6 +113,7 @@ case class ContractProducer(services: LedgerApiServices) { } implicit val resourceContext: ResourceContext = ResourceContext(ec) + val random = new Random(System.currentTimeMillis()) materializerOwner() .use { implicit materializer => Source @@ -113,7 +122,11 @@ case class ContractProducer(services: LedgerApiServices) { elements = 100, per = 1.second, ) - .mapAsync(8)(index => createContract(index, party)) + .mapAsync(8)(index => createContract( + index = index, + party = party, + payload = randomPayload(descriptor.payloadSizeBytes, random), + )) .runForeach(logProgress) } .map(_ => ()) diff --git a/ledger/ledger-api-bench-tool/src/main/scala/com/daml/ledger/api/benchtool/generating/ContractSetDescriptor.scala b/ledger/ledger-api-bench-tool/src/main/scala/com/daml/ledger/api/benchtool/generating/ContractSetDescriptor.scala index 6f3ff45d8168..c7ba20586609 100644 --- a/ledger/ledger-api-bench-tool/src/main/scala/com/daml/ledger/api/benchtool/generating/ContractSetDescriptor.scala +++ b/ledger/ledger-api-bench-tool/src/main/scala/com/daml/ledger/api/benchtool/generating/ContractSetDescriptor.scala @@ -4,5 +4,6 @@ package com.daml.ledger.api.benchtool.generating final case class ContractSetDescriptor( - numberOfInstances: Int + numberOfInstances: Int, + payloadSizeBytes: Int, ) diff --git a/ledger/ledger-api-bench-tool/src/main/scala/com/daml/ledger/api/benchtool/generating/DescriptorParser.scala b/ledger/ledger-api-bench-tool/src/main/scala/com/daml/ledger/api/benchtool/generating/DescriptorParser.scala index bf89dc2a4091..04017f4ccba6 100644 --- a/ledger/ledger-api-bench-tool/src/main/scala/com/daml/ledger/api/benchtool/generating/DescriptorParser.scala +++ b/ledger/ledger-api-bench-tool/src/main/scala/com/daml/ledger/api/benchtool/generating/DescriptorParser.scala @@ -18,7 +18,10 @@ object DescriptorParser { .map(error => DescriptorParserError(error.getLocalizedMessage)) implicit val decoder: Decoder[ContractSetDescriptor] = - Decoder.forProduct1("num_instances")(ContractSetDescriptor.apply) + Decoder.forProduct2( + "num_instances", + "payload_size_bytes", + )(ContractSetDescriptor.apply) case class DescriptorParserError(details: String) diff --git a/ledger/ledger-api-bench-tool/src/main/scala/com/daml/ledger/api/benchtool/services/PartyManagementService.scala b/ledger/ledger-api-bench-tool/src/main/scala/com/daml/ledger/api/benchtool/services/PartyManagementService.scala index 3af3ed96eda1..64815ca87043 100644 --- a/ledger/ledger-api-bench-tool/src/main/scala/com/daml/ledger/api/benchtool/services/PartyManagementService.scala +++ b/ledger/ledger-api-bench-tool/src/main/scala/com/daml/ledger/api/benchtool/services/PartyManagementService.scala @@ -38,5 +38,4 @@ class PartyManagementService(channel: Channel) { exception } } - } diff --git a/ledger/ledger-api-bench-tool/src/test/suite/scala/com/daml/ledger/api/benchtool/generating/DescriptorParserSpec.scala b/ledger/ledger-api-bench-tool/src/test/suite/scala/com/daml/ledger/api/benchtool/generating/DescriptorParserSpec.scala index 9e3d42b71f69..87d54e9b04a6 100644 --- a/ledger/ledger-api-bench-tool/src/test/suite/scala/com/daml/ledger/api/benchtool/generating/DescriptorParserSpec.scala +++ b/ledger/ledger-api-bench-tool/src/test/suite/scala/com/daml/ledger/api/benchtool/generating/DescriptorParserSpec.scala @@ -13,10 +13,13 @@ class DescriptorParserSpec extends AnyWordSpec with Matchers { "return error when empty yaml" in { parseYaml("") shouldBe a[Left[_, _]] } - "parse number of instances" in { - parseYaml("""num_instances: 123""") shouldBe Right( + "parse a correct descriptor" in { + val yaml = """num_instances: 123 + |payload_size_bytes: 111""".stripMargin + parseYaml(yaml) shouldBe Right( ContractSetDescriptor( - numberOfInstances = 123 + numberOfInstances = 123, + payloadSizeBytes = 111, ) ) } diff --git a/ledger/test-common/src/main/daml/model/Foo.daml b/ledger/test-common/src/main/daml/model/Foo.daml index ec9a65d2c58c..f3aa505b53bd 100644 --- a/ledger/test-common/src/main/daml/model/Foo.daml +++ b/ledger/test-common/src/main/daml/model/Foo.daml @@ -7,6 +7,7 @@ template Foo1 with signatory : Party observers : [Party] + payload : Text where signatory signatory observer observers