lotus/storage/sealer/fr32/fr32_test.go

252 lines
4.6 KiB
Go
Raw Normal View History

2020-05-29 17:06:44 +00:00
package fr32_test
2020-05-28 17:15:15 +00:00
import (
"bytes"
2023-11-25 15:15:09 +00:00
"crypto/rand"
2020-05-28 17:15:15 +00:00
"io"
"os"
"testing"
2022-06-14 15:00:51 +00:00
"github.com/stretchr/testify/require"
2020-05-28 17:15:15 +00:00
ffi "github.com/filecoin-project/filecoin-ffi"
2020-11-23 15:09:09 +00:00
commpffi "github.com/filecoin-project/go-commp-utils/ffiwrapper"
2020-09-07 03:49:10 +00:00
"github.com/filecoin-project/go-state-types/abi"
2020-05-29 17:06:44 +00:00
"github.com/filecoin-project/lotus/storage/sealer/fr32"
2020-05-28 17:15:15 +00:00
)
func padFFI(buf []byte) []byte {
2020-11-23 15:09:09 +00:00
rf, w, _ := commpffi.ToReadableFile(bytes.NewReader(buf), int64(len(buf)))
tf, _ := os.CreateTemp("/tmp/", "scrb-")
2020-05-28 17:15:15 +00:00
2020-06-15 12:32:17 +00:00
_, _, _, err := ffi.WriteWithAlignment(abi.RegisteredSealProof_StackedDrg32GiBV1, rf, abi.UnpaddedPieceSize(len(buf)), tf, nil)
2020-05-28 17:15:15 +00:00
if err != nil {
panic(err)
}
if err := w(); err != nil {
panic(err)
}
2020-08-16 10:40:35 +00:00
if _, err := tf.Seek(io.SeekStart, 0); err != nil { // nolint:staticcheck
2020-05-28 17:15:15 +00:00
panic(err)
}
padded, err := io.ReadAll(tf)
2020-05-28 17:15:15 +00:00
if err != nil {
panic(err)
}
if err := tf.Close(); err != nil {
panic(err)
}
if err := os.Remove(tf.Name()); err != nil {
panic(err)
}
return padded
}
func TestPadChunkFFI(t *testing.T) {
testByteChunk := func(b byte) func(*testing.T) {
return func(t *testing.T) {
var buf [128]byte
copy(buf[:], bytes.Repeat([]byte{b}, 127))
2020-05-29 17:06:44 +00:00
fr32.Pad(buf[:], buf[:])
2020-05-28 17:15:15 +00:00
expect := padFFI(bytes.Repeat([]byte{b}, 127))
require.Equal(t, expect, buf[:])
}
}
t.Run("ones", testByteChunk(0xff))
t.Run("lsb1", testByteChunk(0x01))
t.Run("msb1", testByteChunk(0x80))
t.Run("zero", testByteChunk(0x0))
t.Run("mid", testByteChunk(0x3c))
}
func TestPadChunkRandEqFFI(t *testing.T) {
2023-11-25 15:15:09 +00:00
2020-05-28 17:15:15 +00:00
for i := 0; i < 200; i++ {
var input [127]byte
rand.Read(input[:])
var buf [128]byte
2020-05-29 17:06:44 +00:00
fr32.Pad(input[:], buf[:])
2020-05-28 17:15:15 +00:00
expect := padFFI(input[:])
require.Equal(t, expect, buf[:])
}
}
func TestRoundtrip(t *testing.T) {
testByteChunk := func(b byte) func(*testing.T) {
return func(t *testing.T) {
var buf [128]byte
input := bytes.Repeat([]byte{0x01}, 127)
2020-05-29 17:06:44 +00:00
fr32.Pad(input, buf[:])
2020-05-28 17:15:15 +00:00
var out [127]byte
2020-05-29 17:06:44 +00:00
fr32.Unpad(buf[:], out[:])
2020-05-28 17:15:15 +00:00
require.Equal(t, input, out[:])
}
}
t.Run("ones", testByteChunk(0xff))
t.Run("lsb1", testByteChunk(0x01))
t.Run("msb1", testByteChunk(0x80))
t.Run("zero", testByteChunk(0x0))
t.Run("mid", testByteChunk(0x3c))
}
func TestRoundtripChunkRand(t *testing.T) {
for i := 0; i < 200; i++ {
var input [127]byte
rand.Read(input[:])
var buf [128]byte
copy(buf[:], input[:])
2020-05-29 17:06:44 +00:00
fr32.Pad(buf[:], buf[:])
2020-05-28 17:15:15 +00:00
var out [127]byte
2020-05-29 17:06:44 +00:00
fr32.Unpad(buf[:], out[:])
2020-05-28 17:15:15 +00:00
require.Equal(t, input[:], out[:])
}
}
func TestRoundtrip16MRand(t *testing.T) {
up := abi.PaddedPieceSize(16 << 20).Unpadded()
input := make([]byte, up)
rand.Read(input[:])
buf := make([]byte, 16<<20)
2020-05-29 17:06:44 +00:00
fr32.Pad(input, buf)
2020-05-28 17:15:15 +00:00
out := make([]byte, up)
2020-05-29 17:06:44 +00:00
fr32.Unpad(buf, out)
2020-05-28 17:15:15 +00:00
require.Equal(t, input, out)
ffi := padFFI(input)
require.Equal(t, ffi, buf)
}
func BenchmarkPadChunk(b *testing.B) {
var buf [128]byte
in := bytes.Repeat([]byte{0xff}, 127)
b.SetBytes(127)
for i := 0; i < b.N; i++ {
2020-05-29 17:06:44 +00:00
fr32.Pad(in, buf[:])
2020-05-28 17:15:15 +00:00
}
}
func BenchmarkChunkRoundtrip(b *testing.B) {
var buf [128]byte
copy(buf[:], bytes.Repeat([]byte{0xff}, 127))
var out [127]byte
b.SetBytes(127)
for i := 0; i < b.N; i++ {
2020-05-29 17:06:44 +00:00
fr32.Pad(buf[:], buf[:])
fr32.Unpad(buf[:], out[:])
2020-05-28 17:15:15 +00:00
}
}
func BenchmarkUnpadChunk(b *testing.B) {
var buf [128]byte
copy(buf[:], bytes.Repeat([]byte{0xff}, 127))
2020-05-29 17:06:44 +00:00
fr32.Pad(buf[:], buf[:])
2020-05-28 17:15:15 +00:00
var out [127]byte
b.SetBytes(127)
b.ReportAllocs()
bs := buf[:]
for i := 0; i < b.N; i++ {
2020-05-29 17:06:44 +00:00
fr32.Unpad(bs, out[:])
2020-05-28 17:15:15 +00:00
}
}
func BenchmarkUnpad16MChunk(b *testing.B) {
up := abi.PaddedPieceSize(16 << 20).Unpadded()
var buf [16 << 20]byte
2020-05-29 17:06:44 +00:00
fr32.Pad(bytes.Repeat([]byte{0xff}, int(up)), buf[:])
2020-05-28 17:15:15 +00:00
var out [16 << 20]byte
b.SetBytes(16 << 20)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
2020-05-29 17:06:44 +00:00
fr32.Unpad(buf[:], out[:])
2020-05-28 17:15:15 +00:00
}
}
func BenchmarkPad16MChunk(b *testing.B) {
up := abi.PaddedPieceSize(16 << 20).Unpadded()
var buf [16 << 20]byte
in := bytes.Repeat([]byte{0xff}, int(up))
b.SetBytes(16 << 20)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
2020-05-29 17:06:44 +00:00
fr32.Pad(in, buf[:])
2020-05-28 17:15:15 +00:00
}
}
func BenchmarkPad1GChunk(b *testing.B) {
up := abi.PaddedPieceSize(1 << 30).Unpadded()
var buf [1 << 30]byte
in := bytes.Repeat([]byte{0xff}, int(up))
b.SetBytes(1 << 30)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
2020-05-29 17:06:44 +00:00
fr32.Pad(in, buf[:])
2020-05-28 17:15:15 +00:00
}
}
func BenchmarkUnpad1GChunk(b *testing.B) {
up := abi.PaddedPieceSize(1 << 30).Unpadded()
var buf [1 << 30]byte
2020-05-29 17:06:44 +00:00
fr32.Pad(bytes.Repeat([]byte{0xff}, int(up)), buf[:])
2020-05-28 17:15:15 +00:00
var out [1 << 30]byte
b.SetBytes(1 << 30)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
2020-05-29 17:06:44 +00:00
fr32.Unpad(buf[:], out[:])
2020-05-28 17:15:15 +00:00
}
}