2026-05-02 12:43:43 +08:00
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
2026-05-02 13:36:06 +08:00
|
|
|
func BenchmarkLoad(b *testing.B) {
|
2026-05-02 12:43:43 +08:00
|
|
|
os.WriteFile("bench.json", []byte(`{"name":"bench-test","sets":[1,2,3]}`), 0644)
|
|
|
|
|
defer os.Remove("bench.json")
|
2026-05-02 13:36:06 +08:00
|
|
|
|
2026-05-02 12:43:43 +08:00
|
|
|
var conf struct {
|
|
|
|
|
Name string
|
|
|
|
|
Sets []int
|
|
|
|
|
}
|
2026-05-02 13:36:06 +08:00
|
|
|
|
2026-05-02 12:43:43 +08:00
|
|
|
b.ResetTimer()
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2026-05-02 13:36:06 +08:00
|
|
|
_ = Load("bench", &conf)
|
2026-05-02 12:43:43 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkApplyEnvOverrides(b *testing.B) {
|
|
|
|
|
os.Setenv("APP_DB_HOST", "localhost")
|
|
|
|
|
os.Setenv("APP_DB_PORT", "5432")
|
|
|
|
|
os.Setenv("APP_SERVER_TIMEOUT", "30s")
|
2026-05-02 13:36:06 +08:00
|
|
|
|
2026-05-02 12:43:43 +08:00
|
|
|
var conf struct {
|
|
|
|
|
App struct {
|
|
|
|
|
Db struct {
|
|
|
|
|
Host string
|
|
|
|
|
Port int
|
|
|
|
|
}
|
|
|
|
|
Server struct {
|
|
|
|
|
Timeout Duration
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-05-02 13:36:06 +08:00
|
|
|
|
2026-05-02 12:43:43 +08:00
|
|
|
b.ResetTimer()
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
applyEnvOverrides(&conf)
|
|
|
|
|
}
|
|
|
|
|
}
|