Skip to content

Commit 9e9bd04

Browse files
author
1911860538
committed
cmd: add some benchmarks for strings.SplitSeq replacement
1 parent f56319c commit 9e9bd04

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2025 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 base
6+
7+
import (
8+
"cmd/internal/obj"
9+
"strings"
10+
"testing"
11+
)
12+
13+
func BenchmarkParseSpectreNew(b *testing.B) {
14+
if Ctxt == nil {
15+
Ctxt = &obj.Link{}
16+
}
17+
18+
testCases := []struct {
19+
name string
20+
input string
21+
}{{
22+
name: "empty",
23+
input: "",
24+
}, {
25+
name: "index",
26+
input: "index",
27+
}, {
28+
name: "ret",
29+
input: "ret",
30+
}, {
31+
name: "index_ret",
32+
input: "index,ret",
33+
}, {
34+
name: "all",
35+
input: "all",
36+
}, {
37+
name: "multiple_indices_ret",
38+
input: strings.Repeat("index,", 10) + "ret",
39+
}}
40+
41+
for _, tc := range testCases {
42+
b.Run(tc.name, func(b *testing.B) {
43+
// Reset variables before each run
44+
oldFlagCfgSpectreIndex := Flag.Cfg.SpectreIndex
45+
oldCtxtRetpoline := Ctxt.Retpoline
46+
defer func() {
47+
Flag.Cfg.SpectreIndex = oldFlagCfgSpectreIndex
48+
Ctxt.Retpoline = oldCtxtRetpoline
49+
}()
50+
51+
b.ResetTimer()
52+
for b.Loop() {
53+
parseSpectre(tc.input)
54+
}
55+
})
56+
}
57+
}

src/cmd/go/internal/auth/gitauth_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package auth
66

77
import (
8+
"strings"
89
"testing"
910
)
1011

@@ -82,3 +83,77 @@ password:secr3t
8283
}
8384
}
8485
}
86+
87+
func BenchmarkParseGitAuth(b *testing.B) {
88+
// Define different test scenarios to benchmark
89+
testCases := []struct {
90+
name string
91+
data []byte
92+
}{{
93+
// Standard scenario with all basic fields present
94+
name: "standard",
95+
data: []byte(`
96+
protocol=https
97+
host=example.com
98+
username=bob
99+
password=secr3t
100+
`),
101+
}, {
102+
// Scenario with URL field included
103+
name: "with_url",
104+
data: []byte(`
105+
protocol=https
106+
host=example.com
107+
username=bob
108+
password=secr3t
109+
url=https://example.com/repo
110+
`),
111+
}, {
112+
// Minimal scenario with only required fields
113+
name: "minimal",
114+
data: []byte(`
115+
protocol=https
116+
host=example.com
117+
`),
118+
}, {
119+
// Complex scenario with longer values and extra fields
120+
name: "complex",
121+
data: func() []byte {
122+
var builder strings.Builder
123+
builder.WriteString("protocol=https\n")
124+
builder.WriteString("host=example.com\n")
125+
builder.WriteString("username=longusernamenamename\n")
126+
builder.WriteString("password=longpasswordwithmanycharacters123456789\n")
127+
builder.WriteString("url=https://example.com/very/long/path/to/repository\n")
128+
builder.WriteString("extra1=value1\n")
129+
builder.WriteString("extra2=value2\n")
130+
return []byte(builder.String())
131+
}(),
132+
}, {
133+
// Scenario with empty input
134+
name: "empty",
135+
data: []byte(``),
136+
}, {
137+
// Scenario with malformed input (using colon instead of equals)
138+
name: "malformed",
139+
data: []byte(`
140+
protocol:https
141+
host:example.com
142+
username:bob
143+
password:secr3t
144+
`),
145+
}}
146+
147+
for _, tc := range testCases {
148+
b.Run(tc.name, func(b *testing.B) {
149+
b.ResetTimer()
150+
for b.Loop() {
151+
prefix, username, password := parseGitAuth(tc.data)
152+
153+
_ = prefix
154+
_ = username
155+
_ = password
156+
}
157+
})
158+
}
159+
}

0 commit comments

Comments
 (0)