From e9d2636be0a75bffacf0e48fc1b26d54207b18e7 Mon Sep 17 00:00:00 2001 From: Ivan Topolnjak Date: Sun, 11 Jun 2017 17:04:16 +0200 Subject: add HistogramSpec, move counter and mmcounter specs --- .../test/scala/kamon/metric/HistogramSpec.scala | 69 ++++++++++++++++ .../scala/kamon/metric/LongAdderCounterSpec.scala | 63 +++++++++++++++ .../scala/kamon/metric/MinMaxCounterSpec.scala | 91 +++++++++++++++++++++ .../metric/instrument/LongAdderCounterSpec.scala | 64 --------------- .../metric/instrument/MinMaxCounterSpec.scala | 92 ---------------------- 5 files changed, 223 insertions(+), 156 deletions(-) create mode 100644 kamon-core/src/test/scala/kamon/metric/HistogramSpec.scala create mode 100644 kamon-core/src/test/scala/kamon/metric/LongAdderCounterSpec.scala create mode 100644 kamon-core/src/test/scala/kamon/metric/MinMaxCounterSpec.scala delete mode 100644 kamon-core/src/test/scala/kamon/metric/instrument/LongAdderCounterSpec.scala delete mode 100644 kamon-core/src/test/scala/kamon/metric/instrument/MinMaxCounterSpec.scala (limited to 'kamon-core/src/test') diff --git a/kamon-core/src/test/scala/kamon/metric/HistogramSpec.scala b/kamon-core/src/test/scala/kamon/metric/HistogramSpec.scala new file mode 100644 index 00000000..ee0de53b --- /dev/null +++ b/kamon-core/src/test/scala/kamon/metric/HistogramSpec.scala @@ -0,0 +1,69 @@ +package kamon.metric + +import kamon.Kamon +import org.scalatest.{Matchers, WordSpec} +import kamon.util.MeasurementUnit._ + + +class HistogramSpec extends WordSpec with Matchers { + import HistogramTestHelper.HistogramMetricSyntax + + "a Histogram" should { + "record values and reset internal state when a snapshot is taken" in { + val histogram = Kamon.histogram("test", unit = time.nanoseconds) + histogram.record(100) + histogram.record(150, 998) + histogram.record(200) + + val distribution = histogram.distribution() + distribution.min shouldBe(100) + distribution.max shouldBe(200) + distribution.count shouldBe(1000) + distribution.buckets.length shouldBe 3 + distribution.buckets.map(b => (b.value, b.frequency)) should contain.allOf( + (100 -> 1), + (150 -> 998), + (200 -> 1) + ) + + val emptyDistribution = histogram.distribution() + emptyDistribution.min shouldBe(0) + emptyDistribution.max shouldBe(0) + emptyDistribution.count shouldBe(0) + emptyDistribution.buckets.length shouldBe 0 + } + + "[private api] record values and optionally keep the internal state when a snapshot is taken" in { + val histogram = Kamon.histogram("test", unit = time.nanoseconds) + histogram.record(100) + histogram.record(150, 998) + histogram.record(200) + + val distribution = { + histogram.distribution(resetState = false) // first one gets discarded + histogram.distribution(resetState = false) + } + + distribution.min shouldBe(100) + distribution.max shouldBe(200) + distribution.count shouldBe(1000) + distribution.buckets.length shouldBe 3 + distribution.buckets.map(b => (b.value, b.frequency)) should contain.allOf( + (100 -> 1), + (150 -> 998), + (200 -> 1) + ) + } + } +} + +object HistogramTestHelper { + + implicit class HistogramMetricSyntax(metric: HistogramMetric) { + def distribution(resetState: Boolean = true): Distribution = + metric.refine(Map.empty[String, String]) match { + case h: AtomicHdrHistogram => h.snapshot(resetState).distribution + case h: HdrHistogram => h.snapshot(resetState).distribution + } + } +} diff --git a/kamon-core/src/test/scala/kamon/metric/LongAdderCounterSpec.scala b/kamon-core/src/test/scala/kamon/metric/LongAdderCounterSpec.scala new file mode 100644 index 00000000..226b8f43 --- /dev/null +++ b/kamon-core/src/test/scala/kamon/metric/LongAdderCounterSpec.scala @@ -0,0 +1,63 @@ +/* ========================================================================================= + * Copyright © 2013-2017 the kamon project + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language governing permissions + * and limitations under the License. + * ========================================================================================= + */ + +package kamon.metric + +import kamon.util.MeasurementUnit +import org.scalatest.{Matchers, WordSpec} + +class LongAdderCounterSpec extends WordSpec with Matchers { + + "a LongAdderCounter" should { + "allow unit and bundled increments" in { + val counter = buildCounter("unit-increments") + counter.increment() + counter.increment() + counter.increment(40) + + counter.snapshot().value shouldBe 42 + } + + "warn the user and ignore attempts to decrement the counter" in { + val counter = buildCounter("attempt-to-decrement") + counter.increment(100) + counter.increment(100) + counter.increment(100) + + counter.snapshot().value shouldBe 300 + } + + "reset the internal state to zero after taking snapshots as a default behavior" in { + val counter = buildCounter("reset-after-snapshot") + counter.increment() + counter.increment(10) + + counter.snapshot().value shouldBe 11 + counter.snapshot().value shouldBe 0 + } + + "optionally leave the internal state unchanged" in { + val counter = buildCounter("reset-after-snapshot") + counter.increment() + counter.increment(10) + + counter.snapshot(resetState = false).value shouldBe 11 + counter.snapshot(resetState = false).value shouldBe 11 + } + } + + def buildCounter(name: String, tags: Map[String, String] = Map.empty, unit: MeasurementUnit = MeasurementUnit.none): LongAdderCounter = + new LongAdderCounter(name, tags, unit) +} diff --git a/kamon-core/src/test/scala/kamon/metric/MinMaxCounterSpec.scala b/kamon-core/src/test/scala/kamon/metric/MinMaxCounterSpec.scala new file mode 100644 index 00000000..062ae99d --- /dev/null +++ b/kamon-core/src/test/scala/kamon/metric/MinMaxCounterSpec.scala @@ -0,0 +1,91 @@ +/* ========================================================================================= + * Copyright © 2013-2017 the kamon project + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language governing permissions + * and limitations under the License. + * ========================================================================================= + */ + + +package kamon.metric + +import java.time.Duration + +import kamon.util.MeasurementUnit +import org.scalatest.{Matchers, WordSpec} + +case class TemporalBucket(value: Long, frequency: Long) extends Bucket + +class MinMaxCounterSpec extends WordSpec with Matchers { + + "a MinMaxCounter" should { + "track ascending tendencies" in { + val mmCounter = buildMinMaxCounter("track-ascending") + mmCounter.increment() + mmCounter.increment(3) + mmCounter.increment() + + mmCounter.sample() + + val snapshot = mmCounter.snapshot() + + snapshot.distribution.min should be(0) + snapshot.distribution.max should be(5) + } + + "track descending tendencies" in { + val mmCounter = buildMinMaxCounter("track-descending") + mmCounter.increment(5) + mmCounter.decrement() + mmCounter.decrement(3) + mmCounter.decrement() + + mmCounter.sample() + + val snapshot = mmCounter.snapshot() + snapshot.distribution.min should be(0) + snapshot.distribution.max should be(5) + } + + "reset the min and max to the current value after taking a snapshot" in { + val mmCounter = buildMinMaxCounter("reset-min-max-to-current") + + mmCounter.increment(5) + mmCounter.decrement(3) + mmCounter.sample() + + val firstSnapshot = mmCounter.snapshot() + firstSnapshot.distribution.min should be(0) + firstSnapshot.distribution.max should be(5) + + mmCounter.sample() + + val secondSnapshot = mmCounter.snapshot() + secondSnapshot.distribution.min should be(2) + secondSnapshot.distribution.max should be(2) + } + + "report zero as the min and current values if the current value fell bellow zero" in { + val mmCounter = buildMinMaxCounter("report-zero") + + mmCounter.decrement(3) + + mmCounter.sample() + + val snapshot = mmCounter.snapshot() + + snapshot.distribution.min should be(0) + snapshot.distribution.max should be(0) + } + } + + def buildMinMaxCounter(name: String, tags: Map[String, String] = Map.empty, unit: MeasurementUnit = MeasurementUnit.none): SimpleMinMaxCounter = + new SimpleMinMaxCounter(name, tags, new AtomicHdrHistogram(name, tags, unit, dynamicRange = DynamicRange.Default), Duration.ofMillis(100)) +} \ No newline at end of file diff --git a/kamon-core/src/test/scala/kamon/metric/instrument/LongAdderCounterSpec.scala b/kamon-core/src/test/scala/kamon/metric/instrument/LongAdderCounterSpec.scala deleted file mode 100644 index d742b54f..00000000 --- a/kamon-core/src/test/scala/kamon/metric/instrument/LongAdderCounterSpec.scala +++ /dev/null @@ -1,64 +0,0 @@ -/* ========================================================================================= - * Copyright © 2013-2017 the kamon project - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the - * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, - * either express or implied. See the License for the specific language governing permissions - * and limitations under the License. - * ========================================================================================= - */ - -package kamon -package metric - -import kamon.util.MeasurementUnit -import org.scalatest.{Matchers, WordSpec} - -class LongAdderCounterSpec extends WordSpec with Matchers { - - "a LongAdderCounter" should { - "allow unit and bundled increments" in { - val counter = buildCounter("unit-increments") - counter.increment() - counter.increment() - counter.increment(40) - - counter.snapshot().value shouldBe 42 - } - - "warn the user and ignore attempts to decrement the counter" in { - val counter = buildCounter("attempt-to-decrement") - counter.increment(100) - counter.increment(100) - counter.increment(100) - - counter.snapshot().value shouldBe 300 - } - - "reset the internal state to zero after taking snapshots as a default behavior" in { - val counter = buildCounter("reset-after-snapshot") - counter.increment() - counter.increment(10) - - counter.snapshot().value shouldBe 11 - counter.snapshot().value shouldBe 0 - } - - "optionally leave the internal state unchanged" in { - val counter = buildCounter("reset-after-snapshot") - counter.increment() - counter.increment(10) - - counter.snapshot(resetState = false).value shouldBe 11 - counter.snapshot(resetState = false).value shouldBe 11 - } - } - - def buildCounter(name: String, tags: Map[String, String] = Map.empty, unit: MeasurementUnit = MeasurementUnit.none): LongAdderCounter = - new LongAdderCounter(name, tags, unit) -} diff --git a/kamon-core/src/test/scala/kamon/metric/instrument/MinMaxCounterSpec.scala b/kamon-core/src/test/scala/kamon/metric/instrument/MinMaxCounterSpec.scala deleted file mode 100644 index a21bcc96..00000000 --- a/kamon-core/src/test/scala/kamon/metric/instrument/MinMaxCounterSpec.scala +++ /dev/null @@ -1,92 +0,0 @@ -/* ========================================================================================= - * Copyright © 2013-2017 the kamon project - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the - * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, - * either express or implied. See the License for the specific language governing permissions - * and limitations under the License. - * ========================================================================================= - */ - - -package kamon.metric.instrument - -import java.time.Duration - -import kamon.metric.{AtomicHdrHistogram, Bucket, DynamicRange, SimpleMinMaxCounter} -import kamon.util.MeasurementUnit -import org.scalatest.{Matchers, WordSpec} - -case class TemporalBucket(value: Long, frequency: Long) extends Bucket - -class MinMaxCounterSpec extends WordSpec with Matchers { - - "a MinMaxCounter" should { - "track ascending tendencies" in { - val mmCounter = buildMinMaxCounter("track-ascending") - mmCounter.increment() - mmCounter.increment(3) - mmCounter.increment() - - mmCounter.sample() - - val snapshot = mmCounter.snapshot() - - snapshot.distribution.min should be(0) - snapshot.distribution.max should be(5) - } - - "track descending tendencies" in { - val mmCounter = buildMinMaxCounter("track-descending") - mmCounter.increment(5) - mmCounter.decrement() - mmCounter.decrement(3) - mmCounter.decrement() - - mmCounter.sample() - - val snapshot = mmCounter.snapshot() - snapshot.distribution.min should be(0) - snapshot.distribution.max should be(5) - } - - "reset the min and max to the current value after taking a snapshot" in { - val mmCounter = buildMinMaxCounter("reset-min-max-to-current") - - mmCounter.increment(5) - mmCounter.decrement(3) - mmCounter.sample() - - val firstSnapshot = mmCounter.snapshot() - firstSnapshot.distribution.min should be(0) - firstSnapshot.distribution.max should be(5) - - mmCounter.sample() - - val secondSnapshot = mmCounter.snapshot() - secondSnapshot.distribution.min should be(2) - secondSnapshot.distribution.max should be(2) - } - - "report zero as the min and current values if the current value fell bellow zero" in { - val mmCounter = buildMinMaxCounter("report-zero") - - mmCounter.decrement(3) - - mmCounter.sample() - - val snapshot = mmCounter.snapshot() - - snapshot.distribution.min should be(0) - snapshot.distribution.max should be(0) - } - } - - def buildMinMaxCounter(name: String, tags: Map[String, String] = Map.empty, unit: MeasurementUnit = MeasurementUnit.none): SimpleMinMaxCounter = - new SimpleMinMaxCounter(name, tags, new AtomicHdrHistogram(name, tags, unit, dynamicRange = DynamicRange.Default), Duration.ofMillis(100)) -} \ No newline at end of file -- cgit v1.2.3