github.com/chain5j/chain5j-pkg@v1.0.7/collection/maps/treemap/v2/benchmark_test.go (about)

     1  package treemap
     2  
     3  import (
     4  	"math/rand"
     5  	"testing"
     6  )
     7  
     8  func BenchmarkSeqSet(b *testing.B) {
     9  	tr := New[int, string]()
    10  	for i := 0; i < b.N; i++ {
    11  		for j := 0; j < NumIterations; j++ {
    12  			tr.Set(j, "")
    13  		}
    14  		tr.Clear()
    15  	}
    16  	b.ReportAllocs()
    17  }
    18  
    19  func BenchmarkSeqGet(b *testing.B) {
    20  	tr := New[int, string]()
    21  	for i := 0; i < NumIterations; i++ {
    22  		tr.Set(i, "")
    23  	}
    24  	b.ResetTimer()
    25  	for i := 0; i < b.N; i++ {
    26  		tr.Get(i % NumIterations)
    27  	}
    28  	b.ReportAllocs()
    29  }
    30  
    31  func BenchmarkSeqIter(b *testing.B) {
    32  	tr := New[int, string]()
    33  	for i := 0; i < NumIterations; i++ {
    34  		tr.Set(i, "")
    35  	}
    36  	b.ResetTimer()
    37  	for i := 0; i < b.N; i++ {
    38  		for it := tr.Iterator(); it.Valid(); it.Next() {
    39  		}
    40  	}
    41  	b.ReportAllocs()
    42  }
    43  
    44  func BenchmarkRndSet(b *testing.B) {
    45  	keys, _ := benchmarksRandomData()
    46  	tr := New[int, string]()
    47  	b.ResetTimer()
    48  	for i := 0; i < b.N; i++ {
    49  		for _, k := range keys {
    50  			tr.Set(k, "")
    51  		}
    52  		tr.Clear()
    53  	}
    54  	b.ReportAllocs()
    55  }
    56  
    57  func BenchmarkRndGet(b *testing.B) {
    58  	tr := New[int, string]()
    59  	keys, max := benchmarksRandomData()
    60  	for _, k := range keys {
    61  		tr.Set(k, "")
    62  	}
    63  	b.ResetTimer()
    64  	for i := 0; i < b.N; i++ {
    65  		tr.Get(i % max)
    66  	}
    67  	b.ReportAllocs()
    68  }
    69  
    70  func BenchmarkRndIter(b *testing.B) {
    71  	tr := New[int, string]()
    72  	keys, _ := benchmarksRandomData()
    73  	for _, k := range keys {
    74  		tr.Set(k, "")
    75  	}
    76  	b.ResetTimer()
    77  	for i := 0; i < b.N; i++ {
    78  		for it := tr.Iterator(); it.Valid(); it.Next() {
    79  		}
    80  	}
    81  	b.ReportAllocs()
    82  }
    83  
    84  func benchmarksRandomData() ([]int, int) {
    85  	keys := make([]int, NumIterations)
    86  	max := NumIterations * 100
    87  	for i := range keys {
    88  		keys[i] = int(rand.Int63n(int64(max)))
    89  	}
    90  	return keys, max
    91  }