Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

146 строки
3.6 KiB

  1. // Copyright 2018, OpenCensus Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. package view
  16. import (
  17. "reflect"
  18. "testing"
  19. "time"
  20. "github.com/google/go-cmp/cmp"
  21. "github.com/google/go-cmp/cmp/cmpopts"
  22. "go.opencensus.io/exemplar"
  23. )
  24. func TestDataClone(t *testing.T) {
  25. dist := newDistributionData([]float64{1, 2, 3, 4})
  26. dist.Count = 7
  27. dist.Max = 11
  28. dist.Min = 1
  29. dist.CountPerBucket = []int64{0, 2, 3, 2}
  30. dist.Mean = 4
  31. dist.SumOfSquaredDev = 1.2
  32. tests := []struct {
  33. name string
  34. src AggregationData
  35. }{
  36. {
  37. name: "count data",
  38. src: &CountData{Value: 5},
  39. },
  40. {
  41. name: "distribution data",
  42. src: dist,
  43. },
  44. {
  45. name: "sum data",
  46. src: &SumData{Value: 65.7},
  47. },
  48. }
  49. for _, tt := range tests {
  50. t.Run(tt.name, func(t *testing.T) {
  51. got := tt.src.clone()
  52. if !reflect.DeepEqual(got, tt.src) {
  53. t.Errorf("AggregationData.clone() = %v, want %v", got, tt.src)
  54. }
  55. // TODO(jbd): Make sure that data is deep copied.
  56. if got == tt.src {
  57. t.Errorf("AggregationData.clone() returned the same pointer")
  58. }
  59. })
  60. }
  61. }
  62. func TestDistributionData_addSample(t *testing.T) {
  63. dd := newDistributionData([]float64{1, 2})
  64. t1, _ := time.Parse("Mon Jan 2 15:04:05 -0700 MST 2006", "Mon Jan 2 15:04:05 -0700 MST 2006")
  65. e1 := &exemplar.Exemplar{
  66. Attachments: exemplar.Attachments{
  67. "tag:X": "Y",
  68. "tag:A": "B",
  69. },
  70. Timestamp: t1,
  71. Value: 0.5,
  72. }
  73. dd.addSample(e1)
  74. want := &DistributionData{
  75. Count: 1,
  76. CountPerBucket: []int64{1, 0, 0},
  77. ExemplarsPerBucket: []*exemplar.Exemplar{e1, nil, nil},
  78. Max: 0.5,
  79. Min: 0.5,
  80. Mean: 0.5,
  81. SumOfSquaredDev: 0,
  82. }
  83. if diff := cmpDD(dd, want); diff != "" {
  84. t.Fatalf("Unexpected DistributionData -got +want: %s", diff)
  85. }
  86. t2 := t1.Add(time.Microsecond)
  87. e2 := &exemplar.Exemplar{
  88. Attachments: exemplar.Attachments{
  89. "tag:X": "Y",
  90. },
  91. Timestamp: t2,
  92. Value: 0.7,
  93. }
  94. dd.addSample(e2)
  95. // Previous exemplar should be preserved, since it has more annotations.
  96. want = &DistributionData{
  97. Count: 2,
  98. CountPerBucket: []int64{2, 0, 0},
  99. ExemplarsPerBucket: []*exemplar.Exemplar{e1, nil, nil},
  100. Max: 0.7,
  101. Min: 0.5,
  102. Mean: 0.6,
  103. SumOfSquaredDev: 0,
  104. }
  105. if diff := cmpDD(dd, want); diff != "" {
  106. t.Fatalf("Unexpected DistributionData -got +want: %s", diff)
  107. }
  108. t3 := t2.Add(time.Microsecond)
  109. e3 := &exemplar.Exemplar{
  110. Attachments: exemplar.Attachments{
  111. exemplar.KeyTraceID: "abcd",
  112. },
  113. Timestamp: t3,
  114. Value: 0.2,
  115. }
  116. dd.addSample(e3)
  117. // Exemplar should be replaced since it has a trace_id.
  118. want = &DistributionData{
  119. Count: 3,
  120. CountPerBucket: []int64{3, 0, 0},
  121. ExemplarsPerBucket: []*exemplar.Exemplar{e3, nil, nil},
  122. Max: 0.7,
  123. Min: 0.2,
  124. Mean: 0.4666666666666667,
  125. SumOfSquaredDev: 0,
  126. }
  127. if diff := cmpDD(dd, want); diff != "" {
  128. t.Fatalf("Unexpected DistributionData -got +want: %s", diff)
  129. }
  130. }
  131. func cmpDD(got, want *DistributionData) string {
  132. return cmp.Diff(got, want, cmpopts.IgnoreFields(DistributionData{}, "SumOfSquaredDev"), cmpopts.IgnoreUnexported(DistributionData{}))
  133. }