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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
|
// Copyright 2014 The Cockroach Authors.
//
// 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. See the AUTHORS file
// for names of contributors.
//
// Author: Tyler Neely (t@jujit.su)
package loghisto
import (
"fmt"
"math"
"runtime"
"testing"
"time"
)
func ExampleMetricSystem() {
ms := NewMetricSystem(time.Microsecond, true)
ms.Start()
myMetricStream := make(chan *ProcessedMetricSet, 2)
ms.SubscribeToProcessedMetrics(myMetricStream)
timeToken := ms.StartTimer("submit_metrics")
ms.Counter("range_splits", 1)
ms.Histogram("some_ipc_latency", 123)
timeToken.Stop()
processedMetricSet := <-myMetricStream
ms.UnsubscribeFromProcessedMetrics(myMetricStream)
m := processedMetricSet.Metrics
example := []struct {
Name string
Value float64
}{
{
"total range splits during the process lifetime",
m["range_splits"],
}, {
"range splits in this period",
m["range_splits_rate"],
}, {
"some_ipc 99.9th percentile",
m["some_ipc_latency_99.9"],
}, {
"some_ipc max",
m["some_ipc_latency_max"],
}, {
"some_ipc calls this period",
m["some_ipc_latency_count"],
}, {
"some_ipc calls during the process lifetime",
m["some_ipc_latency_agg_count"],
}, {
"some_ipc total latency this period",
m["some_ipc_latency_sum"],
}, {
"some_ipc mean this period",
m["some_ipc_latency_avg"],
}, {
"some_ipc aggregate man",
m["some_ipc_latency_agg_avg"],
}, {
"time spent submitting metrics this period",
m["submit_metrics_sum"],
}, {
"number of goroutines",
m["sys.NumGoroutine"],
}, {
"time spent in GC",
m["sys.PauseTotalNs"],
},
}
for _, nameValue := range example {
var result string
if nameValue.Value == float64(0) {
result = "NOT present"
} else {
result = "present"
}
fmt.Println(nameValue.Name, result)
}
ms.Stop()
// Output:
// total range splits during the process lifetime present
// range splits in this period present
// some_ipc 99.9th percentile present
// some_ipc max present
// some_ipc calls this period present
// some_ipc calls during the process lifetime present
// some_ipc total latency this period present
// some_ipc mean this period present
// some_ipc aggregate man present
// time spent submitting metrics this period present
// number of goroutines present
// time spent in GC present
}
func TestPercentile(t *testing.T) {
metrics := map[float64]uint64{
10: 9000,
25: 900,
33: 90,
47: 9,
500: 1,
}
percentileToExpected := map[float64]float64{
0: 10,
.99: 25,
.999: 33,
.9991: 47,
.9999: 47,
1: 500,
}
totalcount := uint64(0)
proportions := make([]proportion, 0, len(metrics))
for value, count := range metrics {
totalcount += count
proportions = append(proportions, proportion{Value: value, Count: count})
}
for p, expected := range percentileToExpected {
result, err := percentile(totalcount, proportions, p)
if err != nil {
t.Error("error:", err)
}
// results must be within 1% of their expected values.
diff := math.Abs(expected/result - 1)
if diff > .01 {
t.Errorf("percentile: %.04f, expected: %.04f, actual: %.04f, %% off: %.04f\n",
p, expected, result, diff*100)
}
}
}
func TestCompress(t *testing.T) {
toTest := []float64{
-421408208120481,
-1,
0,
1,
214141241241241,
}
for _, f := range toTest {
result := decompress(compress(f))
var diff float64
if result == 0 {
diff = math.Abs(f - result)
} else {
diff = math.Abs(f/result - 1)
}
if diff > .01 {
t.Errorf("expected: %f, actual: %f, %% off: %.04f\n",
f, result, diff*100)
}
}
}
func TestSysStats(t *testing.T) {
metricSystem := NewMetricSystem(time.Microsecond, true)
gauges := metricSystem.collectRawMetrics().Gauges
v, present := gauges["sys.Alloc"]
if v <= 0 || !present {
t.Errorf("expected positive reported allocated bytes, got %f\n", v)
}
}
func TestTimer(t *testing.T) {
metricSystem := NewMetricSystem(time.Microsecond, false)
token1 := metricSystem.StartTimer("timer1")
token2 := metricSystem.StartTimer("timer1")
time.Sleep(50 & time.Microsecond)
token1.Stop()
time.Sleep(5 * time.Microsecond)
token2.Stop()
token3 := metricSystem.StartTimer("timer1")
time.Sleep(10 * time.Microsecond)
token3.Stop()
result := metricSystem.processMetrics(metricSystem.collectRawMetrics()).Metrics
if result["timer1_min"] > result["timer1_50"] ||
result["timer1_50"] > result["timer1_max"] {
t.Error("bad result map:", result)
}
}
func TestRate(t *testing.T) {
metricSystem := NewMetricSystem(time.Microsecond, false)
metricSystem.Counter("rate1", 777)
time.Sleep(20 * time.Millisecond)
metrics := metricSystem.processMetrics(metricSystem.collectRawMetrics()).Metrics
if metrics["rate1_rate"] != 777 {
t.Error("count one value")
}
metricSystem.Counter("rate1", 1223)
time.Sleep(20 * time.Millisecond)
metrics = metricSystem.processMetrics(metricSystem.collectRawMetrics()).Metrics
if metrics["rate1_rate"] != 1223 {
t.Errorf("expected rate: 1223, actual: %f", metrics["rate1_rate"])
}
metricSystem.Counter("rate1", 1223)
metricSystem.Counter("rate1", 1223)
time.Sleep(20 * time.Millisecond)
metrics = metricSystem.processMetrics(metricSystem.collectRawMetrics()).Metrics
if metrics["rate1_rate"] != 2446 {
t.Errorf("expected rate: 2446, actual: %f", metrics["rate1_rate"])
}
}
func TestCounter(t *testing.T) {
metricSystem := NewMetricSystem(time.Microsecond, false)
metricSystem.Counter("counter1", 3290)
time.Sleep(20 * time.Millisecond)
metrics := metricSystem.processMetrics(metricSystem.collectRawMetrics()).Metrics
if metrics["counter1"] != 3290 {
t.Error("count one value", metrics)
}
metricSystem.Counter("counter1", 10000)
time.Sleep(20 * time.Millisecond)
metrics = metricSystem.processMetrics(metricSystem.collectRawMetrics()).Metrics
if metrics["counter1"] != 13290 {
t.Error("accumulate counts across broadcasts")
}
}
func TestUpdateSubscribers(t *testing.T) {
rawMetricStream := make(chan *RawMetricSet)
processedMetricStream := make(chan *ProcessedMetricSet)
metricSystem := NewMetricSystem(2*time.Microsecond, false)
metricSystem.SubscribeToRawMetrics(rawMetricStream)
metricSystem.SubscribeToProcessedMetrics(processedMetricStream)
metricSystem.Counter("counter5", 33)
go func() {
select {
case <-rawMetricStream:
case <-time.After(20 * time.Millisecond):
t.Error("received no raw metrics from the MetricSystem after 2 milliseconds.")
}
metricSystem.UnsubscribeFromRawMetrics(rawMetricStream)
}()
go func() {
select {
case <-processedMetricStream:
case <-time.After(20 * time.Millisecond):
t.Error("received no processed metrics from the MetricSystem after 2 milliseconds.")
}
metricSystem.UnsubscribeFromProcessedMetrics(processedMetricStream)
}()
metricSystem.Start()
time.Sleep(20 * time.Millisecond)
go func() {
select {
case <-rawMetricStream:
t.Error("received raw metrics from the MetricSystem after unsubscribing.")
default:
}
}()
go func() {
select {
case <-processedMetricStream:
t.Error("received processed metrics from the MetricSystem after unsubscribing.")
default:
}
}()
time.Sleep(20 * time.Millisecond)
}
func TestProcessedBroadcast(t *testing.T) {
processedMetricStream := make(chan *ProcessedMetricSet, 128)
metricSystem := NewMetricSystem(time.Microsecond, false)
metricSystem.SubscribeToProcessedMetrics(processedMetricStream)
metricSystem.Histogram("histogram1", 33)
metricSystem.Histogram("histogram1", 59)
metricSystem.Histogram("histogram1", 330000)
metricSystem.Start()
select {
case processedMetrics := <-processedMetricStream:
if int(processedMetrics.Metrics["histogram1_sum"]) != 331132 {
t.Error("expected histogram1_sum to be 331132, instead was",
processedMetrics.Metrics["histogram1_sum"])
}
if int(processedMetrics.Metrics["histogram1_agg_avg"]) != 110377 {
t.Error("expected histogram1_agg_avg to be 110377, instead was",
processedMetrics.Metrics["histogram1_agg_avg"])
}
if int(processedMetrics.Metrics["histogram1_count"]) != 3 {
t.Error("expected histogram1_count to be 3, instead was",
processedMetrics.Metrics["histogram1_count"])
}
case <-time.After(20 * time.Millisecond):
t.Error("received no metrics from the MetricSystem after 2 milliseconds.")
}
metricSystem.UnsubscribeFromProcessedMetrics(processedMetricStream)
metricSystem.Stop()
}
func TestRawBroadcast(t *testing.T) {
rawMetricStream := make(chan *RawMetricSet, 128)
metricSystem := NewMetricSystem(time.Microsecond, false)
metricSystem.SubscribeToRawMetrics(rawMetricStream)
metricSystem.Counter("counter2", 10)
metricSystem.Counter("counter2", 111)
metricSystem.Start()
select {
case rawMetrics := <-rawMetricStream:
if rawMetrics.Counters["counter2"] != 121 {
t.Error("expected counter2 to be 121, instead was",
rawMetrics.Counters["counter2"])
}
if rawMetrics.Rates["counter2"] != 121 {
t.Error("expected counter2 rate to be 121, instead was",
rawMetrics.Counters["counter2"])
}
case <-time.After(20 * time.Millisecond):
t.Error("received no metrics from the MetricSystem after 2 milliseconds.")
}
metricSystem.UnsubscribeFromRawMetrics(rawMetricStream)
metricSystem.Stop()
}
func TestMetricSystemStop(t *testing.T) {
metricSystem := NewMetricSystem(time.Microsecond, false)
startingRoutines := runtime.NumGoroutine()
metricSystem.Start()
metricSystem.Stop()
time.Sleep(20 * time.Millisecond)
endRoutines := runtime.NumGoroutine()
if startingRoutines < endRoutines {
t.Errorf("lingering goroutines have not been cleaned up: "+
"before: %d, after: %d\n", startingRoutines, endRoutines)
}
}
|