Skip to content

Commit 865249a

Browse files
committed
chore(endpoint): Refactor Type for simplification and better performance
test result cpu: AMD Ryzen 7 4800H with Radeon Graphics BenchmarkEndpoint_Type-16 Cut + switch 5053628 238.7 ns/op 0 B/op 0 allocs/op Cut + map 2714998 438.6 ns/op 0 B/op 0 allocs/op switch HasPerfix 2683519 430.9 ns/op 0 B/op 0 allocs/op
1 parent fdd296b commit 865249a

File tree

2 files changed

+136
-121
lines changed

2 files changed

+136
-121
lines changed

config/endpoint/endpoint.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -161,28 +161,33 @@ func (e *Endpoint) IsEnabled() bool {
161161

162162
// Type returns the endpoint type
163163
func (e *Endpoint) Type() Type {
164-
switch {
165-
case e.DNSConfig != nil:
164+
if e.DNSConfig != nil {
166165
return TypeDNS
167-
case strings.HasPrefix(e.URL, "tcp://"):
166+
}
167+
before, _, ok := strings.Cut(e.URL, ":")
168+
if !ok {
169+
return TypeUNKNOWN
170+
}
171+
switch before {
172+
case "tcp":
168173
return TypeTCP
169-
case strings.HasPrefix(e.URL, "sctp://"):
174+
case "sctp":
170175
return TypeSCTP
171-
case strings.HasPrefix(e.URL, "udp://"):
176+
case "udp":
172177
return TypeUDP
173-
case strings.HasPrefix(e.URL, "icmp://"):
178+
case "icmp":
174179
return TypeICMP
175-
case strings.HasPrefix(e.URL, "starttls://"):
180+
case "starttls":
176181
return TypeSTARTTLS
177-
case strings.HasPrefix(e.URL, "tls://"):
182+
case "tls":
178183
return TypeTLS
179-
case strings.HasPrefix(e.URL, "http://") || strings.HasPrefix(e.URL, "https://"):
184+
case "http", "https":
180185
return TypeHTTP
181-
case strings.HasPrefix(e.URL, "ws://") || strings.HasPrefix(e.URL, "wss://"):
186+
case "ws", "wss":
182187
return TypeWS
183-
case strings.HasPrefix(e.URL, "ssh://"):
188+
case "ssh":
184189
return TypeSSH
185-
case strings.HasPrefix(e.URL, "domain://"):
190+
case "domain":
186191
return TypeDomain
187192
default:
188193
return TypeUNKNOWN

config/endpoint/endpoint_test.go

Lines changed: 119 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -301,116 +301,112 @@ func TestEndpoint_IsEnabled(t *testing.T) {
301301
}
302302
}
303303

304+
type testEndpoint_typeArgs struct {
305+
URL string
306+
DNS *dns.Config
307+
SSH *ssh.Config
308+
}
309+
310+
var testEndpoint_typeData = []struct {
311+
args testEndpoint_typeArgs
312+
want Type
313+
}{
314+
{
315+
args: testEndpoint_typeArgs{
316+
URL: "8.8.8.8",
317+
DNS: &dns.Config{
318+
QueryType: "A",
319+
QueryName: "example.com",
320+
},
321+
},
322+
want: TypeDNS,
323+
},
324+
{
325+
args: testEndpoint_typeArgs{
326+
URL: "tcp://127.0.0.1:6379",
327+
},
328+
want: TypeTCP,
329+
},
330+
{
331+
args: testEndpoint_typeArgs{
332+
URL: "icmp://example.com",
333+
},
334+
want: TypeICMP,
335+
},
336+
{
337+
args: testEndpoint_typeArgs{
338+
URL: "sctp://example.com",
339+
},
340+
want: TypeSCTP,
341+
},
342+
{
343+
args: testEndpoint_typeArgs{
344+
URL: "udp://example.com",
345+
},
346+
want: TypeUDP,
347+
},
348+
{
349+
args: testEndpoint_typeArgs{
350+
URL: "starttls://smtp.gmail.com:587",
351+
},
352+
want: TypeSTARTTLS,
353+
},
354+
{
355+
args: testEndpoint_typeArgs{
356+
URL: "tls://example.com:443",
357+
},
358+
want: TypeTLS,
359+
},
360+
{
361+
args: testEndpoint_typeArgs{
362+
URL: "https://twin.sh/health",
363+
},
364+
want: TypeHTTP,
365+
},
366+
{
367+
args: testEndpoint_typeArgs{
368+
URL: "wss://example.com/",
369+
},
370+
want: TypeWS,
371+
},
372+
{
373+
args: testEndpoint_typeArgs{
374+
URL: "ws://example.com/",
375+
},
376+
want: TypeWS,
377+
},
378+
{
379+
args: testEndpoint_typeArgs{
380+
URL: "ssh://example.com:22",
381+
SSH: &ssh.Config{
382+
Username: "root",
383+
Password: "password",
384+
},
385+
},
386+
want: TypeSSH,
387+
},
388+
{
389+
args: testEndpoint_typeArgs{
390+
URL: "domain://example.org",
391+
},
392+
want: TypeDomain,
393+
},
394+
{
395+
args: testEndpoint_typeArgs{
396+
URL: "invalid://example.org",
397+
},
398+
want: TypeUNKNOWN,
399+
},
400+
{
401+
args: testEndpoint_typeArgs{
402+
URL: "no-scheme",
403+
},
404+
want: TypeUNKNOWN,
405+
},
406+
}
407+
304408
func TestEndpoint_Type(t *testing.T) {
305-
type args struct {
306-
URL string
307-
DNS *dns.Config
308-
SSH *ssh.Config
309-
}
310-
tests := []struct {
311-
args args
312-
want Type
313-
}{
314-
{
315-
args: args{
316-
URL: "8.8.8.8",
317-
DNS: &dns.Config{
318-
QueryType: "A",
319-
QueryName: "example.com",
320-
},
321-
},
322-
want: TypeDNS,
323-
},
324-
{
325-
args: args{
326-
URL: "tcp://127.0.0.1:6379",
327-
},
328-
want: TypeTCP,
329-
},
330-
{
331-
args: args{
332-
URL: "icmp://example.com",
333-
},
334-
want: TypeICMP,
335-
},
336-
{
337-
args: args{
338-
URL: "sctp://example.com",
339-
},
340-
want: TypeSCTP,
341-
},
342-
{
343-
args: args{
344-
URL: "udp://example.com",
345-
},
346-
want: TypeUDP,
347-
},
348-
{
349-
args: args{
350-
URL: "starttls://smtp.gmail.com:587",
351-
},
352-
want: TypeSTARTTLS,
353-
},
354-
{
355-
args: args{
356-
URL: "tls://example.com:443",
357-
},
358-
want: TypeTLS,
359-
},
360-
{
361-
args: args{
362-
URL: "https://twin.sh/health",
363-
},
364-
want: TypeHTTP,
365-
},
366-
{
367-
args: args{
368-
URL: "wss://example.com/",
369-
},
370-
want: TypeWS,
371-
},
372-
{
373-
args: args{
374-
URL: "ws://example.com/",
375-
},
376-
want: TypeWS,
377-
},
378-
{
379-
args: args{
380-
URL: "ssh://example.com:22",
381-
SSH: &ssh.Config{
382-
Username: "root",
383-
Password: "password",
384-
},
385-
},
386-
want: TypeSSH,
387-
},
388-
{
389-
args: args{
390-
URL: "domain://example.org",
391-
},
392-
want: TypeDomain,
393-
},
394-
{
395-
args: args{
396-
URL: "domain://example.org",
397-
},
398-
want: TypeDomain,
399-
},
400-
{
401-
args: args{
402-
URL: "invalid://example.org",
403-
},
404-
want: TypeUNKNOWN,
405-
},
406-
{
407-
args: args{
408-
URL: "no-scheme",
409-
},
410-
want: TypeUNKNOWN,
411-
},
412-
}
413-
for _, tt := range tests {
409+
for _, tt := range testEndpoint_typeData {
414410
t.Run(string(tt.want), func(t *testing.T) {
415411
endpoint := Endpoint{
416412
URL: tt.args.URL,
@@ -423,6 +419,20 @@ func TestEndpoint_Type(t *testing.T) {
423419
}
424420
}
425421

422+
func BenchmarkEndpoint_Type(b *testing.B) {
423+
for b.Loop() {
424+
for _, tt := range testEndpoint_typeData {
425+
endpoint := Endpoint{
426+
URL: tt.args.URL,
427+
DNSConfig: tt.args.DNS,
428+
}
429+
if got := endpoint.Type(); got != tt.want {
430+
b.Errorf("Endpoint.Type() = %v, want %v", got, tt.want)
431+
}
432+
}
433+
}
434+
}
435+
426436
func TestEndpoint_ValidateAndSetDefaults(t *testing.T) {
427437
endpoint := Endpoint{
428438
Name: "website-health",

0 commit comments

Comments
 (0)