aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/test/scala/kamon/metric/instrument/HistogramSpec.scala
blob: 9a50e14915a57843abaec52fde6383517c463ea1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * =========================================================================================
 * Copyright © 2013 the kamon project <http://kamon.io/>
 *
 * 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.nio.LongBuffer

import kamon.metric.instrument.Histogram.DynamicRange
import org.scalatest.{ Matchers, WordSpec }

import scala.util.Random

class HistogramSpec extends WordSpec with Matchers {

  "a Histogram" should {
    "allow record values within the configured range" in new HistogramFixture {
      histogram.record(1000)
      histogram.record(5000, count = 100)
      histogram.record(10000)
    }

    "fail when recording values higher than the highest trackable value" in new HistogramFixture {
      intercept[IndexOutOfBoundsException] {
        histogram.record(1000000)
      }
    }

    "reset all recorded levels to zero after a snapshot collection" in new HistogramFixture {
      histogram.record(100)
      histogram.record(200)
      histogram.record(300)

      takeSnapshot().numberOfMeasurements should be(3)
      takeSnapshot().numberOfMeasurements should be(0)
    }

    "produce a snapshot" which {
      "supports min, max, percentile, sum and numberOfMeasurements operations" in new HistogramFixture {
        histogram.record(100)
        histogram.record(200, count = 200)
        histogram.record(300)
        histogram.record(900)

        val snapshot = takeSnapshot()

        snapshot.min should equal(100L +- 1L)
        snapshot.max should equal(900L +- 9L)
        snapshot.percentile(50.0D) should be(200)
        snapshot.percentile(99.5D) should be(300)
        snapshot.percentile(99.9D) should be(900)
        snapshot.sum should be(41300)
        snapshot.numberOfMeasurements should be(203)

      }

      "can be merged with another snapshot" in new MultipleHistogramFixture {
        val random = new Random(System.nanoTime())

        for (repetitions  1 to 1000) {
          // Put some values on A and Control
          for (_  1 to 1000) {
            val newRecording = random.nextInt(100000)
            controlHistogram.record(newRecording)
            histogramA.record(newRecording)
          }

          // Put some values on B and Control
          for (_  1 to 2000) {
            val newRecording = random.nextInt(100000)
            controlHistogram.record(newRecording)
            histogramB.record(newRecording)
          }

          val controlSnapshot = takeSnapshotFrom(controlHistogram)
          val histogramASnapshot = takeSnapshotFrom(histogramA)
          val histogramBSnapshot = takeSnapshotFrom(histogramB)

          assertEquals(controlSnapshot, histogramASnapshot.merge(histogramBSnapshot, collectionContext))
          assertEquals(controlSnapshot, histogramBSnapshot.merge(histogramASnapshot, collectionContext))
        }
      }
    }
  }

  trait HistogramFixture {
    val collectionContext = new CollectionContext {
      val buffer: LongBuffer = LongBuffer.allocate(10000)
    }

    val histogram = Histogram(DynamicRange(1, 100000, 2))

    def takeSnapshot(): Histogram.Snapshot = histogram.collect(collectionContext)
  }

  trait MultipleHistogramFixture {
    val collectionContext = new CollectionContext {
      val buffer: LongBuffer = LongBuffer.allocate(10000)
    }

    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 assertEquals(left: InstrumentSnapshot, right: InstrumentSnapshot): Unit = {
      val leftSnapshot = left.asInstanceOf[Histogram.Snapshot]
      val rightSnapshot = right.asInstanceOf[Histogram.Snapshot]

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