Debian Codegraph @

api/prometheus/v1/api_bench_test.go

raw
1 // Copyright 2019 The Prometheus Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 package v1
14
15 import (
16 "encoding/json"
17 "strconv"
18 "testing"
19 "time"
20
21 jsoniter "github.com/json-iterator/go"
22
23 "github.com/prometheus/common/model"
24 )
25
26 func generateData(timeseries, datapoints int) (floatMatrix, histogramMatrix model.Matrix) {
27 for i := 0; i < timeseries; i++ {
28 lset := map[model.LabelName]model.LabelValue{
29 model.MetricNameLabel: model.LabelValue("timeseries_" + strconv.Itoa(i)),
30 "foo": "bar",
31 }
32 now := model.Time(1677587274055)
33 floats := make([]model.SamplePair, datapoints)
34 histograms := make([]model.SampleHistogramPair, datapoints)
35
36 for x := datapoints; x > 0; x-- {
37 f := float64(x)
38 floats[x-1] = model.SamplePair{
39 // Set the time back assuming a 15s interval. Since this is used for
40 // Marshal/Unmarshal testing the actual interval doesn't matter.
41 Timestamp: now.Add(time.Second * -15 * time.Duration(x)),
42 Value: model.SampleValue(f),
43 }
44 histograms[x-1] = model.SampleHistogramPair{
45 Timestamp: now.Add(time.Second * -15 * time.Duration(x)),
46 Histogram: &model.SampleHistogram{
47 Count: model.FloatString(13.5 * f),
48 Sum: model.FloatString(.1 * f),
49 Buckets: model.HistogramBuckets{
50 {
51 Boundaries: 1,
52 Lower: -4870.992343051145,
53 Upper: -4466.7196729968955,
54 Count: model.FloatString(1 * f),
55 },
56 {
57 Boundaries: 1,
58 Lower: -861.0779292198035,
59 Upper: -789.6119426088657,
60 Count: model.FloatString(2 * f),
61 },
62 {
63 Boundaries: 1,
64 Lower: -558.3399591246119,
65 Upper: -512,
66 Count: model.FloatString(3 * f),
67 },
68 {
69 Boundaries: 0,
70 Lower: 2048,
71 Upper: 2233.3598364984477,
72 Count: model.FloatString(1.5 * f),
73 },
74 {
75 Boundaries: 0,
76 Lower: 2896.3093757400984,
77 Upper: 3158.4477704354626,
78 Count: model.FloatString(2.5 * f),
79 },
80 {
81 Boundaries: 0,
82 Lower: 4466.7196729968955,
83 Upper: 4870.992343051145,
84 Count: model.FloatString(3.5 * f),
85 },
86 },
87 },
88 }
89 }
90
91 fss := &model.SampleStream{
92 Metric: model.Metric(lset),
93 Values: floats,
94 }
95 hss := &model.SampleStream{
96 Metric: model.Metric(lset),
97 Histograms: histograms,
98 }
99
100 floatMatrix = append(floatMatrix, fss)
101 histogramMatrix = append(histogramMatrix, hss)
102 }
103 return
104 }
105
106 func BenchmarkSamplesJsonSerialization(b *testing.B) {
107 for _, timeseriesCount := range []int{10, 100, 1000} {
108 b.Run(strconv.Itoa(timeseriesCount), func(b *testing.B) {
109 for _, datapointCount := range []int{10, 100, 1000} {
110 b.Run(strconv.Itoa(datapointCount), func(b *testing.B) {
111 floats, histograms := generateData(timeseriesCount, datapointCount)
112
113 floatBytes, err := json.Marshal(floats)
114 if err != nil {
115 b.Fatalf("Error marshaling: %v", err)
116 }
117 histogramBytes, err := json.Marshal(histograms)
118 if err != nil {
119 b.Fatalf("Error marshaling: %v", err)
120 }
121
122 b.Run("marshal", func(b *testing.B) {
123 b.Run("encoding/json/floats", func(b *testing.B) {
124 b.ReportAllocs()
125 for i := 0; i < b.N; i++ {
126 if _, err := json.Marshal(floats); err != nil {
127 b.Fatal(err)
128 }
129 }
130 })
131 b.Run("jsoniter/floats", func(b *testing.B) {
132 b.ReportAllocs()
133 for i := 0; i < b.N; i++ {
134 if _, err := jsoniter.Marshal(floats); err != nil {
135 b.Fatal(err)
136 }
137 }
138 })
139 b.Run("encoding/json/histograms", func(b *testing.B) {
140 b.ReportAllocs()
141 for i := 0; i < b.N; i++ {
142 if _, err := json.Marshal(histograms); err != nil {
143 b.Fatal(err)
144 }
145 }
146 })
147 b.Run("jsoniter/histograms", func(b *testing.B) {
148 b.ReportAllocs()
149 for i := 0; i < b.N; i++ {
150 if _, err := jsoniter.Marshal(histograms); err != nil {
151 b.Fatal(err)
152 }
153 }
154 })
155 })
156
157 b.Run("unmarshal", func(b *testing.B) {
158 b.Run("encoding/json/floats", func(b *testing.B) {
159 b.ReportAllocs()
160 var m model.Matrix
161 for i := 0; i < b.N; i++ {
162 if err := json.Unmarshal(floatBytes, &m); err != nil {
163 b.Fatal(err)
164 }
165 }
166 })
167 b.Run("jsoniter/floats", func(b *testing.B) {
168 b.ReportAllocs()
169 var m model.Matrix
170 for i := 0; i < b.N; i++ {
171 if err := jsoniter.Unmarshal(floatBytes, &m); err != nil {
172 b.Fatal(err)
173 }
174 }
175 })
176 b.Run("encoding/json/histograms", func(b *testing.B) {
177 b.ReportAllocs()
178 var m model.Matrix
179 for i := 0; i < b.N; i++ {
180 if err := json.Unmarshal(histogramBytes, &m); err != nil {
181 b.Fatal(err)
182 }
183 }
184 })
185 b.Run("jsoniter/histograms", func(b *testing.B) {
186 b.ReportAllocs()
187 var m model.Matrix
188 for i := 0; i < b.N; i++ {
189 if err := jsoniter.Unmarshal(histogramBytes, &m); err != nil {
190 b.Fatal(err)
191 }
192 }
193 })
194 })
195 })
196 }
197 })
198 }
199 }