From b1acb8ae7a137a2e6ad0ca8c748f2c4e7751ee20 Mon Sep 17 00:00:00 2001 From: Alberto Crespo Date: Sun, 17 Nov 2019 17:09:19 +0100 Subject: [PATCH] Finish basic metrics tests --- .../acsgh/metrics/scala/MetricRegistry.scala | 2 +- .../acsgh/metrics/scala/model/Metric.scala | 6 +- .../metrics/scala/model/PercentileTest.scala | 14 ---- .../acsgh/metrics/scala/model/RateTest.scala | 52 ++++++++++++++ .../metrics/scala/model/SamplerTest.scala | 71 ++++++++++++------- 5 files changed, 102 insertions(+), 43 deletions(-) create mode 100644 core/src/test/scala/acsgh/metrics/scala/model/RateTest.scala diff --git a/core/src/main/scala/acsgh/metrics/scala/MetricRegistry.scala b/core/src/main/scala/acsgh/metrics/scala/MetricRegistry.scala index 5616986..236cba8 100644 --- a/core/src/main/scala/acsgh/metrics/scala/MetricRegistry.scala +++ b/core/src/main/scala/acsgh/metrics/scala/MetricRegistry.scala @@ -2,6 +2,6 @@ package acsgh.metrics.scala import com.acsgh.common.scala.log.LogSupport -class MetricRegistry extends LogSupport{ +class MetricRegistry extends LogSupport { } diff --git a/core/src/main/scala/acsgh/metrics/scala/model/Metric.scala b/core/src/main/scala/acsgh/metrics/scala/model/Metric.scala index e978482..c7ade33 100644 --- a/core/src/main/scala/acsgh/metrics/scala/model/Metric.scala +++ b/core/src/main/scala/acsgh/metrics/scala/model/Metric.scala @@ -135,7 +135,7 @@ case class Percentile() extends Metric with Reset { } } -case class Rate(period: Duration = 1 minute) extends Metric with Reset with Ticks { +case class Rate(period: Duration = 1 minute, timeProvider: () => Instant = () => Instant.now()) extends Metric with Reset with Ticks { private val lock = new ReentrantReadWriteLock() private var times: List[Instant] = List() @@ -143,12 +143,12 @@ case class Rate(period: Duration = 1 minute) extends Metric with Reset with Tick def rate: BigDecimal = lock.read(times.size) def hit(amount: Long = 1): Unit = lock.write { - val now = Instant.now() + val now = timeProvider() times = times ++ (0.toLong until amount).map(_ => now).toList } override def tick(): Unit = lock.write { - val cutTime = Instant.now().minusSeconds(period.toSeconds) + val cutTime = timeProvider().minusSeconds(period.toSeconds) times = times.filter(v => v.compareTo(cutTime) >= 0) } diff --git a/core/src/test/scala/acsgh/metrics/scala/model/PercentileTest.scala b/core/src/test/scala/acsgh/metrics/scala/model/PercentileTest.scala index b55ea0c..8f10b7c 100644 --- a/core/src/test/scala/acsgh/metrics/scala/model/PercentileTest.scala +++ b/core/src/test/scala/acsgh/metrics/scala/model/PercentileTest.scala @@ -57,18 +57,4 @@ class PercentileTest extends FlatSpec with Matchers { metric.update(10) metric.reset() } - - it should "get value" in { - val metric = Percentile() - metric.update(10) - // metric.values should be( - // ListMap( - // "events" -> metric.events, - // "total" -> metric.total, - // "min" -> metric.min, - // "max" -> metric.max, - // "mean" -> metric.mean, - // ) - // ) - } } diff --git a/core/src/test/scala/acsgh/metrics/scala/model/RateTest.scala b/core/src/test/scala/acsgh/metrics/scala/model/RateTest.scala new file mode 100644 index 0000000..bdba722 --- /dev/null +++ b/core/src/test/scala/acsgh/metrics/scala/model/RateTest.scala @@ -0,0 +1,52 @@ +package acsgh.metrics.scala.model + +import java.time.Instant + +import org.scalatest._ + +import scala.collection.immutable.ListMap +import scala.language.reflectiveCalls + +class RateTest extends FlatSpec with Matchers { + + "Rate" should "start at 0" in { + val metric = Rate() + metric.values should be( + ListMap( + "rate" -> 0 + ) + ) + } + + it should "update" in { + var now = Instant.now().minusSeconds(65) + + val metric = Rate(timeProvider = () => now) + metric.hit(50) + metric.values should be( + ListMap( + "rate" -> 50 + ) + ) + + now = now.plusSeconds(65) + metric.tick() + metric.hit(10) + metric.values should be( + ListMap( + "rate" -> 10 + ) + ) + } + + it should "reset" in { + val metric = Rate() + metric.hit(1) + metric.reset() + metric.values should be( + ListMap( + "rate" -> 0 + ) + ) + } +} diff --git a/core/src/test/scala/acsgh/metrics/scala/model/SamplerTest.scala b/core/src/test/scala/acsgh/metrics/scala/model/SamplerTest.scala index f284d17..70501cc 100644 --- a/core/src/test/scala/acsgh/metrics/scala/model/SamplerTest.scala +++ b/core/src/test/scala/acsgh/metrics/scala/model/SamplerTest.scala @@ -9,43 +9,64 @@ class SamplerTest extends FlatSpec with Matchers { "Sampler" should "start at 0" in { val metric = Sampler() - metric.events should be(0) - metric.total should be(0) - metric.min should be(0) - metric.max should be(0) - metric.mean should be(0) + metric.values should be( + ListMap( + "events" -> 0, + "total" -> 0.0, + "min" -> 0.0, + "max" -> 0.0, + "mean" -> 0.0 + ) + ) } it should "update" in { val metric = Sampler() - metric.events should be(0) - metric.total should be(0) - metric.min should be(0) - metric.max should be(0) - metric.mean should be(0) + + metric.values should be( + ListMap( + "events" -> 0, + "total" -> 0.0, + "min" -> 0.0, + "max" -> 0.0, + "mean" -> 0.0 + ) + ) metric.update(10) - metric.events should be(1) - metric.total should be(10) - metric.min should be(10) - metric.max should be(10) - metric.mean should be(10) + metric.values should be( + ListMap( + "events" -> 1, + "total" -> 10.0, + "min" -> 10.0, + "max" -> 10.0, + "mean" -> 10.0 + ) + ) metric.update(20) - metric.events should be(2) - metric.total should be(30) - metric.min should be(10) - metric.max should be(20) - metric.mean should be(15) + metric.values should be( + ListMap( + "events" -> 2, + "total" -> 30.0, + "min" -> 10.0, + "max" -> 20.0, + "mean" -> 15.0 + ) + ) } it should "reset" in { val metric = Sampler() metric.update(10) metric.reset() - metric.events should be(0) - metric.total should be(0) - metric.min should be(0) - metric.max should be(0) - metric.mean should be(0) + metric.values should be( + ListMap( + "events" -> 0, + "total" -> 0.0, + "min" -> 0.0, + "max" -> 0.0, + "mean" -> 0.0 + ) + ) } it should "get value" in {