Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func BenchmarkClient(b *testing.B) {
ctx := context.Background()

for _, sizeKB := range []int{4, 50, 1000, 2000} {
b.Run(fmt.Sprintf("%dKB", sizeKB), func(b *testing.B) {
b.Run(fmt.Sprintf("size=%dKB", sizeKB), func(b *testing.B) {
testServer := httptest.NewServer(serveSpaces{sizeKB})
defer testServer.Close()

Expand All @@ -246,14 +246,12 @@ func BenchmarkClient(b *testing.B) {
req := &http.Request{
URL: url,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
_, _, err := client.Do(ctx, req)
if err != nil {
b.Fatalf("Query failed: %v", err)
}
}
b.StopTimer()
})
}
}
64 changes: 32 additions & 32 deletions api/prometheus/v1/api_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ func generateData(timeseries, datapoints int) (floatMatrix, histogramMatrix mode

func BenchmarkSamplesJsonSerialization(b *testing.B) {
for _, timeseriesCount := range []int{10, 100, 1000} {
b.Run(strconv.Itoa(timeseriesCount), func(b *testing.B) {
b.Run("series="+strconv.Itoa(timeseriesCount), func(b *testing.B) {
for _, datapointCount := range []int{10, 100, 1000} {
b.Run(strconv.Itoa(datapointCount), func(b *testing.B) {
b.Run("dp="+strconv.Itoa(datapointCount), func(b *testing.B) {
floats, histograms := generateData(timeseriesCount, datapointCount)

floatBytes, err := json.Marshal(floats)
Expand All @@ -119,57 +119,57 @@ func BenchmarkSamplesJsonSerialization(b *testing.B) {
b.Fatalf("Error marshaling: %v", err)
}

b.Run("marshal", func(b *testing.B) {
b.Run("floats", func(b *testing.B) {
b.Run("json", func(b *testing.B) {
b.Run("op=marshal", func(b *testing.B) {
b.Run("type=floats", func(b *testing.B) {
b.Run("encoder=json", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
if _, err := json.Marshal(floats); err != nil {
b.Fatal(err)
}
}
})
if supportsJSONv2 {
b.Run("jsonv2", func(b *testing.B) {
b.Run("encoder=jsonv2", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
if _, err := jsonv2Marshal(floats); err != nil {
b.Fatal(err)
}
}
})
}
b.Run("jsoniter", func(b *testing.B) {
b.Run("encoder=jsoniter", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
if _, err := jsoniter.Marshal(floats); err != nil {
b.Fatal(err)
}
}
})
})
b.Run("histograms", func(b *testing.B) {
b.Run("json", func(b *testing.B) {
b.Run("type=histograms", func(b *testing.B) {
b.Run("encoder=json", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
if _, err := json.Marshal(histograms); err != nil {
b.Fatal(err)
}
}
})
if supportsJSONv2 {
b.Run("jsonv2", func(b *testing.B) {
b.Run("encoder=jsonv2", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
if _, err := jsonv2Marshal(histograms); err != nil {
b.Fatal(err)
}
}
})
}
b.Run("jsoniter", func(b *testing.B) {
b.Run("encoder=jsoniter", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
if _, err := jsoniter.Marshal(histograms); err != nil {
b.Fatal(err)
}
Expand All @@ -178,62 +178,62 @@ func BenchmarkSamplesJsonSerialization(b *testing.B) {
})
})

b.Run("unmarshal", func(b *testing.B) {
b.Run("floats", func(b *testing.B) {
b.Run("json", func(b *testing.B) {
b.Run("op=unmarshal", func(b *testing.B) {
b.Run("type=floats", func(b *testing.B) {
b.Run("encoder=json", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
var m model.Matrix
if err := json.Unmarshal(floatBytes, &m); err != nil {
b.Fatal(err)
}
}
})
if supportsJSONv2 {
b.Run("jsonv2", func(b *testing.B) {
b.Run("encoder=jsonv2", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
var m model.Matrix
if err := jsonv2Unmarshal(floatBytes, &m); err != nil {
b.Fatal(err)
}
}
})
}
b.Run("jsoniter", func(b *testing.B) {
b.Run("encoder=jsoniter", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
var m model.Matrix
if err := jsoniter.Unmarshal(floatBytes, &m); err != nil {
b.Fatal(err)
}
}
})
})
b.Run("histograms", func(b *testing.B) {
b.Run("json", func(b *testing.B) {
b.Run("type=histograms", func(b *testing.B) {
b.Run("encoder=json", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
var m model.Matrix
if err := json.Unmarshal(histogramBytes, &m); err != nil {
b.Fatal(err)
}
}
})
if supportsJSONv2 {
b.Run("jsonv2", func(b *testing.B) {
b.Run("encoder=jsonv2", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
var m model.Matrix
if err := jsonv2Unmarshal(histogramBytes, &m); err != nil {
b.Fatal(err)
}
}
})
}
b.Run("jsoniter", func(b *testing.B) {
b.Run("encoder=jsoniter", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
var m model.Matrix
if err := jsoniter.Unmarshal(histogramBytes, &m); err != nil {
b.Fatal(err)
Expand Down
60 changes: 26 additions & 34 deletions prometheus/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,24 @@ func BenchmarkCounter(b *testing.B) {
constraint LabelConstraint
counters fns
}{
{"With Label Values", nil, fns{deLV}},
{"With Label Values and Constraint", twoConstraint, fns{deLV}},
{"With triple Label Values", nil, fns{deLV, frLV, nlLV}},
{"With triple Label Values and Constraint", twoConstraint, fns{deLV, frLV, nlLV}},
{"With repeated Label Values", nil, fns{deLV, deLV}},
{"With repeated Label Values and Constraint", twoConstraint, fns{deLV, deLV}},
{"With Mapped Labels", nil, fns{deML}},
{"With Mapped Labels and Constraint", twoConstraint, fns{deML}},
{"With triple Mapped Labels", nil, fns{deML, frML, nlML}},
{"With triple Mapped Labels and Constraint", twoConstraint, fns{deML, frML, nlML}},
{"With repeated Mapped Labels", nil, fns{deML, deML}},
{"With repeated Mapped Labels and Constraint", twoConstraint, fns{deML, deML}},
{"With Prepared Mapped Labels", nil, fns{dePML}},
{"With Prepared Mapped Labels and Constraint", twoConstraint, fns{dePML}},
{"With triple Prepared Mapped Labels", nil, fns{dePML, frPML, nlPML}},
{"With triple Prepared Mapped Labels and Constraint", twoConstraint, fns{dePML, frPML, nlPML}},
{"With repeated Prepared Mapped Labels", nil, fns{dePML, dePML}},
{"With repeated Prepared Mapped Labels and Constraint", twoConstraint, fns{dePML, dePML}},
{"labels=values,constraint=no", nil, fns{deLV}},
{"labels=values,constraint=yes", twoConstraint, fns{deLV}},
{"labels=values-triple,constraint=no", nil, fns{deLV, frLV, nlLV}},
{"labels=values-triple,constraint=yes", twoConstraint, fns{deLV, frLV, nlLV}},
{"labels=values-repeated,constraint=no", nil, fns{deLV, deLV}},
{"labels=values-repeated,constraint=yes", twoConstraint, fns{deLV, deLV}},
{"labels=mapped,constraint=no", nil, fns{deML}},
{"labels=mapped,constraint=yes", twoConstraint, fns{deML}},
{"labels=mapped-triple,constraint=no", nil, fns{deML, frML, nlML}},
{"labels=mapped-triple,constraint=yes", twoConstraint, fns{deML, frML, nlML}},
{"labels=mapped-repeated,constraint=no", nil, fns{deML, deML}},
{"labels=mapped-repeated,constraint=yes", twoConstraint, fns{deML, deML}},
{"labels=mapped-prepared,constraint=no", nil, fns{dePML}},
{"labels=mapped-prepared,constraint=yes", twoConstraint, fns{dePML}},
{"labels=mapped-prepared-triple,constraint=no", nil, fns{dePML, frPML, nlPML}},
{"labels=mapped-prepared-triple,constraint=yes", twoConstraint, fns{dePML, frPML, nlPML}},
{"labels=mapped-prepared-repeated,constraint=no", nil, fns{dePML, dePML}},
{"labels=mapped-prepared-repeated,constraint=yes", twoConstraint, fns{dePML, dePML}},
}

for _, t := range table {
Expand All @@ -99,8 +99,7 @@ func BenchmarkCounter(b *testing.B) {
},
)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
for _, fn := range t.counters {
fn(m).Inc()
}
Expand Down Expand Up @@ -138,8 +137,7 @@ func BenchmarkCounterNoLabels(b *testing.B) {
Help: "A counter to benchmark it.",
})
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
m.Inc()
}
}
Expand All @@ -153,8 +151,7 @@ func BenchmarkGaugeWithLabelValues(b *testing.B) {
[]string{"one", "two", "three"},
)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
m.WithLabelValues("eins", "zwei", "drei").Set(3.1415)
}
}
Expand All @@ -165,8 +162,7 @@ func BenchmarkGaugeNoLabels(b *testing.B) {
Help: "A gauge to benchmark it.",
})
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
m.Set(3.1415)
}
}
Expand All @@ -181,8 +177,7 @@ func BenchmarkSummaryWithLabelValues(b *testing.B) {
[]string{"one", "two", "three"},
)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
m.WithLabelValues("eins", "zwei", "drei").Observe(3.1415)
}
}
Expand All @@ -195,8 +190,7 @@ func BenchmarkSummaryNoLabels(b *testing.B) {
},
)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
m.Observe(3.1415)
}
}
Expand All @@ -210,8 +204,7 @@ func BenchmarkHistogramWithLabelValues(b *testing.B) {
[]string{"one", "two", "three"},
)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
m.WithLabelValues("eins", "zwei", "drei").Observe(3.1415)
}
}
Expand All @@ -223,8 +216,7 @@ func BenchmarkHistogramNoLabels(b *testing.B) {
},
)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
m.Observe(3.1415)
}
}
Expand Down
3 changes: 1 addition & 2 deletions prometheus/go_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ func TestGoCollectorGC(t *testing.T) {
func BenchmarkGoCollector(b *testing.B) {
c := NewGoCollector().(*goCollector)

b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
ch := make(chan Metric, 8)
go func() {
// Drain all metrics received until the
Expand Down
9 changes: 3 additions & 6 deletions prometheus/histogram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1486,8 +1486,7 @@ func benchmarkFindBucket(b *testing.B, l int) {
}
v := float64(l / 2)

b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
resultFindBucket = h.findBucket(v)
}
}
Expand Down Expand Up @@ -1515,8 +1514,7 @@ func BenchmarkFindBucketInf(b *testing.B) {
}
v := 1000.5

b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
resultFindBucket = h.findBucket(v)
}
}
Expand All @@ -1528,8 +1526,7 @@ func BenchmarkFindBucketLow(b *testing.B) {
}
v := -1.1

b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
resultFindBucket = h.findBucket(v)
}
}
Expand Down
4 changes: 1 addition & 3 deletions prometheus/internal/difflib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,8 @@ func TestSplitLines(t *testing.T) {
func benchmarkSplitLines(b *testing.B, count int) {
str := strings.Repeat("foo\n", count)

b.ResetTimer()

n := 0
for i := 0; i < b.N; i++ {
for b.Loop() {
n += len(SplitLines(str))
}
}
Expand Down
4 changes: 2 additions & 2 deletions prometheus/promhttp/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,8 +760,8 @@ func BenchmarkCompression(b *testing.B) {
}

for _, benchmark := range benchmarks {
b.Run(benchmark.name+"_"+size.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
b.Run("op="+benchmark.name+",size="+size.name, func(b *testing.B) {
for b.Loop() {
writer := httptest.NewRecorder()
request, _ := http.NewRequest(http.MethodGet, "/", nil)
request.Header.Add(acceptEncodingHeader, benchmark.compressionType)
Expand Down
2 changes: 1 addition & 1 deletion prometheus/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ func TestHandler(t *testing.T) {
}

func BenchmarkHandler(b *testing.B) {
for i := 0; i < b.N; i++ {
for b.Loop() {
testHandler(b)
}
}
Expand Down
Loading
Loading