Skip to content

Commit

Permalink
Add benchmark to unmarshal
Browse files Browse the repository at this point in the history
```
goos: linux
goarch: amd64
pkg: github.com/prometheus/client_golang/api/prometheus/v1
BenchmarkSerialization/10/10/unmarshal/encoding/json-4         	  200000	     10272 ns/op	     232 B/op	       5 allocs/op
BenchmarkSerialization/10/10/unmarshal/jsoniter-4              	10000000	       154 ns/op	      16 B/op	       1 allocs/op
BenchmarkSerialization/10/100/unmarshal/encoding/json-4        	   20000	     87834 ns/op	     232 B/op	       5 allocs/op
BenchmarkSerialization/10/100/unmarshal/jsoniter-4             	10000000	       178 ns/op	      16 B/op	       1 allocs/op
BenchmarkSerialization/10/1000/unmarshal/encoding/json-4       	    2000	    966855 ns/op	     232 B/op	       5 allocs/op
BenchmarkSerialization/10/1000/unmarshal/jsoniter-4            	 5000000	       211 ns/op	      16 B/op	       1 allocs/op
BenchmarkSerialization/100/10/unmarshal/encoding/json-4        	   10000	    124487 ns/op	     232 B/op	       5 allocs/op
BenchmarkSerialization/100/10/unmarshal/jsoniter-4             	10000000	       217 ns/op	      16 B/op	       1 allocs/op
BenchmarkSerialization/100/100/unmarshal/encoding/json-4       	    1000	   1335330 ns/op	     232 B/op	       5 allocs/op
BenchmarkSerialization/100/100/unmarshal/jsoniter-4            	10000000	       184 ns/op	      16 B/op	       1 allocs/op
BenchmarkSerialization/100/1000/unmarshal/encoding/json-4      	     100	  11345940 ns/op	     232 B/op	       5 allocs/op
BenchmarkSerialization/100/1000/unmarshal/jsoniter-4           	 5000000	       219 ns/op	      16 B/op	       1 allocs/op
BenchmarkSerialization/1000/10/unmarshal/encoding/json-4       	    1000	   1093244 ns/op	     232 B/op	       5 allocs/op
BenchmarkSerialization/1000/10/unmarshal/jsoniter-4            	10000000	       170 ns/op	      16 B/op	       1 allocs/op
BenchmarkSerialization/1000/100/unmarshal/encoding/json-4      	     200	   9597895 ns/op	     232 B/op	       5 allocs/op
BenchmarkSerialization/1000/100/unmarshal/jsoniter-4           	10000000	       180 ns/op	      16 B/op	       1 allocs/op
BenchmarkSerialization/1000/1000/unmarshal/encoding/json-4     	      10	 104403258 ns/op	     232 B/op	       5 allocs/op
BenchmarkSerialization/1000/1000/unmarshal/jsoniter-4          	10000000	       176 ns/op	      16 B/op	       1 allocs/op
PASS
ok  	github.com/prometheus/client_golang/api/prometheus/v1	35.976s
```

Signed-off-by: Thomas Jackson <jacksontj.89@gmail.com>
  • Loading branch information
jacksontj committed May 8, 2019
1 parent ef34012 commit ae49a4a
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions api/prometheus/v1/api_bench_test.go
@@ -0,0 +1,82 @@
// Copyright 2017 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 (
"encoding/json"
"fmt"
"strconv"
"testing"
"time"

jsoniter "github.com/json-iterator/go"
"github.com/prometheus/common/model"
)

func generateData(timeseries, datapoints int) model.Matrix {
// Create the top-level matrix
m := make(model.Matrix, 0)

for i := 0; i < timeseries; i++ {
lset := map[model.LabelName]model.LabelValue{
model.MetricNameLabel: model.LabelValue("timeseries_" + strconv.Itoa(i)),
}
now := model.Now()
values := make([]model.SamplePair, datapoints)

for x := datapoints; x > 0; x-- {
values[x-1] = model.SamplePair{
Timestamp: now.Add(time.Second * -15 * time.Duration(x)), // Set the time back assuming a 15s interval
Value: model.SampleValue(float64(x)),
}
}

ss := &model.SampleStream{
Metric: model.Metric(lset),
Values: values,
}

m = append(m, ss)
}
return m
}

func BenchmarkSerialization(b *testing.B) {
for _, timeseriesCount := range []int{10, 100, 1000} {
for _, datapointCount := range []int{10, 100, 1000} {
b.Run(fmt.Sprintf("%d/%d", timeseriesCount, datapointCount), func(b *testing.B) {
data := generateData(timeseriesCount, datapointCount)

dataBytes, err := json.Marshal(data)
if err != nil {
b.Fatalf("Error marshaling: %v", err)
}

b.Run("unmarshal", func(b *testing.B) {
var m model.Matrix
b.Run("encoding/json", func(b *testing.B) {
for i := 0; i < b.N; i++ {
json.Unmarshal(dataBytes, m)
}
})

b.Run("jsoniter", func(b *testing.B) {
for i := 0; i < b.N; i++ {
jsoniter.Unmarshal(dataBytes, m)
}
})
})
})
}
}
}

0 comments on commit ae49a4a

Please sign in to comment.