Skip to content

Commit

Permalink
feat: Refactor random data generation to use RandomProvider trait
Browse files Browse the repository at this point in the history
Replaced randomDigit and related methods with the generalized RandomProvider trait for a cleaner and more extensible design. Enhanced readability and maintainability by introducing implicit providers for common data types and improving validation logic. Updated feeder implementations and other code references to use the new randomValue abstraction.
  • Loading branch information
jigarkhwar committed Dec 10, 2024
1 parent 1e94fde commit 383d58e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.galaxio.gatling.feeders

import io.gatling.core.feeder.Feeder
import org.galaxio.gatling.utils.RandomDataGenerators
import org.galaxio.gatling.utils.RandomDataGenerators.IntRandomProvider

object RandomDigitFeeder {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ object RandomRangeStringFeeder {

lazy val alphabet = """abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@#%"&*()_-+={}<>?|:[].~"""

import org.galaxio.gatling.utils.RandomDataGenerators.IntRandomProvider

def apply(paramName: String, from: Int = 10, to: Int = 15, alphabet: String = alphabet): Feeder[String] =
feeder[String](paramName)(RandomDataGenerators.randomString(alphabet)(RandomDataGenerators.randomValue(from, to)))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.galaxio.gatling.utils

import com.typesafe.scalalogging.LazyLogging
import scala.util.Try

object IntensityConverter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import java.time.format.DateTimeFormatter
import java.time.temporal.TemporalUnit
import java.time.{Instant, LocalDateTime, ZoneId}
import java.util.UUID
import java.util.concurrent.ThreadLocalRandom
import scala.annotation.tailrec
import scala.util.Random

Expand Down Expand Up @@ -157,43 +156,6 @@ object RandomDataGenerators {
rng.random(min, max)
}

implicit object IntRandomProvider extends RandomProvider[Int] {

override def random(): Int = ThreadLocalRandom.current().nextInt()

override def random(max: Int): Int = ThreadLocalRandom.current().nextInt(max)

override def random(min: Int, max: Int): Int = ThreadLocalRandom.current().nextInt(min, max)

}

implicit object LongRandomProvider extends RandomProvider[Long] {
override def random(): Long = ThreadLocalRandom.current().nextLong()

override def random(max: Long): Long = ThreadLocalRandom.current().nextLong(max)

override def random(min: Long, max: Long): Long = ThreadLocalRandom.current().nextLong(min, max)

}

implicit object FloatRandomProvider extends RandomProvider[Float] {
override def random(): Float = ThreadLocalRandom.current().nextFloat()

override def random(max: Float): Float = ThreadLocalRandom.current().nextFloat() * max

override def random(min: Float, max: Float): Float =
min + ThreadLocalRandom.current().nextFloat() * (max - min)
}

implicit object DoubleRandomProvider extends RandomProvider[Double] {
override def random(): Double = ThreadLocalRandom.current().nextDouble()

override def random(max: Double): Double = ThreadLocalRandom.current().nextDouble(max)

override def random(min: Double, max: Double): Double =
min + ThreadLocalRandom.current().nextDouble() * (max - min)
}

/** Selects a random element from the provided list of integers. If the list is empty, a random integer of the specified
* length is generated and returned.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
package org.galaxio.gatling.utils

import org.galaxio.gatling.utils.RandomDataGenerators.{
DoubleRandomProvider,
FloatRandomProvider,
IntRandomProvider,
LongRandomProvider,
}

object RandomDataGeneratorsWrapper {

def randomIntValueFromJava(): Int = {
Expand Down
37 changes: 37 additions & 0 deletions src/main/scala/org/galaxio/gatling/utils/package.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,46 @@
package org.galaxio.gatling

import java.util.concurrent.ThreadLocalRandom
import scala.util.Random

package object utils {

def getRandomElement[T](seq: Seq[T]): T = seq(Random.nextInt(seq.length))

implicit object IntRandomProvider extends RandomProvider[Int] {

override def random(): Int = ThreadLocalRandom.current().nextInt()

override def random(max: Int): Int = ThreadLocalRandom.current().nextInt(max)

override def random(min: Int, max: Int): Int = ThreadLocalRandom.current().nextInt(min, max)

}

implicit object LongRandomProvider extends RandomProvider[Long] {
override def random(): Long = ThreadLocalRandom.current().nextLong()

override def random(max: Long): Long = ThreadLocalRandom.current().nextLong(max)

override def random(min: Long, max: Long): Long = ThreadLocalRandom.current().nextLong(min, max)

}

implicit object FloatRandomProvider extends RandomProvider[Float] {
override def random(): Float = ThreadLocalRandom.current().nextFloat()

override def random(max: Float): Float = ThreadLocalRandom.current().nextFloat() * max

override def random(min: Float, max: Float): Float =
min + ThreadLocalRandom.current().nextFloat() * (max - min)
}

implicit object DoubleRandomProvider extends RandomProvider[Double] {
override def random(): Double = ThreadLocalRandom.current().nextDouble()

override def random(max: Double): Double = ThreadLocalRandom.current().nextDouble(max)

override def random(min: Double, max: Double): Double =
min + ThreadLocalRandom.current().nextDouble() * (max - min)
}
}

0 comments on commit 383d58e

Please sign in to comment.