Source file src/internal/concurrent/hashtriemap_bench_test.go

     1  // Copyright 2024 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package concurrent
     6  
     7  import "testing"
     8  
     9  func BenchmarkHashTrieMapLoadSmall(b *testing.B) {
    10  	benchmarkHashTrieMapLoad(b, testDataSmall[:])
    11  }
    12  
    13  func BenchmarkHashTrieMapLoad(b *testing.B) {
    14  	benchmarkHashTrieMapLoad(b, testData[:])
    15  }
    16  
    17  func BenchmarkHashTrieMapLoadLarge(b *testing.B) {
    18  	benchmarkHashTrieMapLoad(b, testDataLarge[:])
    19  }
    20  
    21  func benchmarkHashTrieMapLoad(b *testing.B, data []string) {
    22  	b.ReportAllocs()
    23  	m := NewHashTrieMap[string, int]()
    24  	for i := range data {
    25  		m.LoadOrStore(data[i], i)
    26  	}
    27  	b.ResetTimer()
    28  	b.RunParallel(func(pb *testing.PB) {
    29  		i := 0
    30  		for pb.Next() {
    31  			_, _ = m.Load(data[i])
    32  			i++
    33  			if i >= len(data) {
    34  				i = 0
    35  			}
    36  		}
    37  	})
    38  }
    39  
    40  func BenchmarkHashTrieMapLoadOrStore(b *testing.B) {
    41  	benchmarkHashTrieMapLoadOrStore(b, testData[:])
    42  }
    43  
    44  func BenchmarkHashTrieMapLoadOrStoreLarge(b *testing.B) {
    45  	benchmarkHashTrieMapLoadOrStore(b, testDataLarge[:])
    46  }
    47  
    48  func benchmarkHashTrieMapLoadOrStore(b *testing.B, data []string) {
    49  	b.ReportAllocs()
    50  	m := NewHashTrieMap[string, int]()
    51  
    52  	b.RunParallel(func(pb *testing.PB) {
    53  		i := 0
    54  		for pb.Next() {
    55  			_, _ = m.LoadOrStore(data[i], i)
    56  			i++
    57  			if i >= len(data) {
    58  				i = 0
    59  			}
    60  		}
    61  	})
    62  }
    63  

View as plain text