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