Source file src/unique/handle_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 unique
     6  
     7  import (
     8  	"fmt"
     9  	"runtime"
    10  	"testing"
    11  )
    12  
    13  func BenchmarkMake(b *testing.B) {
    14  	benchmarkMake(b, []string{"foo"})
    15  }
    16  
    17  func BenchmarkMakeMany(b *testing.B) {
    18  	benchmarkMake(b, testData[:])
    19  }
    20  
    21  func BenchmarkMakeManyMany(b *testing.B) {
    22  	benchmarkMake(b, testDataLarge[:])
    23  }
    24  
    25  func benchmarkMake(b *testing.B, testData []string) {
    26  	handles := make([]Handle[string], 0, len(testData))
    27  	for i := range testData {
    28  		handles = append(handles, Make(testData[i]))
    29  	}
    30  
    31  	b.ReportAllocs()
    32  	b.ResetTimer()
    33  
    34  	b.RunParallel(func(pb *testing.PB) {
    35  		i := 0
    36  		for pb.Next() {
    37  			_ = Make(testData[i])
    38  			i++
    39  			if i >= len(testData) {
    40  				i = 0
    41  			}
    42  		}
    43  	})
    44  
    45  	b.StopTimer()
    46  
    47  	runtime.GC()
    48  	runtime.GC()
    49  }
    50  
    51  var (
    52  	testData      [128]string
    53  	testDataLarge [128 << 10]string
    54  )
    55  
    56  func init() {
    57  	for i := range testData {
    58  		testData[i] = fmt.Sprintf("%b", i)
    59  	}
    60  	for i := range testDataLarge {
    61  		testDataLarge[i] = fmt.Sprintf("%b", i)
    62  	}
    63  }
    64  

View as plain text