File: benchmark_test.go

package info (click to toggle)
golang-opentelemetry-otel 1.31.0-5
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 11,844 kB
  • sloc: makefile: 237; sh: 51
file content (78 lines) | stat: -rw-r--r-- 1,567 bytes parent folder | download | duplicates (2)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package resource_test

import (
	"fmt"
	"math/rand"
	"testing"

	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/sdk/resource"
)

const conflict = 0.5

func makeAttrs(n int) (_, _ *resource.Resource) {
	used := map[string]bool{}
	l1 := make([]attribute.KeyValue, n)
	l2 := make([]attribute.KeyValue, n)
	for i := 0; i < n; i++ {
		var k string
		for {
			k = fmt.Sprint("k", rand.Intn(1000000000))
			if !used[k] {
				used[k] = true
				break
			}
		}
		l1[i] = attribute.String(k, fmt.Sprint("v", rand.Intn(1000000000)))

		if rand.Float64() < conflict {
			l2[i] = l1[i]
		} else {
			l2[i] = attribute.String(k, fmt.Sprint("v", rand.Intn(1000000000)))
		}
	}
	return resource.NewSchemaless(l1...), resource.NewSchemaless(l2...)
}

func benchmarkMergeResource(b *testing.B, size int) {
	r1, r2 := makeAttrs(size)

	b.ReportAllocs()
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		_, _ = resource.Merge(r1, r2)
	}
}

func BenchmarkMergeResource_1(b *testing.B) {
	benchmarkMergeResource(b, 1)
}

func BenchmarkMergeResource_2(b *testing.B) {
	benchmarkMergeResource(b, 2)
}

func BenchmarkMergeResource_3(b *testing.B) {
	benchmarkMergeResource(b, 3)
}

func BenchmarkMergeResource_4(b *testing.B) {
	benchmarkMergeResource(b, 4)
}

func BenchmarkMergeResource_6(b *testing.B) {
	benchmarkMergeResource(b, 6)
}

func BenchmarkMergeResource_8(b *testing.B) {
	benchmarkMergeResource(b, 8)
}

func BenchmarkMergeResource_16(b *testing.B) {
	benchmarkMergeResource(b, 16)
}