Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DPP-662 Support archivals in bench tool #11474

Merged
merged 2 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For scala.util.Random.nextDouble, the upper bound is exclusive.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for spotting this! I need to review Distribution implementation and tests to check if this is actually reflected there.

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