2013-05-20 10:10:26 -07:00
|
|
|
// Copyright 2013 Prometheus Team
|
|
|
|
// 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 metric
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2013-06-25 05:02:27 -07:00
|
|
|
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
2013-05-20 10:10:26 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkStreamAdd(b *testing.B) {
|
|
|
|
b.StopTimer()
|
2013-06-25 05:02:27 -07:00
|
|
|
s := newStream(clientmodel.Metric{})
|
2013-05-20 10:10:26 -07:00
|
|
|
times := make([]time.Time, 0, b.N)
|
2013-06-25 05:02:27 -07:00
|
|
|
samples := make([]clientmodel.SampleValue, 0, b.N)
|
2013-05-20 10:10:26 -07:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
times = append(times, time.Date(i, 0, 0, 0, 0, 0, 0, time.UTC))
|
2013-06-25 05:02:27 -07:00
|
|
|
samples = append(samples, clientmodel.SampleValue(i))
|
2013-05-20 10:10:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
var pre runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&pre)
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
s.add(times[i], samples[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
var post runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&post)
|
|
|
|
|
|
|
|
b.Logf("%d cycles with %f bytes per cycle, totalling %d", b.N, float32(post.TotalAlloc-pre.TotalAlloc)/float32(b.N), post.TotalAlloc-pre.TotalAlloc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkAppendSample(b *testing.B, labels int) {
|
|
|
|
b.StopTimer()
|
2013-06-06 09:16:22 -07:00
|
|
|
s := NewMemorySeriesStorage(MemorySeriesOptions{})
|
2013-05-20 10:10:26 -07:00
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
metric := clientmodel.Metric{}
|
2013-05-20 10:10:26 -07:00
|
|
|
|
|
|
|
for i := 0; i < labels; i++ {
|
2013-06-25 05:02:27 -07:00
|
|
|
metric[clientmodel.LabelName(fmt.Sprintf("label_%d", i))] = clientmodel.LabelValue(fmt.Sprintf("value_%d", i))
|
2013-05-20 10:10:26 -07:00
|
|
|
}
|
2013-06-25 05:02:27 -07:00
|
|
|
samples := make(clientmodel.Samples, 0, b.N)
|
2013-05-20 10:10:26 -07:00
|
|
|
for i := 0; i < b.N; i++ {
|
2013-06-25 05:02:27 -07:00
|
|
|
samples = append(samples, &clientmodel.Sample{
|
2013-05-20 10:10:26 -07:00
|
|
|
Metric: metric,
|
2013-06-25 05:02:27 -07:00
|
|
|
Value: clientmodel.SampleValue(i),
|
2013-05-20 10:10:26 -07:00
|
|
|
Timestamp: time.Date(i, 0, 0, 0, 0, 0, 0, time.UTC),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
b.StartTimer()
|
|
|
|
var pre runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&pre)
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
s.AppendSample(samples[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
var post runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&post)
|
|
|
|
|
|
|
|
b.Logf("%d cycles with %f bytes per cycle, totalling %d", b.N, float32(post.TotalAlloc-pre.TotalAlloc)/float32(b.N), post.TotalAlloc-pre.TotalAlloc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAppendSample1(b *testing.B) {
|
|
|
|
benchmarkAppendSample(b, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAppendSample10(b *testing.B) {
|
|
|
|
benchmarkAppendSample(b, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAppendSample100(b *testing.B) {
|
|
|
|
benchmarkAppendSample(b, 100)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAppendSample1000(b *testing.B) {
|
|
|
|
benchmarkAppendSample(b, 1000)
|
|
|
|
}
|