| 1 |
package gitconfig |
| 2 |
|
| 3 |
import ( |
| 4 |
"os" |
| 5 |
"path/filepath" |
| 6 |
"strconv" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
func BenchmarkLoadConfig(b *testing.B) { |
| 11 |
td := b.TempDir() |
| 12 |
configPath := filepath.Join(td, "config") |
| 13 |
content := "[user]\n\tname = Bench User\n\temail = [email protected]\n[core]\n\teditor = vim\n" |
| 14 |
|
| 15 |
if err := os.WriteFile(configPath, []byte(content), 0o644); err != nil { |
| 16 |
b.Fatal(err) |
| 17 |
} |
| 18 |
|
| 19 |
for b.Loop() { |
| 20 |
cfg, err := LoadConfig(configPath) |
| 21 |
if err != nil { |
| 22 |
b.Fatal(err) |
| 23 |
} |
| 24 |
if cfg == nil { |
| 25 |
b.Fatal("nil config") |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
func BenchmarkGet(b *testing.B) { |
| 31 |
td := b.TempDir() |
| 32 |
configPath := filepath.Join(td, "config") |
| 33 |
content := "[user]\n\tname = Bench User\n\temail = [email protected]\n[core]\n\teditor = vim\n" |
| 34 |
|
| 35 |
if err := os.WriteFile(configPath, []byte(content), 0o644); err != nil { |
| 36 |
b.Fatal(err) |
| 37 |
} |
| 38 |
|
| 39 |
cfg, err := LoadConfig(configPath) |
| 40 |
if err != nil { |
| 41 |
b.Fatal(err) |
| 42 |
} |
| 43 |
|
| 44 |
for b.Loop() { |
| 45 |
_, ok := cfg.Get("user.name") |
| 46 |
if !ok { |
| 47 |
b.Fatal("missing key") |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
func BenchmarkSet(b *testing.B) { |
| 53 |
td := b.TempDir() |
| 54 |
configPath := filepath.Join(td, "config") |
| 55 |
content := "[user]\n\tname = Bench User\n\temail = [email protected]\n[core]\n\teditor = vim\n" |
| 56 |
|
| 57 |
if err := os.WriteFile(configPath, []byte(content), 0o644); err != nil { |
| 58 |
b.Fatal(err) |
| 59 |
} |
| 60 |
|
| 61 |
cfg, err := LoadConfig(configPath) |
| 62 |
if err != nil { |
| 63 |
b.Fatal(err) |
| 64 |
} |
| 65 |
cfg.noWrites = true |
| 66 |
|
| 67 |
b.ResetTimer() |
| 68 |
|
| 69 |
for i := range b.N { |
| 70 |
if err := cfg.Set("user.name", strconv.Itoa(i)); err != nil { |
| 71 |
b.Fatal(err) |
| 72 |
} |
| 73 |
} |
| 74 |
} |