swarm/storage: add mock store tests for DbStore

This commit is contained in:
Janos Guljas 2018-01-17 12:59:43 +01:00
parent e9c22d4da0
commit 2844dd69c2
3 changed files with 87 additions and 99 deletions

View file

@ -628,10 +628,10 @@ func (s *DbStore) writeBatch(b *leveldb.Batch, entryCnt, dataIdx, accessCnt uint
// not need to store the data, but still need to create the index. // not need to store the data, but still need to create the index.
func newMockEncodeDataFunc(mockStore *mock.NodeStore) func(chunk *Chunk) []byte { func newMockEncodeDataFunc(mockStore *mock.NodeStore) func(chunk *Chunk) []byte {
return func(chunk *Chunk) []byte { return func(chunk *Chunk) []byte {
if err := mockStore.Put(chunk.Key, chunk.SData); err != nil { if err := mockStore.Put(chunk.Key, encodeData(chunk)); err != nil {
log.Error(fmt.Sprintf("%T: Chunk %v put: %v", mockStore, chunk.Key.Log(), err)) log.Error(fmt.Sprintf("%T: Chunk %v put: %v", mockStore, chunk.Key.Log(), err))
} }
return nil return chunk.Key[:]
} }
} }

View file

@ -21,13 +21,11 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"strings"
"sync" "sync"
"testing" "testing"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/swarm/storage/mock"
"github.com/ethereum/go-ethereum/swarm/storage/mock/mem" "github.com/ethereum/go-ethereum/swarm/storage/mock/mem"
) )
@ -36,12 +34,22 @@ type testDbStore struct {
dir string dir string
} }
func newTestDbStore() (*testDbStore, error) { func newTestDbStore(mock bool) (*testDbStore, error) {
dir, err := ioutil.TempDir("", "bzz-storage-test") dir, err := ioutil.TempDir("", "bzz-storage-test")
if err != nil { if err != nil {
return nil, err return nil, err
} }
db, err := NewDbStore(dir, MakeHashFunc(SHA3Hash), defaultDbCapacity, testPoFunc)
var db *DbStore
if mock {
globalStore := mem.NewGlobalStore()
addr := common.HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed")
mockStore := globalStore.NewNodeStore(addr)
db, err = NewMockDbStore(dir, MakeHashFunc(SHA3Hash), defaultDbCapacity, testPoFunc, mockStore)
} else {
db, err = NewDbStore(dir, MakeHashFunc(SHA3Hash), defaultDbCapacity, testPoFunc)
}
return &testDbStore{db, dir}, err return &testDbStore{db, dir}, err
} }
@ -59,8 +67,8 @@ func (db *testDbStore) close() {
} }
} }
func testDbStoreRandom(n int, processors int, chunksize int, t *testing.T) { func testDbStoreRandom(n int, processors int, chunksize int, mock bool, t *testing.T) {
db, err := newTestDbStore() db, err := newTestDbStore(mock)
if err != nil { if err != nil {
t.Fatalf("init dbStore failed: %v", err) t.Fatalf("init dbStore failed: %v", err)
} }
@ -69,8 +77,8 @@ func testDbStoreRandom(n int, processors int, chunksize int, t *testing.T) {
testStoreRandom(db, processors, n, chunksize, t) testStoreRandom(db, processors, n, chunksize, t)
} }
func testDbStoreCorrect(n int, processors int, chunksize int, t *testing.T) { func testDbStoreCorrect(n int, processors int, chunksize int, mock bool, t *testing.T) {
db, err := newTestDbStore() db, err := newTestDbStore(mock)
if err != nil { if err != nil {
t.Fatalf("init dbStore failed: %v", err) t.Fatalf("init dbStore failed: %v", err)
} }
@ -79,31 +87,55 @@ func testDbStoreCorrect(n int, processors int, chunksize int, t *testing.T) {
} }
func TestDbStoreRandom_1(t *testing.T) { func TestDbStoreRandom_1(t *testing.T) {
testDbStoreRandom(1, 1, 0, t) testDbStoreRandom(1, 1, 0, false, t)
} }
func TestDbStoreCorrect_1(t *testing.T) { func TestDbStoreCorrect_1(t *testing.T) {
testDbStoreCorrect(1, 1, 4096, t) testDbStoreCorrect(1, 1, 4096, false, t)
} }
func TestDbStoreRandom_1_5k(t *testing.T) { func TestDbStoreRandom_1_5k(t *testing.T) {
testDbStoreRandom(8, 5000, 0, t) testDbStoreRandom(8, 5000, 0, false, t)
} }
func TestDbStoreRandom_8_5k(t *testing.T) { func TestDbStoreRandom_8_5k(t *testing.T) {
testDbStoreRandom(8, 5000, 0, t) testDbStoreRandom(8, 5000, 0, false, t)
} }
func TestDbStoreCorrect_1_5k(t *testing.T) { func TestDbStoreCorrect_1_5k(t *testing.T) {
testDbStoreCorrect(1, 5000, 4096, t) testDbStoreCorrect(1, 5000, 4096, false, t)
} }
func TestDbStoreCorrect_8_5k(t *testing.T) { func TestDbStoreCorrect_8_5k(t *testing.T) {
testDbStoreCorrect(8, 5000, 4096, t) testDbStoreCorrect(8, 5000, 4096, false, t)
} }
func TestDbStoreNotFound(t *testing.T) { func TestMockDbStoreRandom_1(t *testing.T) {
db, err := newTestDbStore() testDbStoreRandom(1, 1, 0, true, t)
}
func TestMockDbStoreCorrect_1(t *testing.T) {
testDbStoreCorrect(1, 1, 4096, true, t)
}
func TestMockDbStoreRandom_1_5k(t *testing.T) {
testDbStoreRandom(8, 5000, 0, true, t)
}
func TestMockDbStoreRandom_8_5k(t *testing.T) {
testDbStoreRandom(8, 5000, 0, true, t)
}
func TestMockDbStoreCorrect_1_5k(t *testing.T) {
testDbStoreCorrect(1, 5000, 4096, true, t)
}
func TestMockDbStoreCorrect_8_5k(t *testing.T) {
testDbStoreCorrect(8, 5000, 4096, true, t)
}
func testDbStoreNotFound(t *testing.T, mock bool) {
db, err := newTestDbStore(mock)
if err != nil { if err != nil {
t.Fatalf("init dbStore failed: %v", err) t.Fatalf("init dbStore failed: %v", err)
} }
@ -115,7 +147,14 @@ func TestDbStoreNotFound(t *testing.T) {
} }
} }
func TestIterator(t *testing.T) { func TestDbStoreNotFound(t *testing.T) {
testDbStoreNotFound(t, false)
}
func TestMockDbStoreNotFound(t *testing.T) {
testDbStoreNotFound(t, true)
}
func testIterator(t *testing.T, mock bool) {
var chunkcount int = 32 var chunkcount int = 32
var i int var i int
var poc uint var poc uint
@ -127,7 +166,7 @@ func TestIterator(t *testing.T) {
chunks = append(chunks, NewChunk(nil, nil)) chunks = append(chunks, NewChunk(nil, nil))
} }
db, err := newTestDbStore() db, err := newTestDbStore(mock)
if err != nil { if err != nil {
t.Fatalf("init dbStore failed: %v", err) t.Fatalf("init dbStore failed: %v", err)
} }
@ -174,8 +213,15 @@ func TestIterator(t *testing.T) {
} }
func benchmarkDbStorePut(n int, processors int, chunksize int, b *testing.B) { func TestIterator(t *testing.T) {
db, err := newTestDbStore() testIterator(t, false)
}
func TestMockIterator(t *testing.T) {
testIterator(t, true)
}
func benchmarkDbStorePut(n int, processors int, chunksize int, mock bool, b *testing.B) {
db, err := newTestDbStore(mock)
if err != nil { if err != nil {
b.Fatalf("init dbStore failed: %v", err) b.Fatalf("init dbStore failed: %v", err)
} }
@ -184,8 +230,8 @@ func benchmarkDbStorePut(n int, processors int, chunksize int, b *testing.B) {
benchmarkStorePut(db, processors, n, chunksize, b) benchmarkStorePut(db, processors, n, chunksize, b)
} }
func benchmarkDbStoreGet(n int, processors int, chunksize int, b *testing.B) { func benchmarkDbStoreGet(n int, processors int, chunksize int, mock bool, b *testing.B) {
db, err := newTestDbStore() db, err := newTestDbStore(mock)
if err != nil { if err != nil {
b.Fatalf("init dbStore failed: %v", err) b.Fatalf("init dbStore failed: %v", err)
} }
@ -195,91 +241,33 @@ func benchmarkDbStoreGet(n int, processors int, chunksize int, b *testing.B) {
} }
func BenchmarkDbStorePut_1_5k(b *testing.B) { func BenchmarkDbStorePut_1_5k(b *testing.B) {
benchmarkDbStorePut(5000, 1, 4096, b) benchmarkDbStorePut(5000, 1, 4096, false, b)
} }
func BenchmarkDbStorePut_8_5k(b *testing.B) { func BenchmarkDbStorePut_8_5k(b *testing.B) {
benchmarkDbStorePut(5000, 8, 4096, b) benchmarkDbStorePut(5000, 8, 4096, false, b)
} }
func BenchmarkDbStoreGet_1_5k(b *testing.B) { func BenchmarkDbStoreGet_1_5k(b *testing.B) {
benchmarkDbStoreGet(5000, 1, 4096, b) benchmarkDbStoreGet(5000, 1, 4096, false, b)
} }
func BenchmarkDbStoreGet_8_5k(b *testing.B) { func BenchmarkDbStoreGet_8_5k(b *testing.B) {
benchmarkDbStoreGet(5000, 8, 4096, b) benchmarkDbStoreGet(5000, 8, 4096, false, b)
} }
func initMockDbStore(t *testing.T, mockStore *mock.NodeStore) *DbStore { func BenchmarkMockDbStorePut_1_5k(b *testing.B) {
dir, err := ioutil.TempDir("", "bzz-storage-test-mock") benchmarkDbStorePut(5000, 1, 4096, true, b)
if err != nil {
t.Fatal(err)
}
m, err := NewMockDbStore(dir, MakeHashFunc(SHA3Hash), defaultDbCapacity, testPoFunc, mockStore)
if err != nil {
t.Fatal("can't create store:", err)
}
return m
} }
// testMockDbStore runs the same tests as testDbStore but with mock store configured. func BenchmarkMockDbStorePut_8_5k(b *testing.B) {
// It also verifies if mock global store is storing the chunk data. benchmarkDbStorePut(5000, 8, 4096, true, b)
func testMockDbStore(l int64, branches int64, t *testing.T) {
globalStore := mem.NewGlobalStore()
addr := common.HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed")
mockStore := globalStore.NewNodeStore(addr)
m := initMockDbStore(t, mockStore)
defer m.Close()
key := Key(common.Hex2Bytes("fed1911825fc6a02ebfd19ab218a20455d8d7d275f8bf4d8244eb04364fae6f7"))
data := common.Hex2BytesFixed(strings.Repeat("1234567890abcdf", 10), 4096)
m.Put(&Chunk{
Key: key,
SData: data,
})
_, err := globalStore.Get(addr, key)
if err != nil {
t.Errorf("unexpected error getting the data from global mock store: %v", err)
} }
if !globalStore.HasKey(addr, key) { func BenchmarkMockDbStoreGet_1_5k(b *testing.B) {
t.Error("key not found in global store") benchmarkDbStoreGet(5000, 1, 4096, true, b)
} }
// TODO: fix this! func BenchmarkMockDbStoreGet_8_5k(b *testing.B) {
// testStoreRandom(m, 8, l, chunk.S, t) benchmarkDbStoreGet(5000, 8, 4096, true, b)
} }
// func TestMockDbStore128_0x1000000(t *testing.T) {
// testMockDbStore(0x1000000, 128, t)
// }
//
// func TestMockDbStore128_10000_(t *testing.T) {
// testMockDbStore(10000, 128, t)
// }
//
// func TestMockDbStore128_1000_(t *testing.T) {
// testMockDbStore(1000, 128, t)
// }
//
// func TestMockDbStore128_100_(t *testing.T) {
// testMockDbStore(100, 128, t)
// }
//
// func TestMockDbStore2_100_(t *testing.T) {
// testMockDbStore(100, 2, t)
// }
//
// func TestMockDbStoreNotFound(t *testing.T) {
// globalStore := mem.NewGlobalStore()
// mockStore := globalStore.NewNodeStore(common.HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed"))
// m := initMockDbStore(t, mockStore)
// defer m.Close()
// _, err := m.Get(ZeroKey)
// if err != notFound {
// t.Errorf("Expected notFound, got %v", err)
// }
// }

View file

@ -27,7 +27,7 @@ import (
const testDataSize = 0x1000000 const testDataSize = 0x1000000
func TestDPArandom(t *testing.T) { func TestDPArandom(t *testing.T) {
tdb, err := newTestDbStore() tdb, err := newTestDbStore(false)
if err != nil { if err != nil {
t.Fatalf("init dbStore failed: %v", err) t.Fatalf("init dbStore failed: %v", err)
} }
@ -86,7 +86,7 @@ func TestDPArandom(t *testing.T) {
} }
func TestDPA_capacity(t *testing.T) { func TestDPA_capacity(t *testing.T) {
tdb, err := newTestDbStore() tdb, err := newTestDbStore(false)
if err != nil { if err != nil {
t.Fatalf("init dbStore failed: %v", err) t.Fatalf("init dbStore failed: %v", err)
} }