This repository has been archived by the owner on Sep 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://git.twitter.biz/kestrel
- Loading branch information
Showing
4 changed files
with
91 additions
and
15 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
54 changes: 54 additions & 0 deletions
54
loadtest/src/main/scala/net/lag/kestrel/loadtest/VaryingPoissonProcess.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,54 @@ | ||
package net.lag.kestrel.loadtest | ||
|
||
import util.Random | ||
import com.twitter.conversions.time._ | ||
import com.twitter.parrot.util.RequestDistribution | ||
import com.twitter.util.{Duration, JavaTimer, Time} | ||
import org.apache.commons.math.distribution.ExponentialDistributionImpl | ||
import java.util.concurrent.TimeUnit | ||
import java.lang.IllegalArgumentException | ||
|
||
case class ArrivalRate(val arrivalsPerSecond: Int, val duration: Duration) { | ||
if (arrivalsPerSecond <= 0) { | ||
throw new IllegalArgumentException("arrivalsPerSecond must be >= 1: " + arrivalsPerSecond) | ||
} | ||
|
||
if (duration < 1.millisecond) { | ||
throw new IllegalArgumentException("duration must be >= 1 millisecond: " + duration) | ||
} | ||
} | ||
|
||
class VaryingPoissonProcess(val arrivalRates: Seq[ArrivalRate]) extends RequestDistribution { | ||
if (arrivalRates.isEmpty) { | ||
throw new IllegalArgumentException("must have at least one arrival rate") | ||
} | ||
|
||
private[this] val rand = new Random(Time.now.inMillis) | ||
private[this] val timer = new JavaTimer(true) | ||
|
||
private[this] var iter: Iterator[ArrivalRate] = Iterator.empty | ||
private[this] var dist: Option[ExponentialDistributionImpl] = None | ||
|
||
private def updateDistribution() { | ||
synchronized { | ||
if (!iter.hasNext) { | ||
iter = arrivalRates.iterator | ||
} | ||
|
||
val nextArrivalRate = iter.next | ||
dist = Some(new ExponentialDistributionImpl(1000000000.0 / nextArrivalRate.arrivalsPerSecond)) | ||
timer.schedule(Time.now + nextArrivalRate.duration) { | ||
updateDistribution() | ||
} | ||
} | ||
} | ||
|
||
def timeToNextArrival(): Duration = { | ||
if (!dist.isDefined) { | ||
updateDistribution() | ||
} | ||
|
||
val nanosToNextArrival = dist.get.inverseCumulativeProbability(rand.nextDouble()) | ||
Duration(nanosToNextArrival.toLong, TimeUnit.NANOSECONDS) | ||
} | ||
} |
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