| 1 |
package nebula |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"net/netip" |
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
"runtime" |
| 10 |
"testing" |
| 11 |
"time" |
| 12 |
|
| 13 |
"github.com/slackhq/nebula/cert" |
| 14 |
cert_test "github.com/slackhq/nebula/cert_test" |
| 15 |
"github.com/slackhq/nebula/config" |
| 16 |
"github.com/slackhq/nebula/test" |
| 17 |
"github.com/stretchr/testify/require" |
| 18 |
) |
| 19 |
|
| 20 |
func BenchmarkReloadConfigWithCAs(b *testing.B) { |
| 21 |
prevProcs := runtime.GOMAXPROCS(1) |
| 22 |
b.Cleanup(func() { runtime.GOMAXPROCS(prevProcs) }) |
| 23 |
|
| 24 |
for _, size := range []int{100, 250, 500, 1000, 5000} { |
| 25 |
b.Run(fmt.Sprintf("%dCAs", size), func(b *testing.B) { |
| 26 |
l := test.NewLogger() |
| 27 |
dir := b.TempDir() |
| 28 |
|
| 29 |
ca, caKey, caBundle := buildCABundle(b, size) |
| 30 |
caPath, certPath, keyPath := writePKIFiles(b, dir, ca, caKey, caBundle) |
| 31 |
|
| 32 |
configBody := fmt.Sprintf(`pki: |
| 33 |
ca: %s |
| 34 |
cert: %s |
| 35 |
key: %s |
| 36 |
`, caPath, certPath, keyPath) |
| 37 |
|
| 38 |
configPath := filepath.Join(dir, "config.yml") |
| 39 |
require.NoError(b, os.WriteFile(configPath, []byte(configBody), 0o600)) |
| 40 |
|
| 41 |
c := config.NewC(l) |
| 42 |
require.NoError(b, c.Load(dir)) |
| 43 |
|
| 44 |
_, err := NewPKIFromConfig(test.NewLogger(), c) |
| 45 |
require.NoError(b, err) |
| 46 |
|
| 47 |
b.ReportAllocs() |
| 48 |
b.ResetTimer() |
| 49 |
|
| 50 |
for b.Loop() { |
| 51 |
c.ReloadConfig() |
| 52 |
} |
| 53 |
}) |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
func buildCABundle(b *testing.B, count int) (cert.Certificate, []byte, []byte) { |
| 58 |
b.Helper() |
| 59 |
require.GreaterOrEqual(b, count, 1) |
| 60 |
|
| 61 |
before := time.Now().Add(-24 * time.Hour) |
| 62 |
after := time.Now().Add(24 * time.Hour) |
| 63 |
|
| 64 |
ca, _, caKey, pem := cert_test.NewTestCaCert( |
| 65 |
cert.Version2, |
| 66 |
cert.Curve_CURVE25519, |
| 67 |
before, |
| 68 |
after, |
| 69 |
nil, |
| 70 |
nil, |
| 71 |
nil, |
| 72 |
) |
| 73 |
|
| 74 |
buf := bytes.NewBuffer(pem) |
| 75 |
buf.Write([]byte("\n# a comment!\n")) |
| 76 |
|
| 77 |
for i := 1; i < count; i++ { |
| 78 |
_, _, _, extraPEM := cert_test.NewTestCaCert( |
| 79 |
cert.Version2, |
| 80 |
cert.Curve_CURVE25519, |
| 81 |
time.Now(), |
| 82 |
time.Now().Add(time.Hour), |
| 83 |
nil, |
| 84 |
nil, |
| 85 |
nil, |
| 86 |
) |
| 87 |
buf.Write([]byte("\n# a comment!\n")) |
| 88 |
buf.Write(extraPEM) |
| 89 |
} |
| 90 |
|
| 91 |
return ca, caKey, buf.Bytes() |
| 92 |
} |
| 93 |
|
| 94 |
func writePKIFiles(b *testing.B, dir string, ca cert.Certificate, caKey []byte, caBundle []byte) (string, string, string) { |
| 95 |
b.Helper() |
| 96 |
|
| 97 |
networks := []netip.Prefix{netip.MustParsePrefix("10.0.0.1/24")} |
| 98 |
|
| 99 |
_, _, keyPEM, certPEM := cert_test.NewTestCert( |
| 100 |
cert.Version2, |
| 101 |
cert.Curve_CURVE25519, |
| 102 |
ca, |
| 103 |
caKey, |
| 104 |
"reload-benchmark", |
| 105 |
time.Now(), |
| 106 |
time.Now().Add(time.Hour), |
| 107 |
networks, |
| 108 |
nil, |
| 109 |
nil, |
| 110 |
) |
| 111 |
|
| 112 |
caPath := filepath.Join(dir, "ca.pem") |
| 113 |
certPath := filepath.Join(dir, "cert.pem") |
| 114 |
keyPath := filepath.Join(dir, "key.pem") |
| 115 |
|
| 116 |
require.NoError(b, os.WriteFile(caPath, caBundle, 0o600)) |
| 117 |
require.NoError(b, os.WriteFile(certPath, certPEM, 0o600)) |
| 118 |
require.NoError(b, os.WriteFile(keyPath, keyPEM, 0o600)) |
| 119 |
|
| 120 |
return caPath, certPath, keyPath |
| 121 |
} |