github.com/alibaba/sentinel-golang@v1.0.4/core/stat/base/stat_base_benchmark_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  	"math/rand"
    19  	"testing"
    20  
    21  	"github.com/alibaba/sentinel-golang/core/base"
    22  	"github.com/alibaba/sentinel-golang/util"
    23  )
    24  
    25  func BenchmarkBucketLeapArray_AddCount_Concurrency1(b *testing.B) {
    26  	a := NewBucketLeapArray(base.DefaultSampleCountTotal, base.DefaultIntervalMsTotal)
    27  	b.ReportAllocs()
    28  	b.SetParallelism(1)
    29  	b.ResetTimer()
    30  	b.RunParallel(func(pb *testing.PB) {
    31  		for pb.Next() {
    32  			a.AddCount(base.MetricEventPass, 1)
    33  		}
    34  	})
    35  }
    36  
    37  func BenchmarkBucketLeapArray_AddCount_Concurrency10(b *testing.B) {
    38  	a := NewBucketLeapArray(base.DefaultSampleCountTotal, base.DefaultIntervalMsTotal)
    39  	b.ReportAllocs()
    40  	b.SetParallelism(10)
    41  	b.ResetTimer()
    42  	b.RunParallel(func(pb *testing.PB) {
    43  		for pb.Next() {
    44  			a.AddCount(base.MetricEventPass, 1)
    45  		}
    46  	})
    47  }
    48  
    49  func BenchmarkBucketLeapArray_AddCount_Concurrency100(b *testing.B) {
    50  	a := NewBucketLeapArray(base.DefaultSampleCountTotal, base.DefaultIntervalMsTotal)
    51  	b.ReportAllocs()
    52  	b.SetParallelism(100)
    53  	b.ResetTimer()
    54  	b.RunParallel(func(pb *testing.PB) {
    55  		for pb.Next() {
    56  			a.AddCount(base.MetricEventPass, 1)
    57  		}
    58  	})
    59  }
    60  
    61  func BenchmarkBucketLeapArray_AddCount_Concurrency1000(b *testing.B) {
    62  	a := NewBucketLeapArray(base.DefaultSampleCountTotal, base.DefaultIntervalMsTotal)
    63  	b.ReportAllocs()
    64  	b.SetParallelism(1000)
    65  	b.ResetTimer()
    66  	b.RunParallel(func(pb *testing.PB) {
    67  		for pb.Next() {
    68  			a.AddCount(base.MetricEventPass, 1)
    69  		}
    70  	})
    71  }
    72  
    73  func BenchmarkBucketLeapArray_Count_Concurrency1(b *testing.B) {
    74  	a := NewBucketLeapArray(base.DefaultSampleCountTotal, base.DefaultIntervalMsTotal)
    75  	b.ReportAllocs()
    76  	b.SetParallelism(1)
    77  	b.ResetTimer()
    78  	rand.Seed(util.Now().UnixNano())
    79  	b.RunParallel(func(pb *testing.PB) {
    80  		for pb.Next() {
    81  			if rand.Int()%100 >= 80 {
    82  				a.AddCount(base.MetricEventPass, 1)
    83  			} else {
    84  				_ = a.Count(base.MetricEventPass)
    85  			}
    86  		}
    87  	})
    88  }
    89  
    90  func BenchmarkBucketLeapArray_Count_Concurrency10(b *testing.B) {
    91  	a := NewBucketLeapArray(base.DefaultSampleCountTotal, base.DefaultIntervalMsTotal)
    92  	b.ReportAllocs()
    93  	b.SetParallelism(10)
    94  	b.ResetTimer()
    95  	rand.Seed(util.Now().UnixNano())
    96  	b.RunParallel(func(pb *testing.PB) {
    97  		for pb.Next() {
    98  			if rand.Int()%100 >= 80 {
    99  				a.AddCount(base.MetricEventPass, 1)
   100  			} else {
   101  				_ = a.Count(base.MetricEventPass)
   102  			}
   103  		}
   104  	})
   105  }
   106  func BenchmarkBucketLeapArray_Count_Concurrency100(b *testing.B) {
   107  	a := NewBucketLeapArray(base.DefaultSampleCountTotal, base.DefaultIntervalMsTotal)
   108  	b.ReportAllocs()
   109  	b.SetParallelism(100)
   110  	b.ResetTimer()
   111  	rand.Seed(util.Now().UnixNano())
   112  	b.RunParallel(func(pb *testing.PB) {
   113  		for pb.Next() {
   114  			if rand.Int()%100 >= 80 {
   115  				a.AddCount(base.MetricEventPass, 1)
   116  			} else {
   117  				_ = a.Count(base.MetricEventPass)
   118  			}
   119  		}
   120  	})
   121  }
   122  
   123  func BenchmarkBucketLeapArray_Count_Concurrency1000(b *testing.B) {
   124  	a := NewBucketLeapArray(base.DefaultSampleCountTotal, base.DefaultIntervalMsTotal)
   125  	b.ReportAllocs()
   126  	b.SetParallelism(1000)
   127  	b.ResetTimer()
   128  	rand.Seed(util.Now().UnixNano())
   129  	b.RunParallel(func(pb *testing.PB) {
   130  		for pb.Next() {
   131  			if rand.Int()%100 >= 80 {
   132  				a.AddCount(base.MetricEventPass, 1)
   133  			} else {
   134  				_ = a.Count(base.MetricEventPass)
   135  			}
   136  		}
   137  	})
   138  }