github.com/alibaba/sentinel-golang@v1.0.4/core/stat/base_node.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 stat
    16  
    17  import (
    18  	"sync/atomic"
    19  
    20  	"github.com/alibaba/sentinel-golang/core/base"
    21  	"github.com/alibaba/sentinel-golang/core/config"
    22  	sbase "github.com/alibaba/sentinel-golang/core/stat/base"
    23  )
    24  
    25  type BaseStatNode struct {
    26  	sampleCount uint32
    27  	intervalMs  uint32
    28  
    29  	concurrency int32
    30  
    31  	arr    *sbase.BucketLeapArray
    32  	metric *sbase.SlidingWindowMetric
    33  }
    34  
    35  func NewBaseStatNode(sampleCount uint32, intervalInMs uint32) *BaseStatNode {
    36  	la := sbase.NewBucketLeapArray(config.GlobalStatisticSampleCountTotal(), config.GlobalStatisticIntervalMsTotal())
    37  	metric, _ := sbase.NewSlidingWindowMetric(sampleCount, intervalInMs, la)
    38  	return &BaseStatNode{
    39  		concurrency: 0,
    40  		sampleCount: sampleCount,
    41  		intervalMs:  intervalInMs,
    42  		arr:         la,
    43  		metric:      metric,
    44  	}
    45  }
    46  
    47  func (n *BaseStatNode) MetricsOnCondition(predicate base.TimePredicate) []*base.MetricItem {
    48  	return n.metric.SecondMetricsOnCondition(predicate)
    49  }
    50  
    51  func (n *BaseStatNode) GetQPS(event base.MetricEvent) float64 {
    52  	return n.metric.GetQPS(event)
    53  }
    54  
    55  func (n *BaseStatNode) GetPreviousQPS(event base.MetricEvent) float64 {
    56  	return n.metric.GetPreviousQPS(event)
    57  }
    58  
    59  func (n *BaseStatNode) GetSum(event base.MetricEvent) int64 {
    60  	return n.metric.GetSum(event)
    61  }
    62  
    63  func (n *BaseStatNode) GetMaxAvg(event base.MetricEvent) float64 {
    64  	return float64(n.metric.GetMaxOfSingleBucket(event)) * float64(n.sampleCount) / float64(n.intervalMs) * 1000.0
    65  }
    66  
    67  func (n *BaseStatNode) AddCount(event base.MetricEvent, count int64) {
    68  	n.arr.AddCount(event, count)
    69  }
    70  
    71  func (n *BaseStatNode) UpdateConcurrency(concurrency int32) {
    72  	n.arr.UpdateConcurrency(concurrency)
    73  }
    74  
    75  func (n *BaseStatNode) AvgRT() float64 {
    76  	complete := n.metric.GetSum(base.MetricEventComplete)
    77  	if complete <= 0 {
    78  		return float64(0.0)
    79  	}
    80  	return float64(n.metric.GetSum(base.MetricEventRt) / complete)
    81  }
    82  
    83  func (n *BaseStatNode) MinRT() float64 {
    84  	return float64(n.metric.MinRT())
    85  }
    86  
    87  func (n *BaseStatNode) MaxConcurrency() int32 {
    88  	return n.metric.MaxConcurrency()
    89  }
    90  
    91  func (n *BaseStatNode) CurrentConcurrency() int32 {
    92  	return atomic.LoadInt32(&(n.concurrency))
    93  }
    94  
    95  func (n *BaseStatNode) IncreaseConcurrency() {
    96  	n.UpdateConcurrency(atomic.AddInt32(&(n.concurrency), 1))
    97  }
    98  
    99  func (n *BaseStatNode) DecreaseConcurrency() {
   100  	atomic.AddInt32(&(n.concurrency), -1)
   101  }
   102  
   103  func (n *BaseStatNode) GenerateReadStat(sampleCount uint32, intervalInMs uint32) (base.ReadStat, error) {
   104  	return sbase.NewSlidingWindowMetric(sampleCount, intervalInMs, n.arr)
   105  }
   106  
   107  func (n *BaseStatNode) DefaultMetric() base.ReadStat {
   108  	return n.metric
   109  }