mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-27 02:38:33 +02:00
feat: custom json marshaling implementation, replace json and yaml library (#89)
* chore: replace gopkg.in/yaml.v3 vs goccy/go-yaml; replace encoding/json with bytedance/sonic * fix: yaml unmarshal panic * feat: custom json marshaler implementation * chore: fix import and err marshal handling --------- Co-authored-by: yusing <yusing@6uo.me>
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
package systeminfo
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/shirou/gopsutil/v4/sensors"
|
||||
. "github.com/yusing/go-proxy/internal/utils/testing"
|
||||
"github.com/yusing/go-proxy/pkg/json"
|
||||
)
|
||||
|
||||
func TestExcludeDisks(t *testing.T) {
|
||||
@@ -191,8 +191,7 @@ func TestSerialize(t *testing.T) {
|
||||
for _, query := range allQueries {
|
||||
t.Run(query, func(t *testing.T) {
|
||||
_, result := aggregate(entries, url.Values{"aggregate": []string{query}})
|
||||
s, err := result.MarshalJSON()
|
||||
ExpectNoError(t, err)
|
||||
s := result.MarshalJSONTo(nil)
|
||||
var v []map[string]any
|
||||
ExpectNoError(t, json.Unmarshal(s, &v))
|
||||
ExpectEqual(t, len(v), len(result))
|
||||
@@ -206,31 +205,3 @@ func TestSerialize(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONMarshal(b *testing.B) {
|
||||
entries := make([]*SystemInfo, b.N)
|
||||
for i := range b.N {
|
||||
entries[i] = testInfo
|
||||
}
|
||||
queries := map[string]Aggregated{}
|
||||
for _, query := range allQueries {
|
||||
_, result := aggregate(entries, url.Values{"aggregate": []string{query}})
|
||||
queries[query] = result
|
||||
}
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
b.Run("optimized", func(b *testing.B) {
|
||||
for b.Loop() {
|
||||
for _, query := range allQueries {
|
||||
_, _ = queries[query].MarshalJSON()
|
||||
}
|
||||
}
|
||||
})
|
||||
b.Run("json", func(b *testing.B) {
|
||||
for b.Loop() {
|
||||
for _, query := range allQueries {
|
||||
_, _ = json.Marshal([]map[string]any(queries[query]))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user