github.com/alibaba/sentinel-golang@v1.0.4/core/stat/base/metric_bucket_test.go (about) 1 // Copyright 1999-2020 Alibaba Group Holding Ltd. 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 base 16 17 import ( 18 "sync" 19 "testing" 20 "unsafe" 21 22 "github.com/alibaba/sentinel-golang/core/base" 23 "github.com/stretchr/testify/assert" 24 ) 25 26 func Test_metricBucket_MemSize(t *testing.T) { 27 mb := NewMetricBucket() 28 t.Log("mb:", mb) 29 size := unsafe.Sizeof(*mb) 30 if size != 56 { 31 t.Error("unexpect memory size of MetricBucket") 32 } 33 } 34 35 func Test_metricBucket_Normal(t *testing.T) { 36 mb := NewMetricBucket() 37 38 for i := 0; i < 120; i++ { 39 if i%6 == 0 { 40 mb.Add(base.MetricEventPass, 1) 41 } else if i%6 == 1 { 42 mb.Add(base.MetricEventBlock, 1) 43 } else if i%6 == 2 { 44 mb.Add(base.MetricEventComplete, 1) 45 } else if i%6 == 3 { 46 mb.Add(base.MetricEventError, 1) 47 } else if i%6 == 4 { 48 mb.AddRt(100) 49 } else if i%6 == 5 { 50 mb.UpdateConcurrency(int32(i)) 51 } else { 52 t.Error("unexpect idx") 53 } 54 } 55 56 if mb.Get(base.MetricEventPass) != 20 { 57 t.Error("unexpect count MetricEventBlock") 58 } 59 if mb.Get(base.MetricEventBlock) != 20 { 60 t.Error("unexpect count MetricEventBlock") 61 } 62 if mb.Get(base.MetricEventComplete) != 20 { 63 t.Error("unexpect count MetricEventComplete") 64 } 65 if mb.Get(base.MetricEventError) != 20 { 66 t.Error("unexpect count MetricEventError") 67 } 68 if mb.Get(base.MetricEventRt) != 20*100 { 69 t.Error("unexpect count MetricEventRt") 70 } 71 if mb.MaxConcurrency() != 119 { 72 t.Error("unexpect count MetricEventConcurrency") 73 } 74 } 75 76 func Test_metricBucket_Concurrent(t *testing.T) { 77 mb := NewMetricBucket() 78 wg := &sync.WaitGroup{} 79 wg.Add(5000) 80 81 for i := 0; i < 1000; i++ { 82 go func() { 83 mb.Add(base.MetricEventPass, 1) 84 wg.Done() 85 }() 86 } 87 88 for i := 0; i < 1000; i++ { 89 go func() { 90 mb.Add(base.MetricEventBlock, 2) 91 wg.Done() 92 }() 93 } 94 95 for i := 0; i < 1000; i++ { 96 go func() { 97 mb.Add(base.MetricEventComplete, 3) 98 wg.Done() 99 }() 100 } 101 102 for i := 0; i < 1000; i++ { 103 go func() { 104 mb.Add(base.MetricEventError, 4) 105 wg.Done() 106 }() 107 } 108 109 for i := 0; i < 1000; i++ { 110 go func(c uint64) { 111 mb.AddRt(int64(c)) 112 wg.Done() 113 }(uint64(i)) 114 } 115 wg.Wait() 116 117 if mb.Get(base.MetricEventPass) != 1000 { 118 t.Error("unexpect count MetricEventBlock") 119 } 120 if mb.Get(base.MetricEventBlock) != 2000 { 121 t.Error("unexpect count MetricEventBlock") 122 } 123 if mb.Get(base.MetricEventComplete) != 3000 { 124 t.Error("unexpect count MetricEventComplete") 125 } 126 if mb.Get(base.MetricEventError) != 4000 { 127 t.Error("unexpect count MetricEventError") 128 } 129 130 totalRt := (0 + 999) * 1000 / 2 131 if mb.Get(base.MetricEventRt) != int64(totalRt) { 132 t.Error("unexpect count MetricEventRt") 133 } 134 } 135 136 func Test_Reset(t *testing.T) { 137 mb := NewMetricBucket() 138 mb.AddRt(100) 139 mb.reset() 140 rt := mb.MinRt() 141 mc := mb.MaxConcurrency() 142 assert.True(t, rt == base.DefaultStatisticMaxRt) 143 assert.True(t, mc == int32(0)) 144 }