github.com/songzhibin97/gkit@v1.2.13/structure/zset/zset_bench_test.go (about)

     1  // Copyright 2021 ByteDance Inc.
     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 zset
    16  
    17  import (
    18  	"github.com/songzhibin97/gkit/sys/fastrand"
    19  	"math"
    20  	"strconv"
    21  	"testing"
    22  )
    23  
    24  const initSize = 1 << 10
    25  const randN = math.MaxUint32
    26  
    27  func BenchmarkContains100Hits(b *testing.B) {
    28  	benchmarkContainsNHits(b, 100)
    29  }
    30  
    31  func BenchmarkContains50Hits(b *testing.B) {
    32  	benchmarkContainsNHits(b, 50)
    33  }
    34  
    35  func BenchmarkContainsNoHits(b *testing.B) {
    36  	benchmarkContainsNHits(b, 0)
    37  }
    38  
    39  func benchmarkContainsNHits(b *testing.B, n int) {
    40  	b.Run("sortedset", func(b *testing.B) {
    41  		z := NewFloat64()
    42  		var vals []string
    43  		for i := 0; i < initSize; i++ {
    44  			val := strconv.Itoa(i)
    45  			vals = append(vals, val)
    46  			if fastrand.Intn(100)+1 <= n {
    47  				z.Add(fastrand.Float64(), val)
    48  			}
    49  		}
    50  		b.ResetTimer()
    51  		b.RunParallel(func(pb *testing.PB) {
    52  			for pb.Next() {
    53  				_ = z.Contains(vals[fastrand.Intn(initSize)])
    54  			}
    55  		})
    56  	})
    57  }
    58  
    59  func BenchmarkAdd(b *testing.B) {
    60  	benchmarkNAddNIncrNRemoveNContains(b, 100, 0, 0, 0)
    61  }
    62  
    63  func Benchmark1Add99Contains(b *testing.B) {
    64  	benchmarkNAddNIncrNRemoveNContains(b, 1, 0, 0, 99)
    65  }
    66  
    67  func Benchmark10Add90Contains(b *testing.B) {
    68  	benchmarkNAddNIncrNRemoveNContains(b, 10, 0, 0, 90)
    69  }
    70  
    71  func Benchmark50Add50Contains(b *testing.B) {
    72  	benchmarkNAddNIncrNRemoveNContains(b, 50, 0, 0, 50)
    73  }
    74  
    75  func Benchmark1Add3Incr6Remove90Contains(b *testing.B) {
    76  	benchmarkNAddNIncrNRemoveNContains(b, 1, 3, 6, 90)
    77  }
    78  
    79  func benchmarkNAddNIncrNRemoveNContains(b *testing.B, nAdd, nIncr, nRemove, nContains int) {
    80  	// var anAdd, anIncr, anRemove, anContains int
    81  
    82  	b.Run("sortedset", func(b *testing.B) {
    83  		z := NewFloat64()
    84  		var vals []string
    85  		var scores []float64
    86  		var ops []int
    87  		for i := 0; i < initSize; i++ {
    88  			vals = append(vals, strconv.Itoa(fastrand.Intn(randN)))
    89  			scores = append(scores, fastrand.Float64())
    90  			ops = append(ops, fastrand.Intn(100))
    91  		}
    92  		b.ResetTimer()
    93  		b.RunParallel(func(pb *testing.PB) {
    94  			for pb.Next() {
    95  				r := fastrand.Intn(initSize)
    96  				val := vals[r]
    97  				if u := ops[r] + 1; u <= nAdd {
    98  					// anAdd++
    99  					z.Add(scores[r], val)
   100  				} else if u-nAdd <= nIncr {
   101  					// anIncr++
   102  					z.IncrBy(scores[r], val)
   103  				} else if u-nAdd-nIncr <= nRemove {
   104  					// anRemove++
   105  					z.Remove(val)
   106  				} else if u-nAdd-nIncr-nRemove <= nContains {
   107  					// anContains++
   108  					z.Contains(val)
   109  				}
   110  			}
   111  		})
   112  		// b.Logf("N: %d, Add: %f, Incr: %f, Remove: %f, Contains: %f", b.N, float64(anAdd)/float64(b.N), float64(anIncr)/float64(b.N), float64(anRemove)/float64(b.N), float64(anContains)/float64(b.N))
   113  	})
   114  }