frostfs-sdk-go/eacl/test/benchmark_test.go
Ekaterina Lebedeva 46ee543899
All checks were successful
DCO / DCO (pull_request) Successful in 47s
Tests and linters / Tests (1.22) (pull_request) Successful in 1m7s
Tests and linters / Tests (1.23) (pull_request) Successful in 1m7s
Tests and linters / Lint (pull_request) Successful in 1m42s
[#265] 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 12:37:46 +03:00

116 lines
2.2 KiB
Go

package eacltest
import (
"bytes"
"crypto/rand"
"testing"
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/eacl"
versiontest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/version/test"
"github.com/stretchr/testify/require"
)
func baseBenchmarkTableBinaryComparison(b *testing.B, factor int) {
t := TableN(factor)
exp, err := t.Marshal()
require.NoError(b, err)
b.StopTimer()
b.ResetTimer()
b.StartTimer()
for range b.N {
got, _ := t.Marshal()
if !bytes.Equal(exp, got) {
b.Fail()
}
}
}
func baseBenchmarkTableEqualsComparison(b *testing.B, factor int) {
t := TableN(factor)
data, err := t.Marshal()
require.NoError(b, err)
t2 := eacl.NewTable()
err = t2.Unmarshal(data)
require.NoError(b, err)
b.StopTimer()
b.ResetTimer()
b.StartTimer()
for range b.N {
if !eacl.EqualTables(*t, *t2) {
b.Fail()
}
}
}
func BenchmarkTableBinaryComparison(b *testing.B) {
baseBenchmarkTableBinaryComparison(b, 1)
}
func BenchmarkTableEqualsComparison(b *testing.B) {
baseBenchmarkTableEqualsComparison(b, 1)
}
func BenchmarkTableBinaryComparison10(b *testing.B) {
baseBenchmarkTableBinaryComparison(b, 10)
}
func BenchmarkTableEqualsComparison10(b *testing.B) {
baseBenchmarkTableEqualsComparison(b, 10)
}
func BenchmarkTableBinaryComparison100(b *testing.B) {
baseBenchmarkTableBinaryComparison(b, 100)
}
func BenchmarkTableEqualsComparison100(b *testing.B) {
baseBenchmarkTableEqualsComparison(b, 100)
}
// Target returns random eacl.Target.
func TargetN(n int) *eacl.Target {
x := eacl.NewTarget()
x.SetRole(eacl.RoleSystem)
keys := make([][]byte, n)
for i := range n {
keys[i] = make([]byte, 32)
rand.Read(keys[i])
}
x.SetBinaryKeys(keys)
return x
}
// Record returns random eacl.Record.
func RecordN(n int) *eacl.Record {
x := eacl.NewRecord()
x.SetAction(eacl.ActionAllow)
x.SetOperation(eacl.OperationRangeHash)
x.SetTargets(*TargetN(n))
for range n {
x.AddFilter(eacl.HeaderFromObject, eacl.MatchStringEqual, "", cidtest.ID().EncodeToString())
}
return x
}
func TableN(n int) *eacl.Table {
x := eacl.NewTable()
x.SetCID(cidtest.ID())
for range n {
x.AddRecord(RecordN(n))
}
x.SetVersion(versiontest.Version())
return x
}