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. 13package v1 14 15import ( 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 26func generateData(timeseries, datapoints int) model.Matrix { 27 m := make(model.Matrix, 0) 28 29 for i := 0; i < timeseries; i++ { 30 lset := map[model.LabelName]model.LabelValue{ 31 model.MetricNameLabel: model.LabelValue("timeseries_" + strconv.Itoa(i)), 32 } 33 now := model.Now() 34 values := make([]model.SamplePair, datapoints) 35 36 for x := datapoints; x > 0; x-- { 37 values[x-1] = model.SamplePair{ 38 // Set the time back assuming a 15s interval. Since this is used for 39 // Marshal/Unmarshal testing the actual interval doesn't matter. 40 Timestamp: now.Add(time.Second * -15 * time.Duration(x)), 41 Value: model.SampleValue(float64(x)), 42 } 43 } 44 45 ss := &model.SampleStream{ 46 Metric: model.Metric(lset), 47 Values: values, 48 } 49 50 m = append(m, ss) 51 } 52 return m 53} 54 55func BenchmarkSamplesJsonSerialization(b *testing.B) { 56 for _, timeseriesCount := range []int{10, 100, 1000} { 57 b.Run(strconv.Itoa(timeseriesCount), func(b *testing.B) { 58 for _, datapointCount := range []int{10, 100, 1000} { 59 b.Run(strconv.Itoa(datapointCount), func(b *testing.B) { 60 data := generateData(timeseriesCount, datapointCount) 61 62 dataBytes, err := json.Marshal(data) 63 if err != nil { 64 b.Fatalf("Error marshaling: %v", err) 65 } 66 67 b.Run("marshal", func(b *testing.B) { 68 b.Run("encoding/json", func(b *testing.B) { 69 b.ReportAllocs() 70 for i := 0; i < b.N; i++ { 71 if _, err := json.Marshal(data); err != nil { 72 b.Fatal(err) 73 } 74 } 75 }) 76 77 b.Run("jsoniter", func(b *testing.B) { 78 b.ReportAllocs() 79 for i := 0; i < b.N; i++ { 80 if _, err := jsoniter.Marshal(data); err != nil { 81 b.Fatal(err) 82 } 83 } 84 }) 85 }) 86 87 b.Run("unmarshal", func(b *testing.B) { 88 b.Run("encoding/json", func(b *testing.B) { 89 b.ReportAllocs() 90 var m model.Matrix 91 for i := 0; i < b.N; i++ { 92 if err := json.Unmarshal(dataBytes, &m); err != nil { 93 b.Fatal(err) 94 } 95 } 96 }) 97 98 b.Run("jsoniter", func(b *testing.B) { 99 b.ReportAllocs() 100 var m model.Matrix 101 for i := 0; i < b.N; i++ { 102 if err := jsoniter.Unmarshal(dataBytes, &m); err != nil { 103 b.Fatal(err) 104 } 105 } 106 }) 107 }) 108 }) 109 } 110 }) 111 } 112} 113