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 20, 2021
1 parent 5c714b9 commit bb09225
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 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,16 @@ case class ContractProducer(services: LedgerApiServices) {
} yield ()
}

private def createContract(index: Int, party: Party)(implicit
ec: ExecutionContext
private def createContract(index: Int, party: Party, payloadSizeBytes: Int, random: Random)(
implicit ec: ExecutionContext
): Future[Unit] = {
val createCommand = Foo1(signatory = party, observers = List(party)).create.command
// TODO: move to a separate class
val randomPayload = new String(random.nextBytes(payloadSizeBytes), StandardCharsets.UTF_8)
val createCommand = Foo1(
signatory = party,
observers = List(party),
payload = randomPayload,
).create.command
val commands = new Commands(
ledgerId = services.ledgerId,
applicationId = applicationId,
Expand All @@ -98,6 +105,7 @@ case class ContractProducer(services: LedgerApiServices) {
ec: ExecutionContext
): Future[Unit] = {
implicit val resourceContext: ResourceContext = ResourceContext(ec)
val random = new Random(System.currentTimeMillis())
materializerOwner()
.use { implicit materializer =>
Source
Expand All @@ -106,7 +114,7 @@ case class ContractProducer(services: LedgerApiServices) {
elements = 100,
per = 1.second,
)
.mapAsync(4)(index => createContract(index, party))
.mapAsync(4)(index => createContract(index, party, descriptor.payloadSizeBytes, random))
.run()
}
.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 @@ -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 bb09225

Please sign in to comment.