histogram_test.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package prometheus
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/google/go-cmp/cmp"
  6. "github.com/google/go-cmp/cmp/cmpopts"
  7. dto "github.com/prometheus/client_model/go"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. "github.com/ydb-platform/ydb/library/go/core/metrics"
  11. "github.com/ydb-platform/ydb/library/go/ptr"
  12. "google.golang.org/protobuf/testing/protocmp"
  13. )
  14. func TestHistogram_RecordValue(t *testing.T) {
  15. rg := NewRegistry(NewRegistryOpts())
  16. h := rg.Histogram("test_histogram_record_value",
  17. metrics.NewBuckets(0.1, 1.0, 15.47, 42.0, 128.256),
  18. )
  19. for _, value := range []float64{0.5, 0.7, 34.1234, 127} {
  20. h.RecordValue(value)
  21. }
  22. expectBuckets := []*dto.Bucket{
  23. {CumulativeCount: ptr.Uint64(0), UpperBound: ptr.Float64(0.1)},
  24. {CumulativeCount: ptr.Uint64(2), UpperBound: ptr.Float64(1.0)},
  25. {CumulativeCount: ptr.Uint64(2), UpperBound: ptr.Float64(15.47)},
  26. {CumulativeCount: ptr.Uint64(3), UpperBound: ptr.Float64(42.0)},
  27. {CumulativeCount: ptr.Uint64(4), UpperBound: ptr.Float64(128.256)},
  28. }
  29. gathered, err := rg.Gather()
  30. require.NoError(t, err)
  31. resBuckets := gathered[0].Metric[0].GetHistogram().GetBucket()
  32. cmpOpts := []cmp.Option{
  33. cmpopts.IgnoreUnexported(),
  34. protocmp.Transform(),
  35. }
  36. assert.True(t, cmp.Equal(expectBuckets, resBuckets, cmpOpts...), cmp.Diff(expectBuckets, resBuckets, cmpOpts...))
  37. }
  38. func TestDurationHistogram_RecordDuration(t *testing.T) {
  39. rg := NewRegistry(NewRegistryOpts())
  40. ht := rg.DurationHistogram("test_histogram_record_value",
  41. metrics.NewDurationBuckets(
  42. 1*time.Millisecond, // 0.1
  43. 1*time.Second, // 1.0
  44. 15*time.Second+470*time.Millisecond, // 15.47
  45. 42*time.Second, // 42.0
  46. 128*time.Second+256*time.Millisecond, // 128.256
  47. ),
  48. )
  49. values := []time.Duration{
  50. 500 * time.Millisecond,
  51. 700 * time.Millisecond,
  52. 34*time.Second + 1234*time.Millisecond,
  53. 127 * time.Second,
  54. }
  55. for _, value := range values {
  56. ht.RecordDuration(value)
  57. }
  58. expectBuckets := []*dto.Bucket{
  59. {CumulativeCount: ptr.Uint64(0), UpperBound: ptr.Float64(0.001)},
  60. {CumulativeCount: ptr.Uint64(2), UpperBound: ptr.Float64(1)},
  61. {CumulativeCount: ptr.Uint64(2), UpperBound: ptr.Float64(15.47)},
  62. {CumulativeCount: ptr.Uint64(3), UpperBound: ptr.Float64(42)},
  63. {CumulativeCount: ptr.Uint64(4), UpperBound: ptr.Float64(128.256)},
  64. }
  65. gathered, err := rg.Gather()
  66. require.NoError(t, err)
  67. resBuckets := gathered[0].Metric[0].GetHistogram().GetBucket()
  68. cmpOpts := []cmp.Option{
  69. cmpopts.IgnoreUnexported(),
  70. protocmp.Transform(),
  71. }
  72. assert.True(t, cmp.Equal(expectBuckets, resBuckets, cmpOpts...), cmp.Diff(expectBuckets, resBuckets, cmpOpts...))
  73. }