refactor(metrics): optimize JSON marshaling and aggregation logic

- Updated JSON marshaling in SystemInfo to use quoted keys.
- Refactored aggregation logic to dynamically append entries.
- Adjusted test cases to reflect changes in data structure and ensure accurate serialization.
This commit is contained in:
yusing
2025-09-14 00:07:34 +08:00
parent d56663d3f9
commit 124069aaa4
4 changed files with 79 additions and 70 deletions

View File

@@ -133,11 +133,11 @@ func TestSerialize(t *testing.T) {
ExpectNoError(t, err)
var v []map[string]any
ExpectNoError(t, json.Unmarshal(s, &v))
ExpectEqual(t, len(v), len(result))
ExpectEqual(t, len(v), len(result.Entries))
for i, m := range v {
for k, v := range m {
// some int64 values are converted to float64 on json.Unmarshal
vv := reflect.ValueOf(result[i][k])
vv := reflect.ValueOf(result.Entries[i][k])
ExpectEqual(t, reflect.ValueOf(v).Convert(vv.Type()).Interface(), vv.Interface())
}
}
@@ -177,7 +177,7 @@ func BenchmarkSerialize(b *testing.B) {
b.Run("json", func(b *testing.B) {
for b.Loop() {
for _, query := range allQueries {
_, _ = json.Marshal([]map[string]any(queries[string(query)]))
_, _ = json.Marshal([]map[string]any(queries[string(query)].Entries))
}
}
})