Skip to content

Commit

Permalink
Generating random payload of configurable size in ledger-api-bench-to…
Browse files Browse the repository at this point in the history
…ol command submission

CHANGELOG_BEGIN
- [Integration Kit] - ledger-api-bench-tool can generate test contracts with configurable payload size.
CHANGELOG_END
  • Loading branch information
kamil-da committed Oct 25, 2021
1 parent 0ee59f5 commit 81581bc
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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(_ => ())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
package com.daml.ledger.api.benchtool.generating

final case class ContractSetDescriptor(
numberOfInstances: Int
numberOfInstances: Int,
payloadSizeBytes: Int,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ class PartyManagementService(channel: Channel) {
exception
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
)
}
Expand Down
1 change: 1 addition & 0 deletions ledger/test-common/src/main/daml/model/Foo.daml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ template Foo1
with
signatory : Party
observers : [Party]
payload : Text
where
signatory signatory
observer observers
Expand Down

0 comments on commit 81581bc

Please sign in to comment.