aboutsummaryrefslogtreecommitdiff
path: root/src/unique/handle_bench_test.go
blob: 51f94c3f9180d07804bbd3b6164c2325cd93647b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package unique

import (
	"fmt"
	"runtime"
	"testing"
)

func BenchmarkMake(b *testing.B) {
	benchmarkMake(b, []string{"foo"})
}

func BenchmarkMakeMany(b *testing.B) {
	benchmarkMake(b, testData[:])
}

func BenchmarkMakeManyMany(b *testing.B) {
	benchmarkMake(b, testDataLarge[:])
}

func benchmarkMake(b *testing.B, testData []string) {
	handles := make([]Handle[string], 0, len(testData))
	for i := range testData {
		handles = append(handles, Make(testData[i]))
	}

	b.ReportAllocs()
	b.ResetTimer()

	b.RunParallel(func(pb *testing.PB) {
		i := 0
		for pb.Next() {
			_ = Make(testData[i])
			i++
			if i >= len(testData) {
				i = 0
			}
		}
	})

	b.StopTimer()

	runtime.GC()
	runtime.GC()
}

var (
	testData      [128]string
	testDataLarge [128 << 10]string
)

func init() {
	for i := range testData {
		testData[i] = fmt.Sprintf("%b", i)
	}
	for i := range testDataLarge {
		testDataLarge[i] = fmt.Sprintf("%b", i)
	}
}