Debian Codegraph @

internal/openvpn/main_bench_test.go

raw
1 package openvpn //nolint:testpackage
2
3 import (
4 "io"
5 "log/slog"
6 "testing"
7
8 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config"
9 )
10
11 func BenchmarkManagementCommandName(b *testing.B) {
12 const command = "client-pending-auth 123 456 \"WEB_AUTH::https://example.com/oauth2/start\" 300\r\n"
13
14 var name string
15
16 b.ReportAllocs()
17
18 for b.Loop() {
19 name = managementCommandName(command)
20 }
21
22 if name != "client-pending-auth" {
23 b.Fatalf("unexpected command name %q", name)
24 }
25 }
26
27 func BenchmarkSendCommand(b *testing.B) {
28 conf := config.Defaults
29 client := New(slog.New(slog.NewTextHandler(io.Discard, nil)), &conf) //nolint:sloglint
30 doneCh := make(chan struct{})
31
32 go func() {
33 defer close(doneCh)
34
35 for {
36 select {
37 case <-client.shutdownCh:
38 return
39 case <-client.commandsCh:
40 select {
41 case client.commandResponseCh <- "SUCCESS: command succeeded":
42 case <-client.shutdownCh:
43 return
44 }
45 }
46 }
47 }()
48
49 b.Cleanup(func() {
50 client.Shutdown(b.Context())
51 <-doneCh
52 })
53
54 b.ReportAllocs()
55
56 for b.Loop() {
57 if _, err := client.SendCommand(b.Context(), "status 3", false); err != nil {
58 b.Fatal(err)
59 }
60 }
61 }