Skip to content

Instantly share code, notes, and snippets.

@hagen1778
Created August 22, 2024 13:15
Show Gist options
  • Save hagen1778/b9c3ca7f8ca7d6b21aec9777112c5810 to your computer and use it in GitHub Desktop.
Save hagen1778/b9c3ca7f8ca7d6b21aec9777112c5810 to your computer and use it in GitHub Desktop.
vmalert parsing json benchmark
func BenchmarkParsePrometheusResponse(b *testing.B) {
data, err := os.ReadFile("testdata/instant_response.json")
if err != nil {
b.Fatalf("error while reading file: %s", err)
}
runWithMem := func(name string, f func(b *testing.B)) {
var m1, m2 runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&m1)
b.Run(name, f)
runtime.ReadMemStats(&m2)
fmt.Println("MBs allocated at heap:", float64(m2.HeapAlloc-m1.HeapAlloc)/1e6)
fmt.Println("mallocs:", m2.Mallocs-m1.Mallocs)
}
debug.SetGCPercent(-1)
// Parse with std lib and fastsjson for each `metric` object
runWithMem("Instant std+fastjson", func(b *testing.B) {
for i := 0; i < b.N; i++ {
var pi promInstant
err = pi.UnmarshalMixed(data)
if err != nil {
b.Fatalf("unexpected parse err: %s", err)
}
}
})
// Parse with fastjson only
runWithMem("Instant fastjson", func(b *testing.B) {
for i := 0; i < b.N; i++ {
var pi promInstant
err = pi.UnmarshalFastJson(data)
if err != nil {
b.Fatalf("unexpected parse err: %s", err)
}
}
})
// Old parsing before https://github.com/VictoriaMetrics/VictoriaMetrics/pull/6272
runWithMem("Instant std", func(b *testing.B) {
for i := 0; i < b.N; i++ {
var pi promInstantStd
err = json.Unmarshal(data, &pi.Result)
if err != nil {
b.Fatalf("unexpected parse err: %s", err)
}
_, err = pi.metrics()
if err != nil {
b.Fatalf("unexpected parse err: %s", err)
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment