-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
73dd1fd
commit 4772472
Showing
8 changed files
with
222 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/main/scala/ru/tinkoff/load/jdbc/actions/DBBatchAction.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package ru.tinkoff.load.jdbc.actions | ||
|
||
import io.gatling.commons.stats.{KO, OK} | ||
import io.gatling.core.action.{Action, ChainableAction} | ||
import io.gatling.core.session.{Expression, Session} | ||
import io.gatling.core.structure.ScenarioContext | ||
import io.gatling.core.util.NameGen | ||
import io.gatling.commons.validation._ | ||
import ru.tinkoff.load.jdbc.db.{SQL, SqlWithParam} | ||
|
||
final case class DBBatchAction( | ||
batchName: Expression[String], | ||
actions: Seq[BatchAction], | ||
next: Action, | ||
ctx: ScenarioContext | ||
) extends ChainableAction with NameGen with ActionBase { | ||
|
||
private implicit class TrSeq[+T](seq: Seq[T]) { | ||
def traverse[S](f: T => Validation[S]): Validation[Seq[S]] = seq.foldRight(Seq.empty[S].success)( | ||
(i, r) => r.flatMap(s => f(i).map(s.prepended)) | ||
) | ||
} | ||
|
||
override def name: String = genName("jdbcBatchAction") | ||
|
||
private def resolveParams(session: Session, values: Seq[(String, Expression[Any])]) = | ||
values.traverse { case (k, v) => v(session).map((k, _)) }.map(_.toMap) | ||
|
||
private def resolveBatchAction(session: Session): PartialFunction[BatchAction, Validation[SqlWithParam]] = { | ||
|
||
case BatchUpdateAction(tableName, updateValues, None) => | ||
for { | ||
tName <- tableName(session) | ||
iParams <- resolveParams(session, updateValues) | ||
sql <- SQL(s"UPDATE $tName SET ${iParams.map(c => s"${c._1} = {${c._1}}").mkString(",")}") | ||
.withParamsMap(iParams) | ||
.success | ||
} yield sql | ||
|
||
case BatchUpdateAction(tableName, updateValues, Some(whereExpression)) => | ||
for { | ||
tName <- tableName(session) | ||
iParams <- resolveParams(session, updateValues) | ||
resolvedWhere <- whereExpression(session) | ||
sql <- SQL(s"UPDATE $tName SET ${iParams.map(c => s"${c._1} = {${c._1}}").mkString(",")} WHERE $resolvedWhere") | ||
.withParamsMap(iParams) | ||
.success | ||
} yield sql | ||
|
||
case BatchInsertAction(tableName, columns, sessionValues) => | ||
for { | ||
tName <- tableName(session) | ||
iParams <- resolveParams(session, sessionValues) | ||
sql <- SQL( | ||
s"INSERT INTO $tName (${columns.names.mkString(",")}) VALUES(${columns.names.map(s => s"{$s}").mkString(",")})") | ||
.withParamsMap(iParams) | ||
.success | ||
} yield sql | ||
} | ||
|
||
override protected def execute(session: Session): Unit = | ||
(for { | ||
resolvedBatchName <- batchName(session) | ||
sqlQueriesWithParams <- actions.traverse(resolveBatchAction(session)) | ||
startTime <- ctx.coreComponents.clock.nowMillis.success | ||
} yield | ||
db.executeBatch(implicit c => c.batch(sqlQueriesWithParams)) | ||
.fold( | ||
e => { | ||
println(s"ERROR: ${e.getMessage}") | ||
executeNext(session, | ||
startTime, | ||
ctx.coreComponents.clock.nowMillis, | ||
KO, | ||
next, | ||
resolvedBatchName, | ||
Some("ERROR"), | ||
Some(e.getMessage)) | ||
}, | ||
_ => executeNext(session, startTime, ctx.coreComponents.clock.nowMillis, OK, next, resolvedBatchName, None, None) | ||
)).onFailure(m => | ||
batchName(session).map { rn => | ||
ctx.coreComponents.statsEngine.logCrash(session.scenario, session.groups, rn, m) | ||
executeNext(session, | ||
ctx.coreComponents.clock.nowMillis, | ||
ctx.coreComponents.clock.nowMillis, | ||
KO, | ||
next, | ||
rn, | ||
Some("ERROR"), | ||
Some(m)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters