pcp_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package pcp
  2. import (
  3. "testing"
  4. "github.com/performancecopilot/speed"
  5. "github.com/go-kit/kit/metrics/teststat"
  6. )
  7. func TestCounter(t *testing.T) {
  8. r, err := NewReporter("test_counter")
  9. if err != nil {
  10. t.Fatal(err)
  11. }
  12. counter, err := r.NewCounter("speed_counter")
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. counter = counter.With("label values", "not supported").(*Counter)
  17. value := func() float64 { f := counter.c.Val(); return float64(f) }
  18. if err := teststat.TestCounter(counter, value); err != nil {
  19. t.Fatal(err)
  20. }
  21. }
  22. func TestGauge(t *testing.T) {
  23. r, err := NewReporter("test_gauge")
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. gauge, err := r.NewGauge("speed_gauge")
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. gauge = gauge.With("label values", "not supported").(*Gauge)
  32. value := func() float64 { f := gauge.g.Val(); return f }
  33. if err := teststat.TestGauge(gauge, value); err != nil {
  34. t.Fatal(err)
  35. }
  36. }
  37. func TestHistogram(t *testing.T) {
  38. r, err := NewReporter("test_histogram")
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. histogram, err := r.NewHistogram("speed_histogram", 0, 3600000000, speed.OneUnit)
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. histogram = histogram.With("label values", "not supported").(*Histogram)
  47. quantiles := func() (float64, float64, float64, float64) {
  48. p50 := float64(histogram.Percentile(50))
  49. p90 := float64(histogram.Percentile(90))
  50. p95 := float64(histogram.Percentile(95))
  51. p99 := float64(histogram.Percentile(99))
  52. return p50, p90, p95, p99
  53. }
  54. if err := teststat.TestHistogram(histogram, quantiles, 0.01); err != nil {
  55. t.Fatal(err)
  56. }
  57. }