Skip to content

Commit

Permalink
DPP-662 Support archivals in bench tool (#11474)
Browse files Browse the repository at this point in the history
* Support archivals in bench tool

changelog_begin
changelog_end

* Rename archive_chance
  • Loading branch information
rautenrieth-da authored Nov 2, 2021
1 parent 63d855f commit 6bafeb0
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import scala.util.{Failure, Success, Try}
final class CommandGenerator(
randomnessProvider: RandomnessProvider,
descriptor: ContractSetDescriptor,
signatory: Primitive.Party,
observers: List[Primitive.Party],
) {
private val distribution = new Distribution(descriptor.instanceDistribution.map(_.weight))
Expand All @@ -27,10 +28,12 @@ final class CommandGenerator(
(for {
(description, observers) <- Try((pickDescription(), pickObservers()))
payload <- Try(randomPayload(description.payloadSizeBytes))
archive <- Try(pickArchive(description))
command <- createContractCommand(
template = description.template,
observers = observers,
payload = payload,
archive = archive,
)
} yield command).recoverWith { case NonFatal(ex) =>
Failure(
Expand All @@ -49,18 +52,28 @@ final class CommandGenerator(
.filter { case (_, index) => isObserverUsed(index) }
.map(_._1)

private def pickArchive(description: ContractSetDescriptor.ContractDescription): Boolean =
randomnessProvider.randomDouble() < description.archiveChance

private def isObserverUsed(i: Int): Boolean =
randomnessProvider.randomNatural(math.pow(10.0, i.toDouble).toInt) == 0

private def createContractCommand(
template: String,
observers: List[Primitive.Party],
payload: String,
archive: Boolean,
): Try[Primitive.Party => Command] =
template match {
case "Foo1" => Success(Foo1(_, observers, payload).create.command)
case "Foo2" => Success(Foo2(_, observers, payload).create.command)
case "Foo3" => Success(Foo3(_, observers, payload).create.command)
(template, archive) match {
case ("Foo1", false) => Success(Foo1(_, observers, payload).create.command)
case ("Foo2", false) => Success(Foo2(_, observers, payload).create.command)
case ("Foo3", false) => Success(Foo3(_, observers, payload).create.command)
case ("Foo1", true) =>
Success(Foo1(_, observers, payload).createAnd.exerciseArchive(signatory).command)
case ("Foo2", true) =>
Success(Foo2(_, observers, payload).createAnd.exerciseArchive(signatory).command)
case ("Foo3", true) =>
Success(Foo3(_, observers, payload).createAnd.exerciseArchive(signatory).command)
case invalid => Failure(new RuntimeException(s"Invalid template: $invalid"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ case class CommandSubmitter(services: LedgerApiServices) {
logger.info(progressMeter.getProgress(index))
}
)
val generator = new CommandGenerator(RandomnessProvider.Default, descriptor, observers)
val generator =
new CommandGenerator(RandomnessProvider.Default, descriptor, signatory, observers)

logger.info("Submitting commands...")
materializerOwner()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ object ContractSetDescriptor {
template: String,
weight: Int,
payloadSizeBytes: Int,
archiveChance: Double,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ object DescriptorParser {

object Decoders {
implicit val contractDescriptionDecoder: Decoder[ContractSetDescriptor.ContractDescription] =
Decoder.forProduct3(
Decoder.forProduct4(
"template",
"weight",
"payload_size_bytes",
"archive_probability",
)(ContractSetDescriptor.ContractDescription.apply)

implicit val descriptorDecoder: Decoder[ContractSetDescriptor] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package com.daml.ledger.api.benchtool.submission

trait RandomnessProvider {
def randomDouble(): Double // 0.0 <= randomDouble() <= 1.0
def randomDouble(): Double // 0.0 <= randomDouble() < 1.0
def randomBytes(n: Int): Array[Byte]
def randomNatural(n: Int): Int // 0 <= randomNatural(n) < n
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,15 @@ class DescriptorParserSpec extends AnyWordSpec with Matchers with TableDrivenPro
| - template: Foo1
| weight: 50
| payload_size_bytes: 100
| archive_probability: 0.9
| - template: Foo2
| weight: 25
| payload_size_bytes: 150
| archive_probability: 0.8
| - template: Foo3
| weight: 25
| payload_size_bytes: 30""".stripMargin
| payload_size_bytes: 30
| archive_probability: 0.7""".stripMargin
parseYaml(yaml) shouldBe Right(
ContractSetDescriptor(
numberOfInstances = 123,
Expand All @@ -86,16 +89,19 @@ class DescriptorParserSpec extends AnyWordSpec with Matchers with TableDrivenPro
template = "Foo1",
weight = 50,
payloadSizeBytes = 100,
archiveChance = 0.9,
),
ContractDescription(
template = "Foo2",
weight = 25,
payloadSizeBytes = 150,
archiveChance = 0.8,
),
ContractDescription(
template = "Foo3",
weight = 25,
payloadSizeBytes = 30,
archiveChance = 0.7,
),
),
)
Expand Down

0 comments on commit 6bafeb0

Please sign in to comment.