mirror of
https://github.com/yusing/godoxy.git
synced 2026-02-19 00:47:41 +01:00
This is a large-scale refactoring across the codebase that replaces the custom `gperr.Error` type with Go's standard `error` interface. The changes include: - Replacing `gperr.Error` return types with `error` in function signatures - Using `errors.New()` and `fmt.Errorf()` instead of `gperr.New()` and `gperr.Errorf()` - Using `%w` format verb for error wrapping instead of `.With()` method - Replacing `gperr.Subject()` calls with `gperr.PrependSubject()` - Converting error logging from `gperr.Log*()` functions to zerolog's `.Err().Msg()` pattern - Update NewLogger to handle multiline error message - Updating `goutils` submodule to latest commit This refactoring aligns with Go idioms and removes the dependency on custom error handling abstractions in favor of standard library patterns.
96 lines
2.1 KiB
Go
96 lines
2.1 KiB
Go
package serialization
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/goccy/go-yaml"
|
|
)
|
|
|
|
func BenchmarkDeserialize(b *testing.B) {
|
|
type AnonymousStruct struct {
|
|
J float64 `json:"j"`
|
|
K int `json:"k"`
|
|
}
|
|
type complexStruct struct {
|
|
A string `json:"a"`
|
|
B int `json:"b"`
|
|
C []uint `json:"c"`
|
|
D map[string]string `json:"d"`
|
|
E []map[string]string `json:"e"`
|
|
F *complexStruct
|
|
G struct {
|
|
G1 float64 `json:"g1"`
|
|
G2 int `json:"g2"`
|
|
}
|
|
H []*complexStruct `json:"h"`
|
|
*AnonymousStruct
|
|
}
|
|
src := SerializedObject{
|
|
"a": "a",
|
|
"b": "123",
|
|
"c": "1,2,3",
|
|
"d": "a: a\nb: b\nc: c",
|
|
"e": "- a: a\n b: b\n c: c",
|
|
"f": map[string]any{"a": "a", "b": "456", "c": `1,2,3`},
|
|
"g": map[string]any{"g1": "1.23", "g2": 123},
|
|
"h": []map[string]any{{"a": 123, "b": "456", "c": `["1","2","3"]`}},
|
|
"j": "1.23",
|
|
"k": 123,
|
|
}
|
|
for b.Loop() {
|
|
dst := complexStruct{}
|
|
err := MapUnmarshalValidate(src, &dst)
|
|
if err != nil {
|
|
b.Fatal(err.Error())
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkStringToSlice(b *testing.B) {
|
|
b.Run("ConvertString", func(b *testing.B) {
|
|
for b.Loop() {
|
|
dst := make([]int, 0)
|
|
_, _ = ConvertString("- 1\n- 2\n- 3", reflect.ValueOf(&dst))
|
|
}
|
|
})
|
|
b.Run("yaml.Unmarshal", func(b *testing.B) {
|
|
for b.Loop() {
|
|
dst := make([]int, 0)
|
|
_ = yaml.Unmarshal([]byte("- 1\n- 2\n- 3"), &dst)
|
|
}
|
|
})
|
|
}
|
|
|
|
func BenchmarkStringToMap(b *testing.B) {
|
|
b.Run("ConvertString", func(b *testing.B) {
|
|
for b.Loop() {
|
|
dst := make(map[string]string)
|
|
_, _ = ConvertString(" a: b\n c: d", reflect.ValueOf(&dst))
|
|
}
|
|
})
|
|
b.Run("yaml.Unmarshal", func(b *testing.B) {
|
|
for b.Loop() {
|
|
dst := make(map[string]string)
|
|
_ = yaml.Unmarshal([]byte(" a: b\n c: d"), &dst)
|
|
}
|
|
})
|
|
}
|
|
|
|
func BenchmarkStringToStruct(b *testing.B) {
|
|
dst := struct {
|
|
A string `json:"a"`
|
|
B int `json:"b"`
|
|
}{}
|
|
b.Run("ConvertString", func(b *testing.B) {
|
|
for b.Loop() {
|
|
_, _ = ConvertString(" a: a\n b: 123", reflect.ValueOf(&dst))
|
|
}
|
|
})
|
|
b.Run("yaml.Unmarshal", func(b *testing.B) {
|
|
for b.Loop() {
|
|
_ = yaml.Unmarshal([]byte(" a: a\n b: 123"), &dst)
|
|
}
|
|
})
|
|
}
|