mirror of
https://github.com/prometheus/prometheus
synced 2024-12-24 23:42:32 +00:00
9981b3f3ee
* labels: Reduce allocated memory by Hash method in edge cases; Added tests. Old: /tmp/___BenchmarkLabels_Hash_in_github_com_prometheus_prometheus_pkg_labels -test.v -test.bench ^\QBenchmarkLabels_Hash\E$ -test.run ^$ goos: linux goarch: amd64 pkg: github.com/prometheus/prometheus/pkg/labels BenchmarkLabels_Hash BenchmarkLabels_Hash/typical_labels_under_1KB BenchmarkLabels_Hash/typical_labels_under_1KB-12 5366161 259 ns/op 0 B/op 0 allocs/op BenchmarkLabels_Hash/bigger_labels_over_1KB BenchmarkLabels_Hash/bigger_labels_over_1KB-12 1700371 767 ns/op 2048 B/op 1 allocs/op BenchmarkLabels_Hash/extremely_large_label_value_10MB BenchmarkLabels_Hash/extremely_large_label_value_10MB-12 356 3743115 ns/op 10523442 B/op 1 allocs/op PASS New: /tmp/___BenchmarkLabels_Hash_in_github_com_prometheus_prometheus_pkg_labels -test.v -test.bench ^\QBenchmarkLabels_Hash\E$ -test.run ^$ goos: linux goarch: amd64 pkg: github.com/prometheus/prometheus/pkg/labels BenchmarkLabels_Hash BenchmarkLabels_Hash/typical_labels_under_1KB BenchmarkLabels_Hash/typical_labels_under_1KB-12 4758883 259 ns/op 0 B/op 0 allocs/op BenchmarkLabels_Hash/bigger_labels_over_1KB BenchmarkLabels_Hash/bigger_labels_over_1KB-12 3324492 357 ns/op 80 B/op 1 allocs/op BenchmarkLabels_Hash/extremely_large_label_value_10MB BenchmarkLabels_Hash/extremely_large_label_value_10MB-12 1087 1083949 ns/op 9734 B/op 1 allocs/op PASS Process finished with exit code 0 Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com> * Addressed Kemal's comment. Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com> * Moved to v2 xxhash for improvements. New: /tmp/___BenchmarkLabels_Hash_in_github_com_prometheus_prometheus_pkg_labels -test.v -test.bench ^\QBenchmarkLabels_Hash\E$ -test.run ^$ -test.benchtime 10s goos: linux goarch: amd64 pkg: github.com/prometheus/prometheus/pkg/labels BenchmarkLabels_Hash BenchmarkLabels_Hash/typical_labels_under_1KB BenchmarkLabels_Hash/typical_labels_under_1KB-12 53447894 221 ns/op 0 B/op 0 allocs/op BenchmarkLabels_Hash/bigger_labels_over_1KB BenchmarkLabels_Hash/bigger_labels_over_1KB-12 42341754 326 ns/op 0 B/op 0 allocs/op BenchmarkLabels_Hash/extremely_large_label_value_10MB BenchmarkLabels_Hash/extremely_large_label_value_10MB-12 10000 1248546 ns/op 0 B/op 0 allocs/op PASS Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com> * Removed old xxhash package. Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com> * Pined minor version. Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
125 lines
2.6 KiB
Go
125 lines
2.6 KiB
Go
// 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 test
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"fmt"
|
|
"hash/crc32"
|
|
"testing"
|
|
|
|
"github.com/cespare/xxhash/v2"
|
|
sip13 "github.com/dgryski/go-sip13"
|
|
)
|
|
|
|
type pair struct {
|
|
name, value string
|
|
}
|
|
|
|
var testInput = []pair{
|
|
{"job", "node"},
|
|
{"instance", "123.123.1.211:9090"},
|
|
{"path", "/api/v1/namespaces/<namespace>/deployments/<name>"},
|
|
{"method", "GET"},
|
|
{"namespace", "system"},
|
|
{"status", "500"},
|
|
}
|
|
|
|
func BenchmarkHash(b *testing.B) {
|
|
input := []byte{}
|
|
for _, v := range testInput {
|
|
input = append(input, v.name...)
|
|
input = append(input, '\xff')
|
|
input = append(input, v.value...)
|
|
input = append(input, '\xff')
|
|
}
|
|
|
|
var total uint64
|
|
|
|
var k0 uint64 = 0x0706050403020100
|
|
var k1 uint64 = 0x0f0e0d0c0b0a0908
|
|
|
|
for name, f := range map[string]func(b []byte) uint64{
|
|
"xxhash": xxhash.Sum64,
|
|
"fnv64": fnv64a,
|
|
"sip13": func(b []byte) uint64 { return sip13.Sum64(k0, k1, b) },
|
|
} {
|
|
b.Run(name, func(b *testing.B) {
|
|
b.SetBytes(int64(len(input)))
|
|
total = 0
|
|
for i := 0; i < b.N; i++ {
|
|
total += f(input)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// hashAdd adds a string to a fnv64a hash value, returning the updated hash.
|
|
func fnv64a(b []byte) uint64 {
|
|
const (
|
|
offset64 = 14695981039346656037
|
|
prime64 = 1099511628211
|
|
)
|
|
|
|
h := uint64(offset64)
|
|
for x := range b {
|
|
h ^= uint64(x)
|
|
h *= prime64
|
|
}
|
|
return h
|
|
}
|
|
|
|
func BenchmarkCRC32_diff(b *testing.B) {
|
|
|
|
data := [][]byte{}
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
b := make([]byte, 512)
|
|
rand.Read(b)
|
|
data = append(data, b)
|
|
}
|
|
|
|
ctab := crc32.MakeTable(crc32.Castagnoli)
|
|
total := uint32(0)
|
|
|
|
b.Run("direct", func(b *testing.B) {
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
total += crc32.Checksum(data[i%1000], ctab)
|
|
}
|
|
})
|
|
b.Run("hash-reuse", func(b *testing.B) {
|
|
b.ReportAllocs()
|
|
h := crc32.New(ctab)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
h.Reset()
|
|
h.Write(data[i%1000])
|
|
total += h.Sum32()
|
|
}
|
|
})
|
|
b.Run("hash-new", func(b *testing.B) {
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
h := crc32.New(ctab)
|
|
h.Write(data[i%1000])
|
|
total += h.Sum32()
|
|
}
|
|
})
|
|
|
|
fmt.Println(total)
|
|
}
|