frostfs-api-go/object/bench_test.go
Ekaterina Lebedeva bd588fa2e5 [#113] go.mod: Use range over int
Since Go 1.22 a `for` statement with a `range` clause is able
to iterate through integer values from zero to an upper limit.

gopatch script:
@@
var i, e expression
@@
-for i := 0; i <= e - 1; i++ {
+for i := range e {
    ...
}

@@
var i, e expression
@@
-for i := 0; i <= e; i++ {
+for i := range e + 1 {
    ...
}

@@
var i, e expression
@@
-for i := 0; i < e; i++ {
+for i := range e {
    ...
}

Signed-off-by: Ekaterina Lebedeva <ekaterina.lebedeva@yadro.com>
2024-09-04 15:44:59 +03:00

45 lines
854 B
Go

package object
import (
"math/rand"
"testing"
"github.com/stretchr/testify/require"
)
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) {
attrs := make([]Attribute, 50)
for i := range attrs {
attrs[i].key = SysAttributePrefix + randString(10)
attrs[i].val = randString(10)
}
raw := AttributesToGRPC(attrs)
require.Equal(b, len(raw), len(attrs))
b.Run("marshal", func(b *testing.B) {
b.ReportAllocs()
for range b.N {
res := AttributesToGRPC(attrs)
if len(res) != len(raw) {
b.FailNow()
}
}
})
b.Run("unmarshal", func(b *testing.B) {
b.ReportAllocs()
for range b.N {
res, err := AttributesFromGRPC(raw)
if err != nil || len(res) != len(raw) {
b.FailNow()
}
}
})
}