Source file src/internal/sync/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 sync_test
     6  
     7  import (
     8  	isync "internal/sync"
     9  	"testing"
    10  )
    11  
    12  func BenchmarkHashTrieMapLoadSmall(b *testing.B) {
    13  	benchmarkHashTrieMapLoad(b, testDataSmall[:])
    14  }
    15  
    16  func BenchmarkHashTrieMapLoad(b *testing.B) {
    17  	benchmarkHashTrieMapLoad(b, testData[:])
    18  }
    19  
    20  func BenchmarkHashTrieMapLoadLarge(b *testing.B) {
    21  	benchmarkHashTrieMapLoad(b, testDataLarge[:])
    22  }
    23  
    24  func benchmarkHashTrieMapLoad(b *testing.B, data []string) {
    25  	b.ReportAllocs()
    26  	var m isync.HashTrieMap[string, int]
    27  	for i := range data {
    28  		m.LoadOrStore(data[i], i)
    29  	}
    30  	b.ResetTimer()
    31  	b.RunParallel(func(pb *testing.PB) {
    32  		i := 0
    33  		for pb.Next() {
    34  			_, _ = m.Load(data[i])
    35  			i++
    36  			if i >= len(data) {
    37  				i = 0
    38  			}
    39  		}
    40  	})
    41  }
    42  
    43  func BenchmarkHashTrieMapLoadOrStore(b *testing.B) {
    44  	benchmarkHashTrieMapLoadOrStore(b, testData[:])
    45  }
    46  
    47  func BenchmarkHashTrieMapLoadOrStoreLarge(b *testing.B) {
    48  	benchmarkHashTrieMapLoadOrStore(b, testDataLarge[:])
    49  }
    50  
    51  func benchmarkHashTrieMapLoadOrStore(b *testing.B, data []string) {
    52  	b.ReportAllocs()
    53  	var m isync.HashTrieMap[string, int]
    54  
    55  	b.RunParallel(func(pb *testing.PB) {
    56  		i := 0
    57  		for pb.Next() {
    58  			_, _ = m.LoadOrStore(data[i], i)
    59  			i++
    60  			if i >= len(data) {
    61  				i = 0
    62  			}
    63  		}
    64  	})
    65  }
    66  

View as plain text