mirror of
https://github.com/hanzoai/metrics.git
synced 2026-10-30 20:03:39 +00:00
The prompb and prompbmarshal share exactly the same models and provide
marshal and unmarshale capabilities for them. This creates duplication
(changes in one model has to be made in another, case with metadata) and
confusion where for example you compare same looking models but golang
says they are not the same (because of the type).
This commit merge prompbmarshal logic into prompb so the rest of the
code is aligned on prompb models.
Moves samplesPool and labelsPool to WriteRequestUnmarshaller.
Make WriteRequest struct clean from unmarshal logic.
The benchmark shows no significant changes:
$benchstat prompbmarshal.bench prompb2.bench
goos: darwin
goarch: arm64
pkg: github.com/VictoriaMetrics/VictoriaMetrics/lib/prompb
cpu: Apple M1 Pro
│ prompbmarshal.bench │ prompb2.bench │
│ sec/op │ sec/op vs base │
WriteRequestUnmarshalProtobuf-10 189.2µ ± 5% 190.8µ ± 8% ~ (p=0.579 n=10)
WriteRequestMarshalProtobuf-10 145.3µ ± 7% 143.6µ ± 2% ~ (p=0.143 n=10)
geomean 165.8µ 165.5µ -0.14%
│ prompbmarshal.bench │ prompb2.bench │
│ B/s │ B/s vs base │
WriteRequestUnmarshalProtobuf-10 50.42Mi ± 5% 49.99Mi ± 8% ~ (p=0.593 n=10)
WriteRequestMarshalProtobuf-10 65.64Mi ± 7% 66.39Mi ± 2% ~ (p=0.143 n=10)
geomean 57.53Mi 57.61Mi +0.14%
│ prompbmarshal.bench │ prompb2.bench │
│ B/op │ B/op vs base │
WriteRequestUnmarshalProtobuf-10 27.70Ki ± 4% 26.90Ki ± 7% ~ (p=0.190 n=10)
WriteRequestMarshalProtobuf-10 3.267Ki ± 12% 3.273Ki ± 12% ~ (p=0.971 n=10)
geomean 9.514Ki 9.383Ki -1.38%
│ prompbmarshal.bench │ prompb2.bench │
│ allocs/op │ allocs/op vs base │
WriteRequestUnmarshalProtobuf-10 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹
WriteRequestMarshalProtobuf-10 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹
geomean ² +0.00% ²
¹ all samples are equal
² summaries must be >0 to compute geomean
104 lines
3.3 KiB
Go
104 lines
3.3 KiB
Go
package unittest
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/url"
|
|
"reflect"
|
|
"sort"
|
|
"time"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/datasource"
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompb"
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutil"
|
|
"github.com/VictoriaMetrics/metricsql"
|
|
)
|
|
|
|
// metricsqlTestCase holds metricsql_expr_test cases defined in test file
|
|
type metricsqlTestCase struct {
|
|
Expr string `yaml:"expr"`
|
|
EvalTime *promutil.Duration `yaml:"eval_time"`
|
|
ExpSamples []expSample `yaml:"exp_samples"`
|
|
}
|
|
|
|
type expSample struct {
|
|
Labels string `yaml:"labels"`
|
|
Value float64 `yaml:"value"`
|
|
}
|
|
|
|
// checkMetricsqlCase will check metricsql_expr_test cases
|
|
func checkMetricsqlCase(cases []metricsqlTestCase, q datasource.QuerierBuilder) (checkErrs []error) {
|
|
queries := q.BuildWithParams(datasource.QuerierParams{QueryParams: url.Values{"nocache": {"1"}, "latency_offset": {"1ms"}}, DataSourceType: "prometheus"})
|
|
Outer:
|
|
for _, mt := range cases {
|
|
result, _, err := queries.Query(context.Background(), mt.Expr, durationToTime(mt.EvalTime))
|
|
if err != nil {
|
|
checkErrs = append(checkErrs, fmt.Errorf(" expr: %q, time: %s, err: %w", mt.Expr,
|
|
mt.EvalTime.Duration().String(), err))
|
|
continue
|
|
}
|
|
var gotSamples []parsedSample
|
|
for _, s := range result.Data {
|
|
sort.Slice(s.Labels, func(i, j int) bool {
|
|
return s.Labels[i].Name < s.Labels[j].Name
|
|
})
|
|
gotSamples = append(gotSamples, parsedSample{
|
|
Labels: s.Labels,
|
|
Value: s.Values[0],
|
|
})
|
|
}
|
|
var expSamples []parsedSample
|
|
for _, s := range mt.ExpSamples {
|
|
expLb := []prompb.Label{}
|
|
if s.Labels != "" {
|
|
metricsqlExpr, err := metricsql.Parse(s.Labels)
|
|
if err != nil {
|
|
checkErrs = append(checkErrs, fmt.Errorf("\n expr: %q, time: %s, err: %v", mt.Expr,
|
|
mt.EvalTime.Duration().String(), fmt.Errorf("failed to parse labels %q: %w", s.Labels, err)))
|
|
continue Outer
|
|
}
|
|
metricsqlMetricExpr, ok := metricsqlExpr.(*metricsql.MetricExpr)
|
|
if !ok || len(metricsqlMetricExpr.LabelFilterss) > 1 {
|
|
checkErrs = append(checkErrs, fmt.Errorf("\n expr: %q, time: %s, err: %v", mt.Expr,
|
|
mt.EvalTime.Duration().String(), fmt.Errorf("got invalid exp_samples: %q", s.Labels)))
|
|
continue Outer
|
|
}
|
|
if len(metricsqlMetricExpr.LabelFilterss) > 0 {
|
|
for _, l := range metricsqlMetricExpr.LabelFilterss[0] {
|
|
expLb = append(expLb, prompb.Label{
|
|
Name: l.Label,
|
|
Value: l.Value,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
sort.Slice(expLb, func(i, j int) bool {
|
|
return expLb[i].Name < expLb[j].Name
|
|
})
|
|
expSamples = append(expSamples, parsedSample{
|
|
Labels: expLb,
|
|
Value: s.Value,
|
|
})
|
|
}
|
|
sort.Slice(expSamples, func(i, j int) bool {
|
|
return datasource.LabelCompare(expSamples[i].Labels, expSamples[j].Labels) <= 0
|
|
})
|
|
sort.Slice(gotSamples, func(i, j int) bool {
|
|
return datasource.LabelCompare(gotSamples[i].Labels, gotSamples[j].Labels) <= 0
|
|
})
|
|
if !reflect.DeepEqual(expSamples, gotSamples) {
|
|
checkErrs = append(checkErrs, fmt.Errorf("\n expr: %q, time: %s,\n exp: %v\n got: %v", mt.Expr,
|
|
mt.EvalTime.Duration().String(), parsedSamplesString(expSamples), parsedSamplesString(gotSamples)))
|
|
}
|
|
|
|
}
|
|
return
|
|
}
|
|
|
|
func durationToTime(pd *promutil.Duration) time.Time {
|
|
if pd == nil {
|
|
return time.Time{}
|
|
}
|
|
return time.UnixMilli(pd.Duration().Milliseconds())
|
|
}
|