| 1 |
package config //nolint:testpackage |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
func BenchmarkLookupConfigArgument(b *testing.B) { |
| 9 |
for _, tc := range []struct { |
| 10 |
name string |
| 11 |
args []string |
| 12 |
}{ |
| 13 |
{ |
| 14 |
name: "equals", |
| 15 |
args: []string{"openvpn-auth-oauth2", "--config=/tmp/config.yaml"}, |
| 16 |
}, |
| 17 |
{ |
| 18 |
name: "separate-argument", |
| 19 |
args: []string{"openvpn-auth-oauth2", "--config", "/tmp/config.yaml"}, |
| 20 |
}, |
| 21 |
{ |
| 22 |
name: "missing", |
| 23 |
args: []string{"openvpn-auth-oauth2", "--http.listen=:9001"}, |
| 24 |
}, |
| 25 |
} { |
| 26 |
b.Run(tc.name, func(b *testing.B) { |
| 27 |
var configFile string |
| 28 |
|
| 29 |
b.ReportAllocs() |
| 30 |
b.ResetTimer() |
| 31 |
|
| 32 |
for b.Loop() { |
| 33 |
configFile = lookupConfigArgument(tc.args) |
| 34 |
} |
| 35 |
|
| 36 |
_ = configFile |
| 37 |
}) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
func BenchmarkReadFromFlagAndEnvironment(b *testing.B) { |
| 42 |
flagArgs := []string{ |
| 43 |
"openvpn-auth-oauth2", |
| 44 |
"--http.listen=:9001", |
| 45 |
"--http.secret=1234567890123456", |
| 46 |
"--oauth2.issuer=http://localhost", |
| 47 |
"--oauth2.endpoint.discovery=http://localhost/.well-known/openid-configuration", |
| 48 |
"--oauth2.endpoint.auth=http://localhost/authorize", |
| 49 |
"--oauth2.endpoint.token=http://localhost/token", |
| 50 |
"--oauth2.client.id=bench-client", |
| 51 |
"--oauth2.client.secret=bench-secret", |
| 52 |
"--oauth2.authorize-params=prompt=login&audience=vpn", |
| 53 |
} |
| 54 |
|
| 55 |
b.Run("flags", func(b *testing.B) { |
| 56 |
var conf Config |
| 57 |
|
| 58 |
b.ReportAllocs() |
| 59 |
b.ResetTimer() |
| 60 |
|
| 61 |
for b.Loop() { |
| 62 |
conf = Defaults |
| 63 |
if err := conf.ReadFromFlagAndEnvironment(flagArgs, io.Discard); err != nil { |
| 64 |
b.Fatal(err) |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
_ = conf |
| 69 |
}) |
| 70 |
|
| 71 |
b.Run("environment", func(b *testing.B) { |
| 72 |
b.Setenv(getEnvironmentVariableByFlagName("http.listen"), ":9002") |
| 73 |
b.Setenv(getEnvironmentVariableByFlagName("http.secret"), "1234567890123456") |
| 74 |
b.Setenv(getEnvironmentVariableByFlagName("oauth2.issuer"), "http://localhost") |
| 75 |
b.Setenv(getEnvironmentVariableByFlagName("oauth2.client.id"), "bench-client") |
| 76 |
b.Setenv(getEnvironmentVariableByFlagName("oauth2.client.secret"), "bench-secret") |
| 77 |
b.Setenv(getEnvironmentVariableByFlagName("oauth2.authorize-params"), "prompt=login&audience=vpn") |
| 78 |
|
| 79 |
var conf Config |
| 80 |
|
| 81 |
b.ReportAllocs() |
| 82 |
b.ResetTimer() |
| 83 |
|
| 84 |
for b.Loop() { |
| 85 |
conf = Defaults |
| 86 |
if err := conf.ReadFromFlagAndEnvironment([]string{"openvpn-auth-oauth2"}, io.Discard); err != nil { |
| 87 |
b.Fatal(err) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
_ = conf |
| 92 |
}) |
| 93 |
} |