daml/triggers/simulations/scala/Contention.scala
Remy 96f16df3c0
remove script/trigger/cantonFixture deps on scala-bindings (#17954)
Basically we remove the dependency of different components on "//language-support/scala/bindings" by:

- replacing com.daml.ledger.api.refinements.ApiTypes.Party by com.daml.lf.data.Ref.Party
- replacing com.daml.ledger.api.refinements.ApiTypes.ApplicationId by Option[com.daml.lf.data.Ref.ApplicationId] (here we use option as ApiTypes.ApplicationId allows empty string while Ref.ApplicationId does not).
- adding rounding logic for timestamp in com.daml.lf.data.Time.Timestamp and use it instead of the one from com.daml.api.util.TimestampConversion

Note we did not clean daml-sript export as it have never pass the alpha stage and will be dropped with the 3.x fork.
2023-12-05 15:08:09 +00:00

88 lines
3.2 KiB
Scala

// Copyright (c) 2023 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package com.daml.lf.engine.trigger
import org.apache.pekko.actor.typed.Behavior
import org.apache.pekko.actor.typed.scaladsl.Behaviors
import com.daml.lf.data.Ref
import com.daml.lf.engine.trigger.simulation.TriggerMultiProcessSimulation
import com.daml.lf.engine.trigger.simulation.TriggerMultiProcessSimulation.TriggerSimulationConfig
import com.daml.lf.engine.trigger.simulation.process.wrapper.TriggerTimer
import java.nio.file.{Path, Paths}
import scala.concurrent.duration._
class GenericContention(delay: FiniteDuration) extends TriggerMultiProcessSimulation {
override protected lazy val darFile: Either[Path, Path] =
Right(
Paths.get(
Option(System.getenv("DAR")).getOrElse(
throw new RuntimeException(
"Trigger simulation needs a Dar file specified using the environment variable: DAR"
)
)
)
)
// For demonstration purposes, we only run the simulation for 30 seconds
override protected implicit lazy val simulationConfig: TriggerSimulationConfig =
TriggerSimulationConfig(simulationDuration = 30.seconds)
override protected val cantonFixtureDebugMode: Boolean = true
override protected def triggerMultiProcessSimulation: Behavior[Unit] = {
implicit val applicationId: Option[Ref.ApplicationId] = this.applicationId
withLedger { (client, ledger, actAs, controllerContext) =>
val breedingTriggerFactory =
triggerProcessFactory(client, ledger, "Cats:slowBreedingTrigger", actAs)
val pettingTriggerFactory =
triggerProcessFactory(client, ledger, "Cats:pettingTrigger", actAs)
val cullingTriggerFactory =
triggerProcessFactory(client, ledger, "Cats:cullingTrigger", actAs)
val breedingStartState = unsafeSValueFromLf("Types:Tuple2 { _1 = False, _2 = 0 }")
val startState = unsafeSValueFromLf(
"Types:Tuple2 { _1 = Nil @(ContractId Cats:Cat), _2 = None @(ContractId Cats:Cat) }"
)
val breedingTrigger = controllerContext.spawn(
breedingTriggerFactory.create(breedingStartState, Seq.empty),
"slowBreedingTrigger",
)
val pettingTrigger = controllerContext.spawn(
pettingTriggerFactory.create(startState, Seq.empty),
"pettingTrigger",
)
val cullingTrigger = controllerContext.spawn(
cullingTriggerFactory.create(startState, Seq.empty),
"cullingTrigger",
)
controllerContext.watch(breedingTrigger)
controllerContext.watch(pettingTrigger)
controllerContext.watch(cullingTrigger)
controllerContext.spawn(
TriggerTimer.regularMessage(1.second)(breedingTrigger),
"timed-slowBreedingTrigger",
)
controllerContext.spawn(
TriggerTimer.regularMessage(delay)(pettingTrigger),
"timed-pettingTrigger",
)
controllerContext.spawn(
TriggerTimer.regularMessage(1.second)(cullingTrigger),
"timed-cullingTrigger",
)
Behaviors.empty
}
}
}
class SlowContention extends GenericContention(1.second)
class MediumContention extends GenericContention(500.milliseconds)
class FastContention extends GenericContention(250.milliseconds)