Files
Max Kotliar be3eddf425 lib/prompb: Merge prompbmarshal logic into prompb
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
2025-07-31 01:04:11 +03:00

82 lines
2.2 KiB
Go

package promscrape
import (
"fmt"
"math/rand"
"testing"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/auth"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompb"
)
func TestScraperReload(t *testing.T) {
f := func(oldCfgData, newCfgData string, reloadExpected bool) {
pushData := func(_ *auth.Token, _ *prompb.WriteRequest) {}
globalStopChan = make(chan struct{})
defer close(globalStopChan)
randName := rand.Int()
sg := newScraperGroup(fmt.Sprintf("static_configs_%d", randName), pushData, globalStopChan)
defer sg.stop()
scrapeConfigPath := "test-scrape.yaml"
var oldCfg, newCfg Config
if err := oldCfg.parseData([]byte(oldCfgData), scrapeConfigPath); err != nil {
t.Fatalf("cannot create old config: %s", err)
}
oldSws := oldCfg.getStaticScrapeWork()
sg.update(oldSws)
oldChangesCount := sg.changesCount.Get()
if err := newCfg.parseData([]byte(newCfgData), scrapeConfigPath); err != nil {
t.Fatalf("cannot create new config: %s", err)
}
doReload := (&newCfg).mustRestart(&oldCfg)
if doReload != reloadExpected {
t.Errorf("unexpected reload behaviour:\nexpected: %t\nactual: %t\n", reloadExpected, doReload)
}
newSws := newCfg.getStaticScrapeWork()
sg.update(newSws)
newChangesCount := sg.changesCount.Get()
if (newChangesCount != oldChangesCount) != reloadExpected {
t.Errorf("expected reload behaviour:\nexpected reload happen: %t\nactual reload happen: %t", reloadExpected, newChangesCount != oldChangesCount)
}
}
f(`
scrape_configs:
- job_name: node-exporter
static_configs:
- targets:
- localhost:8429`, `
scrape_configs:
- job_name: node-exporter
static_configs:
- targets:
- localhost:8429`, false)
f(`
scrape_configs:
- job_name: node-exporter
static_configs:
- targets:
- localhost:8429`, `
scrape_configs:
- job_name: node-exporter
static_configs:
- targets:
- localhost:8429
- localhost:8428`, true)
f(`
scrape_configs:
- job_name: node-exporter
max_scrape_size: 1KiB
static_configs:
- targets:
- localhost:8429`, `
scrape_configs:
- job_name: node-exporter
max_scrape_size: 2KiB
static_configs:
- targets:
- localhost:8429`, true)
}