mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-23 20:37:12 +01:00
4f0525852f
Allocations are reduced by implementing custom json parser via fastjson lib. The change also re-uses `promInstant` object in attempt to reduce number of allocations when parsing big responses, as usually happens with heavy recording rules. ``` name old allocs/op new allocs/op delta ParsePrometheusResponse/Instant-10 9.65k ± 0% 5.60k ± 0% ~ (p=1.000 n=1+1) ``` Signed-off-by: hagen1778 <roman@victoriametrics.com> --------- Signed-off-by: hagen1778 <roman@victoriametrics.com>
44 lines
1.0 KiB
Go
44 lines
1.0 KiB
Go
package datasource
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func BenchmarkMetrics(b *testing.B) {
|
|
payload := []byte(`[{"metric":{"__name__":"vm_rows"},"value":[1583786142,"13763"]},{"metric":{"__name__":"vm_requests", "foo":"bar", "baz": "qux"},"value":[1583786140,"2000"]}]`)
|
|
|
|
var pi promInstant
|
|
if err := pi.Unmarshal(payload); err != nil {
|
|
b.Fatalf(err.Error())
|
|
}
|
|
b.Run("Instant", func(b *testing.B) {
|
|
for i := 0; i < b.N; i++ {
|
|
_, _ = pi.metrics()
|
|
}
|
|
})
|
|
}
|
|
|
|
func BenchmarkParsePrometheusResponse(b *testing.B) {
|
|
req, _ := http.NewRequest("GET", "", nil)
|
|
resp := &http.Response{StatusCode: http.StatusOK}
|
|
data, err := os.ReadFile("testdata/instant_response.json")
|
|
if err != nil {
|
|
b.Fatalf("error while reading file: %s", err)
|
|
}
|
|
resp.Body = io.NopCloser(bytes.NewReader(data))
|
|
|
|
b.Run("Instant", func(b *testing.B) {
|
|
for i := 0; i < b.N; i++ {
|
|
_, err := parsePrometheusResponse(req, resp)
|
|
if err != nil {
|
|
b.Fatalf("unexpected parse err: %s", err)
|
|
}
|
|
resp.Body = io.NopCloser(bytes.NewReader(data))
|
|
}
|
|
})
|
|
}
|