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