Repository: prometheus/prometheus
File: scrape/scrape_test.go (Go)

Code before:
	}
	return buf.Bytes()
}

func TestPromTextToProto(t *testing.T) {
	metricsText := readTextParseTestMetrics(t)
	// TODO(bwplotka): Windows adds \r for new lines which is
	// not handled correctly in the expfmt parser, fix it.
	metricsText = bytes.ReplaceAll(metricsText, []byte("\r"), nil)

	metricsProto := promTextToProto(t, metricsText)
	d := expfmt.NewDecoder(bytes.NewReader(metricsProto), expfmt.NewFormat(expfmt.TypeProtoDelim))

	var got []string
	for {
		mf := &dto.MetricFamily{}
		if err := d.Decode(mf); err != nil {
			if errors.Is(err, io.EOF) {
				break
			}
			t.Fatal(err)
		}
		got = append(got, mf.GetName())
	}
	require.Len(t, got, 59)
	// Check first few to see if those are not dups.
	require.Equal(t, "go_gc_duration_seconds", got[0])
	require.Equal(t, "prometheus_evaluator_duration_seconds", got[32])
	require.Equal(t, "prometheus_treecache_zookeeper_failures_total", got[58])
}

/*
	export bench=append-v1 && go test \
		-run '^$' -bench '^BenchmarkScrapeLoopAppend' \
		-benchtime 5s -count 6 -cpu 2 -timeout 999m \
		| tee ${bench}.txt
*/
func BenchmarkScrapeLoopAppend(b *testing.B) {
	for _, data := range []struct {
		name         string
		parsableText []byte
	}{
		{name: "1Fam1000Gauges", parsableText: makeTestGauges(1000)},        // ~33.8 KB, ~38.8 KB in proto
		{name: "59FamsAllTypes", parsableText: readTextParseTestMetrics(b)}, // ~33.3 KB, ~13.2 KB in proto.
	} {
		b.Run(fmt.Sprintf("data=%v", data.name), func(b *testing.B) {
			metricsProto := promTextToProto(b, data.parsableText)

			for _, bcase := range []struct {
				name        string
				contentType string

Review comment (suggestion):
nit:
```suggestion
	// Check a few to see if those are not dups.
```

Code after:
	}
	return buf.Bytes()
}

func TestPromTextToProto(t *testing.T) {
	metricsText := readTextParseTestMetrics(t)
	// TODO(bwplotka): Windows adds \r for new lines which is
	// not handled correctly in the expfmt parser, fix it.
	metricsText = bytes.ReplaceAll(metricsText, []byte("\r"), nil)

	metricsProto := promTextToProto(t, metricsText)
	d := expfmt.NewDecoder(bytes.NewReader(metricsProto), expfmt.NewFormat(expfmt.TypeProtoDelim))

	var got []string
	for {
		mf := &dto.MetricFamily{}
		if err := d.Decode(mf); err != nil {
			if errors.Is(err, io.EOF) {
				break
			}
			t.Fatal(err)
		}
		got = append(got, mf.GetName())
	}
	require.Len(t, got, 59)
	// Check a few to see if those are not dups.
	require.Equal(t, "go_gc_duration_seconds", got[0])
	require.Equal(t, "prometheus_evaluator_duration_seconds", got[32])
	require.Equal(t, "prometheus_treecache_zookeeper_failures_total", got[58])
}

/*
	export bench=append-v1 && go test \
		-run '^$' -bench '^BenchmarkScrapeLoopAppend' \
		-benchtime 5s -count 6 -cpu 2 -timeout 999m \
		| tee ${bench}.txt
*/
func BenchmarkScrapeLoopAppend(b *testing.B) {
	for _, data := range []struct {
		name         string
		parsableText []byte
	}{
		{name: "1Fam1000Gauges", parsableText: makeTestGauges(1000)},        // ~33.8 KB, ~38.8 KB in proto
		{name: "59FamsAllTypes", parsableText: readTextParseTestMetrics(b)}, // ~33.3 KB, ~13.2 KB in proto.
	} {
		b.Run(fmt.Sprintf("data=%v", data.name), func(b *testing.B) {
			metricsProto := promTextToProto(b, data.parsableText)

			for _, bcase := range []struct {
				name        string
				contentType string