Skip to content

Commit

Permalink
Finish basic metrics tests
Browse files Browse the repository at this point in the history
  • Loading branch information
acsgh committed Nov 17, 2019
1 parent 0736d7a commit b1acb8a
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 43 deletions.
Expand Up @@ -2,6 +2,6 @@ package acsgh.metrics.scala

import com.acsgh.common.scala.log.LogSupport

class MetricRegistry extends LogSupport{
class MetricRegistry extends LogSupport {

}
6 changes: 3 additions & 3 deletions core/src/main/scala/acsgh/metrics/scala/model/Metric.scala
Expand Up @@ -135,20 +135,20 @@ 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()

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)
}

Expand Down
14 changes: 0 additions & 14 deletions core/src/test/scala/acsgh/metrics/scala/model/PercentileTest.scala
Expand Up @@ -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,
// )
// )
}
}
52 changes: 52 additions & 0 deletions 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
)
)
}
}
71 changes: 46 additions & 25 deletions core/src/test/scala/acsgh/metrics/scala/model/SamplerTest.scala
Expand Up @@ -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 {
Expand Down

0 comments on commit b1acb8a

Please sign in to comment.