2022-02-28 11:45:35 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
2022-02-28 12:06:51 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-02-28 11:45:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func randString(n int) string {
|
|
|
|
x := make([]byte, n)
|
|
|
|
for i := range x {
|
|
|
|
x[i] = byte('a' + rand.Intn('z'-'a'))
|
|
|
|
}
|
|
|
|
return string(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAttributesMarshal(b *testing.B) {
|
2022-02-28 12:06:51 +00:00
|
|
|
attrs := make([]Attribute, 50)
|
2022-02-28 11:45:35 +00:00
|
|
|
for i := range attrs {
|
|
|
|
attrs[i].key = SysAttributePrefix + randString(10)
|
|
|
|
attrs[i].val = randString(10)
|
|
|
|
}
|
|
|
|
raw := AttributesToGRPC(attrs)
|
2022-02-28 12:06:51 +00:00
|
|
|
require.Equal(b, len(raw), len(attrs))
|
2022-02-28 11:45:35 +00:00
|
|
|
|
|
|
|
b.Run("marshal", func(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
res := AttributesToGRPC(attrs)
|
|
|
|
if len(res) != len(raw) {
|
|
|
|
b.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
b.Run("unmarshal", func(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
res, err := AttributesFromGRPC(raw)
|
|
|
|
if err != nil || len(res) != len(raw) {
|
|
|
|
b.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|