Skip to content

Commit

Permalink
Merge pull request #2440 from ciandt-crodrigues/add-silent-option-for…
Browse files Browse the repository at this point in the history
…-gatling-karate-builder

Add silent option for gatling karate builder
  • Loading branch information
ptrthomas authored Nov 9, 2023
2 parents f4529f8 + 8ee33b7 commit e74145b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
3 changes: 3 additions & 0 deletions karate-gatling/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,9 @@ And to run scenarios tagged `foo` AND `bar`
val delete = scenario("delete").exec(karateFeature("classpath:mock/cats-delete.feature", "@foo", "@bar"))
```

#### Silent execution
It is possible to set a `karateFeature()` to be silent, this allows the request executions to not be counter towards the gatling statistics, this is specially useful if you are planning to execute a warm-up process that could call the possible flows before the performance test starts, making sure all the flows will be compiled on the server before counting statistics like request time.

### Karate Variables
On the Scala side, after a `scenario` involving a [`karateFeature()`](#karatefeature) completes, the Karate variables that were part of the feature will be added to the [Gatling session](#gatling-session).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import scala.concurrent.duration.{Duration, MILLISECONDS}
import scala.concurrent.{Await, ExecutionContextExecutor, Future}

class KarateFeatureAction(val name: String, val tags: Seq[String], val protocol: KarateProtocol, val system: ActorSystem,
val statsEngine: StatsEngine, val clock: Clock, val next: Action) extends ExitableAction {
val statsEngine: StatsEngine, val clock: Clock, val next: Action, val isSilent: Boolean = false) extends ExitableAction {

override def execute(session: Session) = {

Expand Down Expand Up @@ -73,6 +73,9 @@ class KarateFeatureAction(val name: String, val tags: Seq[String], val protocol:
}

override def reportPerfEvent(event: PerfEvent): Unit = {
if(isSilent){
return
}
val okOrNot = if (event.isFailed) KO else OK
val message = if (event.getMessage == null) None else Option(event.getMessage)
statsEngine.logResponse(session.scenario, session.groups, event.getName, event.getStartTime, event.getEndTime, okOrNot, Option(event.getStatusCode.toString), message)
Expand Down Expand Up @@ -125,9 +128,16 @@ class KarateFeatureAction(val name: String, val tags: Seq[String], val protocol:

class KarateFeatureActionBuilder(name: String, tags: Seq[String]) extends ActionBuilder {

private var isSilent = false

def silent(): KarateFeatureActionBuilder = {
this.isSilent = true;
this;
}

override def build(ctx: ScenarioContext, next: Action): Action = {
val karateComponents = ctx.protocolComponentsRegistry.components(KarateProtocol.KarateProtocolKey)
new KarateFeatureAction(name, tags, karateComponents.protocol, karateComponents.system, ctx.coreComponents.statsEngine, ctx.coreComponents.clock, next)
new KarateFeatureAction(name, tags, karateComponents.protocol, karateComponents.system, ctx.coreComponents.statsEngine, ctx.coreComponents.clock, next, isSilent)
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package mock

import com.intuit.karate.Runner
import com.intuit.karate.gatling.KarateProtocol
import com.intuit.karate.gatling.PreDef._
import io.gatling.core.Predef._
import io.gatling.core.structure.ScenarioBuilder

import scala.concurrent.duration._

class CatsSimulationWithSilentWarmUp extends Simulation {

MockUtils.startServer(0)

val protocol: KarateProtocol = karateProtocol(
"/cats/{id}" -> Nil,
"/cats" -> pauseFor("get" -> 15, "post" -> 25)
)

protocol.nameResolver = (req, ctx) => req.getHeader("karate-name")
protocol.runner.karateEnv("perf")

val createWarmup: ScenarioBuilder = scenario("create warm-up").exec(karateFeature("classpath:mock/cats-create.feature").silent())
val create: ScenarioBuilder = scenario("create").exec(karateFeature("classpath:mock/cats-create.feature")).exec(session => {
println("*** id in gatling: " + session("id").as[String])
println("*** session status in gatling: " + session.status)
session
})

val deleteWarmup: ScenarioBuilder = scenario("delete warm-up").exec(karateFeature("classpath:mock/cats-delete.feature").silent())
val delete: ScenarioBuilder = scenario("delete").group("delete cats") {
exec(karateFeature("classpath:mock/cats-delete.feature@name=delete"))
}

val customWarmup: ScenarioBuilder = scenario("custom warm-up").exec(karateFeature("classpath:mock/cats-rpc.feature").silent())
val custom: ScenarioBuilder = scenario("custom").exec(karateFeature("classpath:mock/custom-rpc.feature"))

setUp(
createWarmup.inject(rampUsers(1) during (5 seconds)).andThen(
create.inject(rampUsers(10) during (5 seconds))
),
deleteWarmup.inject(rampUsers(1) during (5 seconds)).andThen(
delete.inject(rampUsers(5) during (5 seconds))
),
customWarmup.inject(rampUsers(1) during (5 seconds)).andThen(
custom.inject(rampUsers(10) during (5 seconds))
)
).protocols(protocol)

}

0 comments on commit e74145b

Please sign in to comment.