2020-05-29 17:06:44 +00:00
|
|
|
package fr32_test
|
2020-05-28 17:15:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
ffi "github.com/filecoin-project/filecoin-ffi"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-05-29 17:06:44 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/sector-storage/ffiwrapper"
|
|
|
|
"github.com/filecoin-project/sector-storage/fr32"
|
2020-05-28 17:15:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func padFFI(buf []byte) []byte {
|
|
|
|
rf, w, _ := ffiwrapper.ToReadableFile(bytes.NewReader(buf), int64(len(buf)))
|
|
|
|
tf, _ := ioutil.TempFile("/tmp/", "scrb-")
|
|
|
|
|
|
|
|
_, _, _, err := ffi.WriteWithAlignment(abi.RegisteredProof_StackedDRG32GiBSeal, rf, abi.UnpaddedPieceSize(len(buf)), tf, nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := w(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := tf.Seek(io.SeekStart, 0); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
padded, err := ioutil.ReadAll(tf)
|
|
|
|
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) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|