diff --git a/api/prometheus/v1/api_bench_test.go b/api/prometheus/v1/api_bench_test.go index 97f502e56..6450600e6 100644 --- a/api/prometheus/v1/api_bench_test.go +++ b/api/prometheus/v1/api_bench_test.go @@ -120,76 +120,126 @@ func BenchmarkSamplesJsonSerialization(b *testing.B) { } b.Run("marshal", func(b *testing.B) { - b.Run("encoding/json/floats", func(b *testing.B) { - b.ReportAllocs() - for i := 0; i < b.N; i++ { - if _, err := json.Marshal(floats); err != nil { - b.Fatal(err) + b.Run("floats", func(b *testing.B) { + b.Run("json", func(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + if _, err := json.Marshal(floats); err != nil { + b.Fatal(err) + } } + }) + if supportsJSONv2 { + b.Run("jsonv2", func(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + if _, err := jsonv2Marshal(floats); err != nil { + b.Fatal(err) + } + } + }) } - }) - b.Run("jsoniter/floats", func(b *testing.B) { - b.ReportAllocs() - for i := 0; i < b.N; i++ { - if _, err := jsoniter.Marshal(floats); err != nil { - b.Fatal(err) + b.Run("jsoniter", func(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + if _, err := jsoniter.Marshal(floats); err != nil { + b.Fatal(err) + } } - } + }) }) - b.Run("encoding/json/histograms", func(b *testing.B) { - b.ReportAllocs() - for i := 0; i < b.N; i++ { - if _, err := json.Marshal(histograms); err != nil { - b.Fatal(err) + b.Run("histograms", func(b *testing.B) { + b.Run("json", func(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + if _, err := json.Marshal(histograms); err != nil { + b.Fatal(err) + } } + }) + if supportsJSONv2 { + b.Run("jsonv2", func(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + if _, err := jsonv2Marshal(histograms); err != nil { + b.Fatal(err) + } + } + }) } - }) - b.Run("jsoniter/histograms", func(b *testing.B) { - b.ReportAllocs() - for i := 0; i < b.N; i++ { - if _, err := jsoniter.Marshal(histograms); err != nil { - b.Fatal(err) + b.Run("jsoniter", func(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + if _, err := jsoniter.Marshal(histograms); err != nil { + b.Fatal(err) + } } - } + }) }) }) b.Run("unmarshal", func(b *testing.B) { - b.Run("encoding/json/floats", func(b *testing.B) { - b.ReportAllocs() - var m model.Matrix - for i := 0; i < b.N; i++ { - if err := json.Unmarshal(floatBytes, &m); err != nil { - b.Fatal(err) + b.Run("floats", func(b *testing.B) { + b.Run("json", func(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + 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.ReportAllocs() + for i := 0; i < b.N; i++ { + var m model.Matrix + if err := jsonv2Unmarshal(floatBytes, &m); err != nil { + b.Fatal(err) + } + } + }) } - }) - b.Run("jsoniter/floats", func(b *testing.B) { - b.ReportAllocs() - var m model.Matrix - for i := 0; i < b.N; i++ { - if err := jsoniter.Unmarshal(floatBytes, &m); err != nil { - b.Fatal(err) + b.Run("jsoniter", func(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + var m model.Matrix + if err := jsoniter.Unmarshal(floatBytes, &m); err != nil { + b.Fatal(err) + } } - } + }) }) - b.Run("encoding/json/histograms", func(b *testing.B) { - b.ReportAllocs() - var m model.Matrix - for i := 0; i < b.N; i++ { - if err := json.Unmarshal(histogramBytes, &m); err != nil { - b.Fatal(err) + b.Run("histograms", func(b *testing.B) { + b.Run("json", func(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + 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.ReportAllocs() + for i := 0; i < b.N; i++ { + var m model.Matrix + if err := jsonv2Unmarshal(histogramBytes, &m); err != nil { + b.Fatal(err) + } + } + }) } - }) - b.Run("jsoniter/histograms", func(b *testing.B) { - b.ReportAllocs() - var m model.Matrix - for i := 0; i < b.N; i++ { - if err := jsoniter.Unmarshal(histogramBytes, &m); err != nil { - b.Fatal(err) + b.Run("jsoniter", func(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + var m model.Matrix + if err := jsoniter.Unmarshal(histogramBytes, &m); err != nil { + b.Fatal(err) + } } - } + }) }) }) }) diff --git a/api/prometheus/v1/api_bench_test_127.go b/api/prometheus/v1/api_bench_test_127.go new file mode 100644 index 000000000..3add4d36c --- /dev/null +++ b/api/prometheus/v1/api_bench_test_127.go @@ -0,0 +1,26 @@ +//go:build go1.27 + +// Copyright 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 v1 + +import ( + jsonv2 "encoding/json/v2" +) + +const supportsJSONv2 = true + +var jsonv2Marshal = jsonv2.Marshal + +var jsonv2Unmarshal = jsonv2.Unmarshal diff --git a/api/prometheus/v1/api_bench_test_pre127.go b/api/prometheus/v1/api_bench_test_pre127.go new file mode 100644 index 000000000..88c552c26 --- /dev/null +++ b/api/prometheus/v1/api_bench_test_pre127.go @@ -0,0 +1,28 @@ +//go:build !go1.27 + +// Copyright 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 v1 + +import "errors" + +const supportsJSONv2 = false + +func jsonv2Marshal(v any) ([]byte, error) { + return nil, errors.New("unsupported") +} + +func jsonv2Unmarshal(data []byte, v any) error { + return errors.New("unsupported") +}