load_bench_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package config
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. )
  7. func BenchmarkLoadFromConfigPaths(b *testing.B) {
  8. // Create temp config files with realistic content.
  9. tmpDir := b.TempDir()
  10. globalConfig := filepath.Join(tmpDir, "global.json")
  11. localConfig := filepath.Join(tmpDir, "local.json")
  12. globalContent := []byte(`{
  13. "providers": {
  14. "openai": {
  15. "api_key": "$OPENAI_API_KEY",
  16. "base_url": "https://api.openai.com/v1"
  17. },
  18. "anthropic": {
  19. "api_key": "$ANTHROPIC_API_KEY",
  20. "base_url": "https://api.anthropic.com"
  21. }
  22. },
  23. "options": {
  24. "tui": {
  25. "theme": "dark"
  26. }
  27. }
  28. }`)
  29. localContent := []byte(`{
  30. "providers": {
  31. "openai": {
  32. "api_key": "sk-override-key"
  33. }
  34. },
  35. "options": {
  36. "context_paths": ["README.md", "AGENTS.md"]
  37. }
  38. }`)
  39. if err := os.WriteFile(globalConfig, globalContent, 0o644); err != nil {
  40. b.Fatal(err)
  41. }
  42. if err := os.WriteFile(localConfig, localContent, 0o644); err != nil {
  43. b.Fatal(err)
  44. }
  45. configPaths := []string{globalConfig, localConfig}
  46. b.ReportAllocs()
  47. for b.Loop() {
  48. _, err := loadFromConfigPaths(configPaths)
  49. if err != nil {
  50. b.Fatal(err)
  51. }
  52. }
  53. }
  54. func BenchmarkLoadFromConfigPaths_MissingFiles(b *testing.B) {
  55. // Test with mix of existing and non-existing paths.
  56. tmpDir := b.TempDir()
  57. existingConfig := filepath.Join(tmpDir, "exists.json")
  58. content := []byte(`{"options": {"tui": {"theme": "dark"}}}`)
  59. if err := os.WriteFile(existingConfig, content, 0o644); err != nil {
  60. b.Fatal(err)
  61. }
  62. configPaths := []string{
  63. filepath.Join(tmpDir, "nonexistent1.json"),
  64. existingConfig,
  65. filepath.Join(tmpDir, "nonexistent2.json"),
  66. }
  67. b.ReportAllocs()
  68. for b.Loop() {
  69. _, err := loadFromConfigPaths(configPaths)
  70. if err != nil {
  71. b.Fatal(err)
  72. }
  73. }
  74. }
  75. func BenchmarkLoadFromConfigPaths_Empty(b *testing.B) {
  76. // Test with no config files.
  77. tmpDir := b.TempDir()
  78. configPaths := []string{
  79. filepath.Join(tmpDir, "nonexistent1.json"),
  80. filepath.Join(tmpDir, "nonexistent2.json"),
  81. }
  82. b.ReportAllocs()
  83. for b.Loop() {
  84. _, err := loadFromConfigPaths(configPaths)
  85. if err != nil {
  86. b.Fatal(err)
  87. }
  88. }
  89. }