diff options
Diffstat (limited to 'kamon-core/src/test/scala/kamon/metric/instrument')
4 files changed, 49 insertions, 74 deletions
diff --git a/kamon-core/src/test/scala/kamon/metric/instrument/CounterSpec.scala b/kamon-core/src/test/scala/kamon/metric/instrument/CounterSpec.scala index 1a93e1f6..500a69c5 100644 --- a/kamon-core/src/test/scala/kamon/metric/instrument/CounterSpec.scala +++ b/kamon-core/src/test/scala/kamon/metric/instrument/CounterSpec.scala @@ -2,7 +2,6 @@ package kamon.metric.instrument import java.nio.LongBuffer -import kamon.metric.CollectionContext import org.scalatest.{ Matchers, WordSpec } class CounterSpec extends WordSpec with Matchers { diff --git a/kamon-core/src/test/scala/kamon/metric/instrument/GaugeSpec.scala b/kamon-core/src/test/scala/kamon/metric/instrument/GaugeSpec.scala index 9192d999..bd39652c 100644 --- a/kamon-core/src/test/scala/kamon/metric/instrument/GaugeSpec.scala +++ b/kamon-core/src/test/scala/kamon/metric/instrument/GaugeSpec.scala @@ -1,72 +1,62 @@ package kamon.metric.instrument import java.util.concurrent.atomic.AtomicLong - -import akka.actor.ActorSystem -import com.typesafe.config.ConfigFactory -import kamon.Kamon -import kamon.metric.{ Metrics, Scale, CollectionContext } -import org.scalatest.{ Matchers, WordSpecLike } +import kamon.metric.instrument.Histogram.DynamicRange +import kamon.testkit.BaseKamonSpec import scala.concurrent.duration._ -class GaugeSpec extends WordSpecLike with Matchers { - implicit val system = ActorSystem("gauge-spec", ConfigFactory.parseString( - """ - |kamon.metrics { - | flush-interval = 1 hour - | default-collection-context-buffer-size = 10 - | precision { - | default-gauge-precision { - | refresh-interval = 100 milliseconds - | highest-trackable-value = 999999999 - | significant-value-digits = 2 - | } - | } - |} - """.stripMargin)) +class GaugeSpec extends BaseKamonSpec("gauge-spec") { "a Gauge" should { - "automatically record the current value using the configured refresh-interval" in { - val numberOfValuesRecorded = new AtomicLong(0) - val gauge = Gauge.fromDefaultConfig(system) { () ⇒ numberOfValuesRecorded.addAndGet(1) } - + "automatically record the current value using the configured refresh-interval" in new GaugeFixture { + val (numberOfValuesRecorded, gauge) = createGauge() Thread.sleep(1.second.toMillis) + numberOfValuesRecorded.get() should be(10L +- 1L) gauge.cleanup } - "stop automatically recording after a call to cleanup" in { - val numberOfValuesRecorded = new AtomicLong(0) - val gauge = Gauge.fromDefaultConfig(system) { () ⇒ numberOfValuesRecorded.addAndGet(1) } - + "stop automatically recording after a call to cleanup" in new GaugeFixture { + val (numberOfValuesRecorded, gauge) = createGauge() Thread.sleep(1.second.toMillis) + gauge.cleanup numberOfValuesRecorded.get() should be(10L +- 1L) Thread.sleep(1.second.toMillis) + numberOfValuesRecorded.get() should be(10L +- 1L) } - "produce a Histogram snapshot including all the recorded values" in { - val numberOfValuesRecorded = new AtomicLong(0) - val gauge = Gauge.fromDefaultConfig(system) { () ⇒ numberOfValuesRecorded.addAndGet(1) } + "produce a Histogram snapshot including all the recorded values" in new GaugeFixture { + val (numberOfValuesRecorded, gauge) = createGauge() Thread.sleep(1.second.toMillis) gauge.cleanup - val snapshot = gauge.collect(Kamon(Metrics).buildDefaultCollectionContext) + val snapshot = gauge.collect(kamon.metrics.buildDefaultCollectionContext) snapshot.numberOfMeasurements should be(10L +- 1L) snapshot.min should be(1) snapshot.max should be(10L +- 1L) } - "not record the current value when doing a collection" in { - val numberOfValuesRecorded = new AtomicLong(0) - val gauge = Gauge(Histogram.Precision.Normal, 10000L, Scale.Unit, 1 hour, system)(() ⇒ numberOfValuesRecorded.addAndGet(1)) - - val snapshot = gauge.collect(Kamon(Metrics).buildDefaultCollectionContext) + "not record the current value when doing a collection" in new GaugeFixture { + val (numberOfValuesRecorded, gauge) = createGauge(10 seconds) + val snapshot = gauge.collect(kamon.metrics.buildDefaultCollectionContext) snapshot.numberOfMeasurements should be(0) numberOfValuesRecorded.get() should be(0) } } + + trait GaugeFixture { + def createGauge(refreshInterval: FiniteDuration = 100 millis): (AtomicLong, Gauge) = { + val recordedValuesCounter = new AtomicLong(0) + val gauge = Gauge(DynamicRange(1, 100, 2), refreshInterval, kamon.metrics.settings.refreshScheduler, { + () ⇒ recordedValuesCounter.addAndGet(1) + }) + + (recordedValuesCounter, gauge) + } + + } } diff --git a/kamon-core/src/test/scala/kamon/metric/instrument/HistogramSpec.scala b/kamon-core/src/test/scala/kamon/metric/instrument/HistogramSpec.scala index c3060d4a..9a50e149 100644 --- a/kamon-core/src/test/scala/kamon/metric/instrument/HistogramSpec.scala +++ b/kamon-core/src/test/scala/kamon/metric/instrument/HistogramSpec.scala @@ -18,22 +18,13 @@ package kamon.metric.instrument import java.nio.LongBuffer -import com.typesafe.config.ConfigFactory -import kamon.metric.CollectionContext +import kamon.metric.instrument.Histogram.DynamicRange import org.scalatest.{ Matchers, WordSpec } import scala.util.Random class HistogramSpec extends WordSpec with Matchers { - val histogramConfig = ConfigFactory.parseString( - """ - | - |highest-trackable-value = 100000 - |significant-value-digits = 2 - | - """.stripMargin) - "a Histogram" should { "allow record values within the configured range" in new HistogramFixture { histogram.record(1000) @@ -109,7 +100,7 @@ class HistogramSpec extends WordSpec with Matchers { val buffer: LongBuffer = LongBuffer.allocate(10000) } - val histogram = Histogram.fromConfig(histogramConfig) + val histogram = Histogram(DynamicRange(1, 100000, 2)) def takeSnapshot(): Histogram.Snapshot = histogram.collect(collectionContext) } @@ -119,17 +110,20 @@ class HistogramSpec extends WordSpec with Matchers { val buffer: LongBuffer = LongBuffer.allocate(10000) } - val controlHistogram = Histogram.fromConfig(histogramConfig) - val histogramA = Histogram.fromConfig(histogramConfig) - val histogramB = Histogram.fromConfig(histogramConfig) + val controlHistogram = Histogram(DynamicRange(1, 100000, 2)) + val histogramA = Histogram(DynamicRange(1, 100000, 2)) + val histogramB = Histogram(DynamicRange(1, 100000, 2)) + + def takeSnapshotFrom(histogram: Histogram): InstrumentSnapshot = histogram.collect(collectionContext) - def takeSnapshotFrom(histogram: Histogram): Histogram.Snapshot = histogram.collect(collectionContext) + def assertEquals(left: InstrumentSnapshot, right: InstrumentSnapshot): Unit = { + val leftSnapshot = left.asInstanceOf[Histogram.Snapshot] + val rightSnapshot = right.asInstanceOf[Histogram.Snapshot] - def assertEquals(left: Histogram.Snapshot, right: Histogram.Snapshot): Unit = { - left.numberOfMeasurements should equal(right.numberOfMeasurements) - left.min should equal(right.min) - left.max should equal(right.max) - left.recordsIterator.toStream should contain theSameElementsAs (right.recordsIterator.toStream) + leftSnapshot.numberOfMeasurements should equal(rightSnapshot.numberOfMeasurements) + leftSnapshot.min should equal(rightSnapshot.min) + leftSnapshot.max should equal(rightSnapshot.max) + leftSnapshot.recordsIterator.toStream should contain theSameElementsAs (rightSnapshot.recordsIterator.toStream) } } } diff --git a/kamon-core/src/test/scala/kamon/metric/instrument/MinMaxCounterSpec.scala b/kamon-core/src/test/scala/kamon/metric/instrument/MinMaxCounterSpec.scala index 2c11adc3..7a3d7aa3 100644 --- a/kamon-core/src/test/scala/kamon/metric/instrument/MinMaxCounterSpec.scala +++ b/kamon-core/src/test/scala/kamon/metric/instrument/MinMaxCounterSpec.scala @@ -19,19 +19,11 @@ import java.nio.LongBuffer import akka.actor._ import akka.testkit.TestProbe -import com.typesafe.config.ConfigFactory -import kamon.metric.CollectionContext -import kamon.metric.instrument.Histogram.MutableRecord -import org.scalatest.{ Matchers, WordSpecLike } - -class MinMaxCounterSpec extends WordSpecLike with Matchers { - implicit val system = ActorSystem("min-max-counter-spec") - val minMaxCounterConfig = ConfigFactory.parseString( - """ - |refresh-interval = 1 hour - |highest-trackable-value = 1000 - |significant-value-digits = 2 - """.stripMargin) +import kamon.metric.instrument.Histogram.{ DynamicRange, MutableRecord } +import kamon.testkit.BaseKamonSpec +import scala.concurrent.duration._ + +class MinMaxCounterSpec extends BaseKamonSpec("min-max-counter-spec") { "the MinMaxCounter" should { "track ascending tendencies" in new MinMaxCounterFixture { @@ -104,7 +96,7 @@ class MinMaxCounterSpec extends WordSpecLike with Matchers { workers foreach (_ ! "increment") for (refresh ← 1 to 1000) { collectCounterSnapshot() - Thread.sleep(10) + Thread.sleep(1) } monitor.expectNoMsg() @@ -117,7 +109,7 @@ class MinMaxCounterSpec extends WordSpecLike with Matchers { val buffer: LongBuffer = LongBuffer.allocate(64) } - val mmCounter = MinMaxCounter.fromConfig(minMaxCounterConfig, system).asInstanceOf[PaddedMinMaxCounter] + val mmCounter = MinMaxCounter(DynamicRange(1, 1000, 2), 1 hour, kamon.metrics.settings.refreshScheduler) mmCounter.cleanup // cancel the refresh schedule def collectCounterSnapshot(): Histogram.Snapshot = mmCounter.collect(collectionContext) |