2023-02-23 14:13:27 +00:00
|
|
|
package transformer
|
|
|
|
|
|
|
|
import (
|
2023-04-21 10:04:44 +00:00
|
|
|
"context"
|
2023-02-23 14:13:27 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-16 06:51:03 +00:00
|
|
|
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
|
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/version"
|
2023-02-23 14:13:27 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestChannelTarget(t *testing.T) {
|
|
|
|
const maxSize = 100
|
|
|
|
|
|
|
|
ch := make(chan *objectSDK.Object, 10)
|
|
|
|
tt := new(testTarget)
|
2023-05-03 08:18:08 +00:00
|
|
|
ct := NewChannelTarget(ch)
|
2023-02-23 14:13:27 +00:00
|
|
|
|
[#188] transformer: Allow to provide size hint
For big objects with known size we can optimize allocation patterns
by providing size hint. As with any hint, it does not affect transformer
functionality: slices with capacity > MaxSize are never allocated.
```
goos: linux
goarch: amd64
pkg: git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/transformer
cpu: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
│ out │
│ sec/op │
Transformer/small/no_size_hint-8 65.44µ ± 3%
Transformer/small/no_size_hint,_with_buffer-8 64.24µ ± 5%
Transformer/small/with_size_hint,_with_buffer-8 58.70µ ± 5%
Transformer/big/no_size_hint-8 367.8m ± 3%
Transformer/big/no_size_hint,_with_buffer-8 562.7m ± 0%
Transformer/big/with_size_hint,_with_buffer-8 385.6m ± 7%
geomean 5.197m
│ out │
│ B/op │
Transformer/small/no_size_hint-8 13.40Ki ± 0%
Transformer/small/no_size_hint,_with_buffer-8 13.40Ki ± 0%
Transformer/small/with_size_hint,_with_buffer-8 13.39Ki ± 0%
Transformer/big/no_size_hint-8 288.0Mi ± 0%
Transformer/big/no_size_hint,_with_buffer-8 1.390Gi ± 0%
Transformer/big/with_size_hint,_with_buffer-8 288.0Mi ± 0%
geomean 2.533Mi
│ out │
│ allocs/op │
Transformer/small/no_size_hint-8 92.00 ± 0%
Transformer/small/no_size_hint,_with_buffer-8 92.00 ± 0%
Transformer/small/with_size_hint,_with_buffer-8 92.00 ± 0%
Transformer/big/no_size_hint-8 546.5 ± 0%
Transformer/big/no_size_hint,_with_buffer-8 607.5 ± 0%
Transformer/big/with_size_hint,_with_buffer-8 545.5 ± 0%
geomean 228.1
```
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
2023-10-26 15:13:54 +00:00
|
|
|
chTarget, _ := newPayloadSizeLimiter(maxSize, 0, func() ObjectWriter { return ct })
|
|
|
|
testTarget, _ := newPayloadSizeLimiter(maxSize, 0, func() ObjectWriter { return tt })
|
2023-02-23 14:13:27 +00:00
|
|
|
|
|
|
|
ver := version.Current()
|
|
|
|
cnr := cidtest.ID()
|
|
|
|
hdr := objectSDK.New()
|
|
|
|
hdr.SetContainerID(cnr)
|
|
|
|
hdr.SetType(objectSDK.TypeRegular)
|
|
|
|
hdr.SetVersion(&ver)
|
|
|
|
|
|
|
|
payload := make([]byte, maxSize*2+maxSize/2)
|
|
|
|
_, _ = rand.Read(payload)
|
|
|
|
|
2023-04-21 10:04:44 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
expectedIDs := writeObject(t, ctx, testTarget, hdr, payload)
|
|
|
|
actualIDs := writeObject(t, ctx, chTarget, hdr, payload)
|
2023-02-23 14:13:27 +00:00
|
|
|
_ = expectedIDs
|
|
|
|
_ = actualIDs
|
2023-11-21 09:57:35 +00:00
|
|
|
// require.Equal(t, expectedIDs, actualIDs)
|
2023-02-23 14:13:27 +00:00
|
|
|
|
|
|
|
for i := range tt.objects {
|
|
|
|
select {
|
|
|
|
case obj := <-ch:
|
|
|
|
// Because of the split ID objects can be different.
|
|
|
|
// However, payload and attributes must be the same.
|
|
|
|
require.Equal(t, tt.objects[i].Payload(), obj.Payload())
|
|
|
|
require.Equal(t, tt.objects[i].Attributes(), obj.Attributes())
|
|
|
|
default:
|
|
|
|
require.FailNow(t, "received less parts than expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
require.FailNow(t, "received more parts than expected")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|