From bbc10cb105b2ecfa2127d94f66686ad4b76b18ab Mon Sep 17 00:00:00 2001 From: yusing Date: Sun, 26 Jan 2025 15:08:10 +0800 Subject: [PATCH] fix serialization, added benchmark tests, updated next release docs --- cmd/main_prof.go | 3 ++ internal/docker/label_test.go | 18 ++++++++ internal/task/task_test.go | 8 ++++ internal/utils/serialization.go | 14 +++++-- internal/utils/serialization_test.go | 49 ++++++++++++++++++++++ internal/utils/strutils/split_join_test.go | 24 +++++++++++ next-release.md | 8 +++- 7 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 internal/docker/label_test.go diff --git a/cmd/main_prof.go b/cmd/main_prof.go index 29702171..a3fbaa1f 100644 --- a/cmd/main_prof.go +++ b/cmd/main_prof.go @@ -7,10 +7,13 @@ import ( "net/http" _ "net/http/pprof" "runtime" + "runtime/debug" ) func initProfiling() { runtime.GOMAXPROCS(2) + debug.SetMemoryLimit(100 * 1024 * 1024) + debug.SetMaxStack(15 * 1024 * 1024) go func() { log.Println(http.ListenAndServe(":7777", nil)) }() diff --git a/internal/docker/label_test.go b/internal/docker/label_test.go new file mode 100644 index 00000000..d5167d36 --- /dev/null +++ b/internal/docker/label_test.go @@ -0,0 +1,18 @@ +package docker_test + +import ( + "testing" + + "github.com/yusing/go-proxy/internal/docker" +) + +func BenchmarkParseLabels(b *testing.B) { + for range b.N { + _, _ = docker.ParseLabels(map[string]string{ + "proxy.a.host": "localhost", + "proxy.a.port": "4444", + "proxy.a.scheme": "http", + "proxy.a.middlewares.request.hide_headers": "X-Header1,X-Header2", + }) + } +} diff --git a/internal/task/task_test.go b/internal/task/task_test.go index 64b4d565..f9c35aa8 100644 --- a/internal/task/task_test.go +++ b/internal/task/task_test.go @@ -111,3 +111,11 @@ func TestFinishMultipleCalls(t *testing.T) { } wg.Wait() } + +func BenchmarkTasks(b *testing.B) { + for range b.N { + task := testTask() + task.Subtask("", true).Finish(nil) + task.Finish(nil) + } +} diff --git a/internal/utils/serialization.go b/internal/utils/serialization.go index f552c298..d989cd56 100644 --- a/internal/utils/serialization.go +++ b/internal/utils/serialization.go @@ -204,8 +204,13 @@ func Deserialize(src SerializedObject, dst any) E.Error { } if hasValidateTag { errs.Add(ValidateWithFieldTags(dstV.Interface())) - } else if validator, ok := dstV.Addr().Interface().(CustomValidator); ok { - errs.Add(validator.Validate()) + } else { + if dstV.CanAddr() { + dstV = dstV.Addr() + } + if validator, ok := dstV.Interface().(CustomValidator); ok { + errs.Add(validator.Validate()) + } } return errs.Error() case reflect.Map: @@ -222,7 +227,10 @@ func Deserialize(src SerializedObject, dst any) E.Error { errs.Add(err.Subject(k)) } } - if validator, ok := dstV.Addr().Interface().(CustomValidator); ok { + if dstV.CanAddr() { + dstV = dstV.Addr() + } + if validator, ok := dstV.Interface().(CustomValidator); ok { errs.Add(validator.Validate()) } return errs.Error() diff --git a/internal/utils/serialization_test.go b/internal/utils/serialization_test.go index fded12b7..23696e9a 100644 --- a/internal/utils/serialization_test.go +++ b/internal/utils/serialization_test.go @@ -6,6 +6,7 @@ import ( "testing" . "github.com/yusing/go-proxy/internal/utils/testing" + "gopkg.in/yaml.v3" ) func TestDeserialize(t *testing.T) { @@ -187,6 +188,20 @@ func TestStringToSlice(t *testing.T) { }) } +func BenchmarkStringToSlice(b *testing.B) { + for range b.N { + dst := make([]int, 0) + _, _ = ConvertString("- 1\n- 2\n- 3", reflect.ValueOf(&dst)) + } +} + +func BenchmarkStringToSliceYAML(b *testing.B) { + for range b.N { + dst := make([]int, 0) + _ = yaml.Unmarshal([]byte("- 1\n- 2\n- 3"), &dst) + } +} + func TestStringToMap(t *testing.T) { t.Run("yaml-like", func(t *testing.T) { dst := make(map[string]string) @@ -197,6 +212,20 @@ func TestStringToMap(t *testing.T) { }) } +func BenchmarkStringToMap(b *testing.B) { + for range b.N { + dst := make(map[string]string) + _, _ = ConvertString(" a: b\n c: d", reflect.ValueOf(&dst)) + } +} + +func BenchmarkStringToMapYAML(b *testing.B) { + for range b.N { + dst := make(map[string]string) + _ = yaml.Unmarshal([]byte(" a: b\n c: d"), &dst) + } +} + func TestStringToStruct(t *testing.T) { t.Run("yaml-like", func(t *testing.T) { dst := struct { @@ -212,3 +241,23 @@ func TestStringToStruct(t *testing.T) { }{"a", 123}) }) } + +func BenchmarkStringToStruct(b *testing.B) { + for range b.N { + dst := struct { + A string `json:"a"` + B int `json:"b"` + }{} + _, _ = ConvertString(" a: a\n b: 123", reflect.ValueOf(&dst)) + } +} + +func BenchmarkStringToStructYAML(b *testing.B) { + for range b.N { + dst := struct { + A string `yaml:"a"` + B int `yaml:"b"` + }{} + _ = yaml.Unmarshal([]byte(" a: a\n b: 123"), &dst) + } +} diff --git a/internal/utils/strutils/split_join_test.go b/internal/utils/strutils/split_join_test.go index 96e2fe69..be052ab3 100644 --- a/internal/utils/strutils/split_join_test.go +++ b/internal/utils/strutils/split_join_test.go @@ -36,3 +36,27 @@ func TestSplit(t *testing.T) { }) } } + +func BenchmarkSplitRune(b *testing.B) { + for range b.N { + SplitRune(alphaNumeric, ',') + } +} + +func BenchmarkSplitRuneStdlib(b *testing.B) { + for range b.N { + strings.Split(alphaNumeric, ",") + } +} + +func BenchmarkJoinRune(b *testing.B) { + for range b.N { + JoinRune(SplitRune(alphaNumeric, ','), ',') + } +} + +func BenchmarkJoinRuneStdlib(b *testing.B) { + for range b.N { + strings.Join(SplitRune(alphaNumeric, ','), ",") + } +} diff --git a/next-release.md b/next-release.md index 31099401..9b8272f4 100644 --- a/next-release.md +++ b/next-release.md @@ -5,6 +5,7 @@ GoDoxy v0.9.0 expected changes - Edit dashboard app config (e.g. icon, name, category, etc.) - Toggle show / hide apps - Health bubbles, latency, etc. rich info on dashboard items + - UI config editor ![{7829FA41-5733-4BAD-8183-CDF093CEC6F2}](https://github.com/user-attachments/assets/4bb371f4-6e4c-425c-89b2-b9e962bdd46f) ![{29A4608C-607F-43C9-A542-15EC6B9D024E}](https://github.com/user-attachments/assets/8469cfaf-dc37-4b6e-9f29-c44eea91bb82) ![{83118DF5-9D46-4D00-9CEF-C0F6C8D18C4B}](https://github.com/user-attachments/assets/856140f0-78bb-4a76-98f2-ad47544a3515) @@ -99,14 +100,19 @@ GoDoxy v0.9.0 expected changes - `GODOXY_OIDC_REDIRECT_URL` - `GODOXY_OIDC_SCOPES` _(optional)_ - `GODOXY_OIDC_ALLOWED_USERS` + - `GODOXY_OIDC_ALLOWED_GROUPS` _(optional)_ - Use OpenID Connect to authenticate GoDoxy's WebUI and all your services (SSO) + ```yaml - # default + # default proxy.app.middlewares.oidc: # override allowed users proxy.app.middlewares.oidc.allowed_users: user1, user2 + + # override allowed groups + proxy.app.middlewares.oidc.allowed_groups: group1, group2 ``` - Caddyfile like rules