Skip to content

Commit

Permalink
Modify the log method in JdbcAction to also log group times. Because …
Browse files Browse the repository at this point in the history
…this modifies the session the actions have to pass on the modified session again. Add simulation for group metrics.

Closes #2
  • Loading branch information
rbraeunlich committed May 9, 2019
1 parent e757507 commit d79496a
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import scala.util.Try
*/
trait JdbcAction extends ChainableAction with NameGen {

def log(start: Long, end: Long, tried: Try[_], requestName: Expression[String], session: Session, statsEngine: StatsEngine): Unit = {
def log(start: Long, end: Long, tried: Try[_], requestName: Expression[String], session: Session, statsEngine: StatsEngine): Session = {
val (status, message) = tried match {
case scala.util.Success(_) => (OK, None)
case scala.util.Failure(exception) => (KO, Some(exception.getMessage))
}
requestName.apply(session).foreach { resolvedRequestName =>
statsEngine.logResponse(session, resolvedRequestName, start, end, status, None, message)
}
session.logGroupRequest(start, end, status)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ case class JdbcCreateTableAction(requestName: Expression[String],
}
}
future.onComplete(result => {
log(start, clock.nowMillis, result, requestName, session, statsEngine)
next ! session
next ! log(start, clock.nowMillis, result, requestName, session, statsEngine)
})

case Failure(error) => throw new IllegalArgumentException(error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ case class JdbcDeletionAction(requestName: Expression[String],
}

result.foreach(_.onComplete(result => {
log(start, clock.nowMillis, result, requestName, session, statsEngine)
next ! session
next ! log(start, clock.nowMillis, result, requestName, session, statsEngine)
}))

result.onFailure(e => throw new IllegalArgumentException(e))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ case class JdbcDropTableAction(requestName: Expression[String],
}
}
future.onComplete(result => {
log(start, clock.nowMillis, result, requestName, session, statsEngine)
next ! session
next ! log(start, clock.nowMillis, result, requestName, session, statsEngine)
})

case Failure(error) => throw new IllegalArgumentException(error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ case class JdbcInsertAction(requestName: Expression[String],
}
}
result.foreach(_.onComplete(result => {
log(start, clock.nowMillis, result, requestName, session, statsEngine)
next ! session
next ! log(start, clock.nowMillis, result, requestName, session, statsEngine)
}))

result.onFailure(e => throw new IllegalArgumentException(e))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ case class JdbcSelectAction[T](requestName: Expression[String],
session.markAsFailed
}.get
case fail: Failure[_] =>
log(start, clock.nowMillis, fail, requestName, session, statsEngine)
next ! session
next ! log(start, clock.nowMillis, fail, requestName, session, statsEngine)
}
}

Expand All @@ -69,8 +68,7 @@ case class JdbcSelectAction[T](requestName: Expression[String],
}
modifiedSession.markAsFailed
case _ =>
log(start, clock.nowMillis, scala.util.Success(""), requestName, session, statsEngine)
modifiedSession
log(start, clock.nowMillis, scala.util.Success(""), requestName, modifiedSession, statsEngine)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package dev.code_n_roll.gatling.jdbc.simulation

import dev.code_n_roll.gatling.jdbc.Predef._
import dev.code_n_roll.gatling.jdbc.builder.column.ColumnHelper._
import io.gatling.core.Predef._
import io.gatling.core.scenario.Simulation
import scalikejdbc.{GlobalSettings, LoggingSQLAndTimeSettings}

import scala.concurrent.duration._
import scala.util.Random

/**
* Simulation to check that the reporting of times also works when using a group.
* Therefore, the response time is set to be between 0 and 1000. When the group
* time doesn't work the time will be just zero.
*/
class GroupInsertSimulation extends Simulation {

val jdbcConfig = jdbc.url("jdbc:h2:mem:test;DB_CLOSE_ON_EXIT=FALSE").username("sa").password("sa").driver("org.h2.Driver")

GlobalSettings.loggingSQLAndTime = LoggingSQLAndTimeSettings(singleLineMode = true, logLevel = 'warn)

val scn = scenario("group create insert")
.group("group") {
exec(jdbc("bar table")
.create()
.table("bar")
.columns(
column(
name("abc"),
dataType("VARCHAR"),
constraint("PRIMARY KEY")
)
)
).pause(3.seconds)
.exec(jdbc("insertion")
.insert()
.into("bar (abc)")
.values("'1'"))
}

setUp(
scn.inject(atOnceUsers(1)),
).protocols(jdbcConfig)
.assertions(details("group").responseTime.mean.between(0, 1000, inclusive = false))
}

0 comments on commit d79496a

Please sign in to comment.