Skip to content

Instantly share code, notes, and snippets.

@aartajew
aartajew / SignatureCalculatorRemoveHeader.scala
Created April 20, 2023 10:15
Gatling > Remove header using signature calculator
package util
import io.gatling.commons.validation._
import io.gatling.core.Predef._
import io.gatling.http.client._
object SignatureCalculatorRemoveHeader {
def apply(header: String): (Request, Session) => Validation[_] = (request, _) => {
request.getHeaders.remove(header)
@aartajew
aartajew / Sha256.scala
Created April 20, 2023 10:14
Scala > Calculate SHA-256 for a string
package util
import java.math._
import java.security._
object Sha256 {
def encode(raw: String): String = {
String.format("%032x", new BigInteger(1, MessageDigest.getInstance("SHA-256").digest(raw.getBytes("UTF-8"))))
}
}
@aartajew
aartajew / RoundRobin.scala
Created April 20, 2023 10:12
Gatling > Round-robin over a sequence
package util
import io.gatling.commons.util._
object RoundRobin {
def apply[T](values: IndexedSeq[T]) = CircularIterator(values = values, threadSafe = true)
}
@aartajew
aartajew / Percentile.scala
Created April 20, 2023 10:10
Scala > Calculate percentile
package util
object Percentile {
// @see http://commons.apache.org/proper/commons-math/javadocs/api-3.6/org/apache/commons/math3/stat/descriptive/rank/Percentile.html
def apply(array: Seq[Double], p: Int) = {
require(p > 0)
require(p <= 100)
require(array.nonEmpty)
val n = array.length
@aartajew
aartajew / PauseExceptFirstTime.scala
Created April 20, 2023 10:08
Gatling > Pause user in a loop except first iteration
package util
import io.gatling.core.Predef._
import scala.concurrent.duration._
object PauseExceptFirstTime {
def apply(counter: String, duration: FiniteDuration) =
doIf(_(counter).asOption[Int].getOrElse(0) > 0) {
@aartajew
aartajew / OAuth2BasicHeader.scala
Created April 20, 2023 10:07
Scala > Build OAuth2 basic header
package util
import java.nio.charset._
import java.util._
object OAuth2BasicHeader {
def apply(key: String, secret: String) = {
val bytes = s"$key:$secret".getBytes(StandardCharsets.UTF_8)
"Basic " + Base64.getEncoder.encodeToString(bytes)
@aartajew
aartajew / LevenshteinDistance.scala
Created April 20, 2023 10:05
Scala > Calculate Levenshtein distance
package util
import scala.math.min
object LevenshteinDistance {
def apply[A](expected: Iterable[A], actual: Iterable[A]) = round(100.0 * editDist(expected, actual) / expected.size)
def pair[A](expectedLeft: Iterable[A], actualLeft: Iterable[A], expectedRight: Iterable[A], actualRight: Iterable[A]) = {
val leftDistance = apply(expectedLeft, actualLeft)
@aartajew
aartajew / Base64File.scala
Created April 20, 2023 10:03
Scala > Encode file to base64
package util
import java.io._
import java.util._
object Base64File {
def encode(sourcePath: String, targetPath: String) = {
val inputStream = new FileInputStream(sourcePath)
val outputStream = new FileOutputStream(targetPath)
@aartajew
aartajew / SaveResponseBodyToFile.scala
Last active April 20, 2023 10:01
Gatling > Save response body to a file
package util
import java.io._
import java.nio.file._
object SaveResponseBodyToFile {
def apply(body: InputStream, path: String) = {
val file = new File(path)
new File(file.getParent).mkdirs()
@aartajew
aartajew / ElParser.scala
Last active June 20, 2023 12:35
Gatling > Parse Expression Language string
package util
import io.gatling.core.session._
object ElParser {
def apply[T](expression: Expression[T], session: Session): T = toOption(expression, session).get
def toOption[T](expression: Expression[T], session: Session): Option[T] = expression(session).toOption
}