2016-12-04 12:16:11 +00:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
2016-12-15 11:15:54 +00:00
|
|
|
"bytes"
|
2016-12-16 11:13:17 +00:00
|
|
|
"crypto/rand"
|
2016-12-04 12:16:11 +00:00
|
|
|
"testing"
|
|
|
|
|
2016-12-21 09:37:38 +00:00
|
|
|
"github.com/fabxc/tsdb/labels"
|
2016-12-04 12:16:11 +00:00
|
|
|
)
|
|
|
|
|
2016-12-21 09:37:38 +00:00
|
|
|
func BenchmarkMapClone(b *testing.B) {
|
|
|
|
m := map[string]string{
|
|
|
|
"job": "node",
|
|
|
|
"instance": "123.123.1.211:9090",
|
|
|
|
"path": "/api/v1/namespaces/<namespace>/deployments/<name>",
|
|
|
|
"method": "GET",
|
|
|
|
"namespace": "system",
|
|
|
|
"status": "500",
|
|
|
|
"prometheus": "prometheus-core-1",
|
|
|
|
"datacenter": "eu-west-1",
|
|
|
|
"pod_name": "abcdef-99999-defee",
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
res := make(map[string]string, len(m))
|
|
|
|
for k, v := range m {
|
|
|
|
res[k] = v
|
|
|
|
}
|
|
|
|
m = res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLabelsClone(b *testing.B) {
|
|
|
|
m := map[string]string{
|
|
|
|
"job": "node",
|
|
|
|
"instance": "123.123.1.211:9090",
|
|
|
|
"path": "/api/v1/namespaces/<namespace>/deployments/<name>",
|
|
|
|
"method": "GET",
|
|
|
|
"namespace": "system",
|
|
|
|
"status": "500",
|
|
|
|
"prometheus": "prometheus-core-1",
|
|
|
|
"datacenter": "eu-west-1",
|
|
|
|
"pod_name": "abcdef-99999-defee",
|
|
|
|
}
|
2016-12-21 14:12:26 +00:00
|
|
|
l := labels.FromMap(m)
|
2016-12-21 09:37:38 +00:00
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
res := make(labels.Labels, len(l))
|
|
|
|
copy(res, l)
|
|
|
|
l = res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-04 12:16:11 +00:00
|
|
|
func BenchmarkLabelMapAccess(b *testing.B) {
|
|
|
|
m := map[string]string{
|
2016-12-21 08:39:01 +00:00
|
|
|
"job": "node",
|
|
|
|
"instance": "123.123.1.211:9090",
|
|
|
|
"path": "/api/v1/namespaces/<namespace>/deployments/<name>",
|
|
|
|
"method": "GET",
|
|
|
|
"namespace": "system",
|
|
|
|
"status": "500",
|
|
|
|
"prometheus": "prometheus-core-1",
|
|
|
|
"datacenter": "eu-west-1",
|
|
|
|
"pod_name": "abcdef-99999-defee",
|
2016-12-04 12:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var v string
|
|
|
|
|
|
|
|
for k := range m {
|
|
|
|
b.Run(k, func(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
v = m[k]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-12-14 20:58:29 +00:00
|
|
|
|
|
|
|
_ = v
|
2016-12-04 12:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLabelSetAccess(b *testing.B) {
|
|
|
|
m := map[string]string{
|
2016-12-21 08:39:01 +00:00
|
|
|
"job": "node",
|
|
|
|
"instance": "123.123.1.211:9090",
|
|
|
|
"path": "/api/v1/namespaces/<namespace>/deployments/<name>",
|
|
|
|
"method": "GET",
|
|
|
|
"namespace": "system",
|
|
|
|
"status": "500",
|
|
|
|
"prometheus": "prometheus-core-1",
|
|
|
|
"datacenter": "eu-west-1",
|
|
|
|
"pod_name": "abcdef-99999-defee",
|
2016-12-04 12:16:11 +00:00
|
|
|
}
|
2016-12-21 14:12:26 +00:00
|
|
|
ls := labels.FromMap(m)
|
2016-12-04 12:16:11 +00:00
|
|
|
|
|
|
|
var v string
|
|
|
|
|
|
|
|
for _, l := range ls {
|
|
|
|
b.Run(l.Name, func(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
v = ls.Get(l.Name)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-12-14 20:58:29 +00:00
|
|
|
|
|
|
|
_ = v
|
2016-12-04 12:16:11 +00:00
|
|
|
}
|
2016-12-15 11:15:54 +00:00
|
|
|
|
|
|
|
func BenchmarkStringBytesEquals(b *testing.B) {
|
2016-12-16 11:13:17 +00:00
|
|
|
randBytes := func(n int) ([]byte, []byte) {
|
|
|
|
buf1 := make([]byte, n)
|
|
|
|
if _, err := rand.Read(buf1); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
buf2 := make([]byte, n)
|
|
|
|
copy(buf1, buf2)
|
|
|
|
|
|
|
|
return buf1, buf2
|
|
|
|
}
|
|
|
|
|
2016-12-15 11:15:54 +00:00
|
|
|
cases := []struct {
|
|
|
|
name string
|
2016-12-16 11:13:17 +00:00
|
|
|
f func() ([]byte, []byte)
|
2016-12-15 11:15:54 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "equal",
|
2016-12-16 11:13:17 +00:00
|
|
|
f: func() ([]byte, []byte) {
|
|
|
|
return randBytes(60)
|
|
|
|
},
|
2016-12-15 11:15:54 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "1-flip-end",
|
2016-12-16 11:13:17 +00:00
|
|
|
f: func() ([]byte, []byte) {
|
|
|
|
b1, b2 := randBytes(60)
|
|
|
|
b2[59] ^= b2[59]
|
|
|
|
return b1, b2
|
|
|
|
},
|
2016-12-15 11:15:54 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "1-flip-middle",
|
2016-12-16 11:13:17 +00:00
|
|
|
f: func() ([]byte, []byte) {
|
|
|
|
b1, b2 := randBytes(60)
|
|
|
|
b2[29] ^= b2[29]
|
|
|
|
return b1, b2
|
|
|
|
},
|
2016-12-15 11:15:54 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "1-flip-start",
|
2016-12-16 11:13:17 +00:00
|
|
|
f: func() ([]byte, []byte) {
|
|
|
|
b1, b2 := randBytes(60)
|
|
|
|
b2[0] ^= b2[0]
|
|
|
|
return b1, b2
|
|
|
|
},
|
2016-12-15 11:15:54 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "different-length",
|
2016-12-16 11:13:17 +00:00
|
|
|
f: func() ([]byte, []byte) {
|
|
|
|
b1, b2 := randBytes(60)
|
|
|
|
return b1, b2[:59]
|
|
|
|
},
|
2016-12-15 11:15:54 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
b.Run(c.name+"-strings", func(b *testing.B) {
|
2016-12-16 11:13:17 +00:00
|
|
|
ab, bb := c.f()
|
|
|
|
as, bs := string(ab), string(bb)
|
2016-12-15 11:15:54 +00:00
|
|
|
b.SetBytes(int64(len(as)))
|
|
|
|
|
|
|
|
var r bool
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
r = as == bs
|
|
|
|
}
|
|
|
|
_ = r
|
|
|
|
})
|
|
|
|
|
|
|
|
b.Run(c.name+"-bytes", func(b *testing.B) {
|
2016-12-16 11:13:17 +00:00
|
|
|
ab, bb := c.f()
|
2016-12-15 11:15:54 +00:00
|
|
|
b.SetBytes(int64(len(ab)))
|
|
|
|
|
|
|
|
var r bool
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
r = bytes.Equal(ab, bb)
|
|
|
|
}
|
|
|
|
_ = r
|
|
|
|
})
|
|
|
|
|
|
|
|
b.Run(c.name+"-bytes-length-check", func(b *testing.B) {
|
2016-12-16 11:13:17 +00:00
|
|
|
ab, bb := c.f()
|
2016-12-15 11:15:54 +00:00
|
|
|
b.SetBytes(int64(len(ab)))
|
|
|
|
|
|
|
|
var r bool
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if len(ab) != len(bb) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
r = bytes.Equal(ab, bb)
|
|
|
|
}
|
|
|
|
_ = r
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|