pki_hup_benchmark_test.go 2.6 KB

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