Store filesystem read-only state during mount parsing instead of
serializing and reparsing mount options on every scrape. Rework
textfile metric family conversion to build one desc per family and
normalize label sets by index.
Benchstat:
goos: linux
goarch: amd64
pkg: github.com/prometheus/node_exporter/collector
cpu: AMD Ryzen 9 8945HS w/ Radeon 780M Graphics
│ /tmp/base.txt │ /tmp/current.txt │
│ sec/op │ sec/op vs base │
ParseFilesystemLabels-8 219.78µ ± 0% 98.89µ ± 1% -55.01% (p=0.000 n=20)
TextfileConvertMetricFamily-8 466.0µ ± 1% 223.5µ ± 1% -52.04% (p=0.000 n=20)
geomean 320.0µ 148.7µ -53.55%
│ /tmp/base.txt │ /tmp/current.txt │
│ B/op │ B/op vs base │
ParseFilesystemLabels-8 191.29Ki ± 0% 99.28Ki ± 0% -48.10% (p=0.000 n=20)
TextfileConvertMetricFamily-8 472.0Ki ± 0% 292.8Ki ± 0% -37.95% (p=0.000 n=20)
geomean 300.5Ki 170.5Ki -43.25%
│ /tmp/base.txt │ /tmp/current.txt │
│ allocs/op │ allocs/op vs base │
ParseFilesystemLabels-8 4.169k ± 0% 1.097k ± 0% -73.69% (p=0.000 n=20)
TextfileConvertMetricFamily-8 13.054k ± 0% 7.955k ± 0% -39.06% (p=0.000 n=20)
geomean 7.377k 2.954k -59.96%
Signed-off-by: Kevin Burke <kevin@burke.dev>
pull/3631/head
parent
04d9906f84
commit
231cb5d862
@ -0,0 +1,126 @@ |
||||
//go:build linux
|
||||
|
||||
// Copyright 2026 The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package collector |
||||
|
||||
import ( |
||||
"fmt" |
||||
"io" |
||||
"log/slog" |
||||
"testing" |
||||
|
||||
"github.com/prometheus/client_golang/prometheus" |
||||
dto "github.com/prometheus/client_model/go" |
||||
"github.com/prometheus/procfs" |
||||
) |
||||
|
||||
func BenchmarkParseFilesystemLabels(b *testing.B) { |
||||
mountInfo := benchmarkMountInfo(256) |
||||
|
||||
b.ReportAllocs() |
||||
b.ResetTimer() |
||||
|
||||
for i := 0; i < b.N; i++ { |
||||
filesystems, err := parseFilesystemLabels(mountInfo) |
||||
if err != nil { |
||||
b.Fatal(err) |
||||
} |
||||
if len(filesystems) != len(mountInfo) { |
||||
b.Fatalf("got %d filesystems, want %d", len(filesystems), len(mountInfo)) |
||||
} |
||||
} |
||||
} |
||||
|
||||
func BenchmarkTextfileConvertMetricFamily(b *testing.B) { |
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
||||
metricFamily := benchmarkMetricFamily(256, 8) |
||||
ch := make(chan prometheus.Metric, len(metricFamily.Metric)+1) |
||||
|
||||
b.ReportAllocs() |
||||
b.ResetTimer() |
||||
|
||||
for i := 0; i < b.N; i++ { |
||||
convertMetricFamily(metricFamily, ch, logger) |
||||
drainMetrics(ch) |
||||
} |
||||
} |
||||
|
||||
func benchmarkMountInfo(count int) []*procfs.MountInfo { |
||||
mountInfo := make([]*procfs.MountInfo, 0, count) |
||||
for i := 0; i < count; i++ { |
||||
mountInfo = append(mountInfo, &procfs.MountInfo{ |
||||
MajorMinorVer: fmt.Sprintf("%d:%d", i/16+8, i%16), |
||||
Source: fmt.Sprintf("/dev/vd%c", 'a'+rune(i%26)), |
||||
MountPoint: fmt.Sprintf("/var/lib/containers/storage/overlay/%d", i), |
||||
FSType: "ext4", |
||||
Options: map[string]string{ |
||||
"rw": "", |
||||
"relatime": "", |
||||
"discard": "", |
||||
}, |
||||
SuperOptions: map[string]string{ |
||||
"rw": "", |
||||
"errors": "remount-ro", |
||||
}, |
||||
}) |
||||
} |
||||
|
||||
return mountInfo |
||||
} |
||||
|
||||
func benchmarkMetricFamily(metricCount, labelCount int) *dto.MetricFamily { |
||||
metrics := make([]*dto.Metric, 0, metricCount) |
||||
for i := 0; i < metricCount; i++ { |
||||
labels := make([]*dto.LabelPair, 0, labelCount) |
||||
for j := 0; j < labelCount; j++ { |
||||
// Leave some labels out on each metric so the benchmark exercises
|
||||
// label union normalization as well as desc reuse.
|
||||
if (i+j)%3 == 0 { |
||||
continue |
||||
} |
||||
name := fmt.Sprintf("label_%d", j) |
||||
value := fmt.Sprintf("value_%d_%d", i, j) |
||||
labels = append(labels, &dto.LabelPair{ |
||||
Name: stringPtr(name), |
||||
Value: stringPtr(value), |
||||
}) |
||||
} |
||||
|
||||
value := float64(i) |
||||
metrics = append(metrics, &dto.Metric{ |
||||
Label: labels, |
||||
Gauge: &dto.Gauge{Value: &value}, |
||||
}) |
||||
} |
||||
|
||||
metricType := dto.MetricType_GAUGE |
||||
|
||||
return &dto.MetricFamily{ |
||||
Name: stringPtr("node_benchmark_textfile_metric"), |
||||
Help: stringPtr("Benchmark metric family for textfile collector."), |
||||
Type: &metricType, |
||||
Metric: metrics, |
||||
} |
||||
} |
||||
|
||||
func drainMetrics(ch chan prometheus.Metric) { |
||||
for len(ch) > 0 { |
||||
<-ch |
||||
} |
||||
} |
||||
|
||||
func stringPtr(s string) *string { |
||||
return &s |
||||
} |
||||
Loading…
Reference in new issue