Skip to content

Instantly share code, notes, and snippets.

@ubourdon
Created November 18, 2013 22:07
Show Gist options
  • Save ubourdon/7536264 to your computer and use it in GitHub Desktop.
Save ubourdon/7536264 to your computer and use it in GitHub Desktop.
DurationTestTools
import scala.concurrent.duration._
trait DurationTestTools {
implicit def duration2DurationCompare(duration: Duration): DurationCompare = DurationCompare(duration)
case class DurationCompare(duration: Duration) {
def shouldBeMinusThan(expectedDuration: Duration) { assert(duration.compare(expectedDuration) < 0, s"$duration was not minus than $expectedDuration") }
def shouldBeMinusOrEqualThan(expectedDuration: Duration) { assert(duration.compare(expectedDuration) <= 0, s"$duration was not minus or equal than $expectedDuration") }
def shouldBeGreaterThan(expectedDuration: Duration) { assert(duration.compare(expectedDuration) > 0, s"$duration was not greater than $expectedDuration") }
def shouldBeGreaterOrEqualThan(expectedDuration: Duration) { assert(duration.compare(expectedDuration) >= 0, s"$duration was not greater or equal than $expectedDuration") }
def shouldBeEqualTo(expectedDuration: Duration) { assert(duration.compare(expectedDuration) == 0, s"$duration was not equal to $expectedDuration") }
}
def mesureTimeExecutionOf[T](toMesure: => T): (Duration, T) = {
val start = System.currentTimeMillis()
val result = toMesure
val stop = System.currentTimeMillis()
((stop - start) milliseconds, result)
}
}
import org.scalatest.{Matchers, BeforeAndAfterAll, FunSuite}
import scala.concurrent.duration._
class ServerTest extends FunSuite with DurationTestTools {
test("ping server with 2 client") {
val (time, results) = mesureTimeExecutionOf {
(1 to 5).par.map { x => Source.fromURL("http://google.fr").mkString }
}
println(s"time elapsed for ${results.size} requests = $time")
time shouldBeMinusThan (500 milliseconds)
}
}
@fbiville
Copy link

Plutôt que d'utiliser System.currentTimeMillis(), tu ferais pas mieux d'utiliser le StopWatch de Guava ?
Quoique tu n'as peut-être pas besoin d'une précision de fou non plus...

Mais l'avantage est que tu peux passer un Ticker et tu ne dépends donc plus (nécessairement) de System.

@ubourdon
Copy link
Author

ubourdon commented Feb 7, 2014

Connaissais pas. Je vais jeter un oeil.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment