Skip to content

Commit

Permalink
Add support for negative date ranges in feeders
Browse files Browse the repository at this point in the history
Refactored `RandomDateRangeFeeder` to handle negative offset dates, added `negativeDateRange` feeder example, and updated related utilities and tests. This improves flexibility in generating dynamic date ranges.
  • Loading branch information
jigarkhwar committed Dec 11, 2024
1 parent 91048e6 commit 7d2e310
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.galaxio.performance.example

import io.gatling.app.Gatling
import io.gatling.shared.cli.GatlingCliOptions

object GatlingRunner {

def main(args: Array[String]): Unit = {

// this is where you specify the class you want to run
val simulationClass = classOf[SampleSimulation].getName

Gatling.main(
args ++
Array(
GatlingCliOptions.Simulation.shortOption,
simulationClass,
GatlingCliOptions.ResultsFolder.shortOption,
"results",
GatlingCliOptions.Launcher.shortOption,
"sbt",
),
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.galaxio.gatling.utils.phone._

import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit
import java.time.{LocalDateTime, ZoneId}
import java.time.{LocalDateTime, ZoneId, ZoneOffset, ZonedDateTime}

object Feeders {

Expand Down Expand Up @@ -42,6 +42,17 @@ object Feeders {
val vacationDate: Feeder[String] =
RandomDateRangeFeeder("startOfVacation", "endOfVacation", 14, "yyyy-MM-dd", LocalDateTime.now(), ChronoUnit.DAYS)

val negativeDateRange: Feeder[String] = {
RandomDateRangeFeeder(
"from",
"to",
-1,
"YYYY-MM-dd",
ZonedDateTime.now(ZoneOffset.UTC).toLocalDateTime,
ChronoUnit.MONTHS,
)
}

// random Int
val randomDigit: Feeder[Int] = RandomDigitFeeder("randomDigit")
val randomRangeInt: Feeder[Int] = CustomFeeder[Int]("randomRangeInt", RandomDataGenerators.randomValue(1, 50))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class SampleScenario {
.feed(randomDigit)
.feed(randomPhone)
.feed(randomRangeString)
.feed(negativeDateRange)
.feed(randomString)
.feed(randomUsaPhone)
.feed(randomUuid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ object RandomDateRangeFeeder {
dateFrom: LocalDateTime = LocalDateTime.now(),
unit: TemporalUnit = ChronoUnit.DAYS,
timezone: ZoneId = ZoneId.systemDefault(),
): Feeder[String] =
feeder[String](paramNameFrom)(dateFrom.format(DateTimeFormatter.ofPattern(datePattern)))
.map(m => m + (paramNameTo -> RandomDataGenerators.randomDate(offsetDate, datePattern, dateFrom, unit, timezone)))
): Feeder[String] = {
val dateFormatter = DateTimeFormatter.ofPattern(datePattern)

def addRandomDateToMap(originalMap: Map[String, String]): Map[String, String] =
originalMap + (paramNameTo -> RandomDataGenerators.randomDate(offsetDate, datePattern, dateFrom, unit, timezone))

feeder[String](paramNameFrom)(dateFrom.format(dateFormatter)).map(addRandomDateToMap)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object RandomDataGenerators {
}

private def validateRange[T](min: T, max: T)(implicit ordering: Ordering[T]): Unit = {
require(ordering.lt(min, max), s"Min value ($min) must be less than max value ($max)")
require(ordering.lteq(min, max), s"Min value ($min) must be less than max value ($max)")
}

/** Generates a random string of the specified length using the provided alphabet of characters.
Expand Down Expand Up @@ -153,7 +153,10 @@ object RandomDataGenerators {
*/
def randomValue[T](min: T, max: T)(implicit rng: RandomProvider[T], ord: Ordering[T]): T = {
validateRange(min, max)
rng.random(min, max)
if (min != max)
rng.random(min, max)
else
max
}

/** Selects a random element from the provided list of integers. If the list is empty, a random integer of the specified
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks

import java.time.temporal.ChronoUnit
import java.time.{LocalDateTime, ZoneId}

class RandomDataGeneratorsTest extends AnyFlatSpec with Matchers with ScalaCheckDrivenPropertyChecks {

// //uuid generator
// //uuid generator
val uuidPattern = "([a-f0-9]{8}(-[a-f0-9]{4}){4}[a-f0-9]{8})"

// //date generator
// //date generator
val dateFormat = "yyyy-MM-dd'T'HH:mm"
val datePattern = "[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}"
val dateFrom: LocalDateTime = LocalDateTime.now()
Expand All @@ -30,6 +31,10 @@ class RandomDataGeneratorsTest extends AnyFlatSpec with Matchers with ScalaCheck
RandomDataGenerators.randomUUID should fullyMatch regex uuidPattern
}

it should "generate correct value that is not greater than the input" in {
RandomDataGenerators.randomValue(1L, 1) should be <= 1L
}

it should "generate correct random date pattern" in {
forAll(Gen.choose(1, 100), Gen.choose(1, 100)) { (positiveOffset: Int, negativeOffset: Int) =>
RandomDataGenerators
Expand Down

0 comments on commit 7d2e310

Please sign in to comment.