2016-08-29 22:18:00 +03:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2018-07-13 18:40:28 +03:00
|
|
|
"context"
|
2016-08-29 22:18:00 +03:00
|
|
|
"testing"
|
2018-06-20 15:06:27 +03:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/swarm/log"
|
2016-08-29 22:18:00 +03:00
|
|
|
)
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
func newTestMemStore() *MemStore {
|
|
|
|
storeparams := NewDefaultStoreParams()
|
|
|
|
return NewMemStore(storeparams, nil)
|
|
|
|
}
|
|
|
|
|
2019-02-08 19:07:11 +03:00
|
|
|
func testMemStoreRandom(n int, t *testing.T) {
|
2018-06-20 15:06:27 +03:00
|
|
|
m := newTestMemStore()
|
|
|
|
defer m.Close()
|
2019-02-08 19:07:11 +03:00
|
|
|
testStoreRandom(m, n, t)
|
2018-06-20 15:06:27 +03:00
|
|
|
}
|
|
|
|
|
2019-02-08 19:07:11 +03:00
|
|
|
func testMemStoreCorrect(n int, t *testing.T) {
|
2018-06-20 15:06:27 +03:00
|
|
|
m := newTestMemStore()
|
|
|
|
defer m.Close()
|
2019-02-08 19:07:11 +03:00
|
|
|
testStoreCorrect(m, n, t)
|
2018-06-20 15:06:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemStoreRandom_1(t *testing.T) {
|
2019-02-08 19:07:11 +03:00
|
|
|
testMemStoreRandom(1, t)
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
func TestMemStoreCorrect_1(t *testing.T) {
|
2019-02-08 19:07:11 +03:00
|
|
|
testMemStoreCorrect(1, t)
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|
|
|
|
|
2018-09-13 12:42:19 +03:00
|
|
|
func TestMemStoreRandom_1k(t *testing.T) {
|
2019-02-08 19:07:11 +03:00
|
|
|
testMemStoreRandom(1000, t)
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|
|
|
|
|
2018-09-13 12:42:19 +03:00
|
|
|
func TestMemStoreCorrect_1k(t *testing.T) {
|
2019-02-08 19:07:11 +03:00
|
|
|
testMemStoreCorrect(100, t)
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemStoreNotFound(t *testing.T) {
|
2018-06-20 15:06:27 +03:00
|
|
|
m := newTestMemStore()
|
|
|
|
defer m.Close()
|
|
|
|
|
2018-07-13 18:40:28 +03:00
|
|
|
_, err := m.Get(context.TODO(), ZeroAddr)
|
2018-06-20 15:06:27 +03:00
|
|
|
if err != ErrChunkNotFound {
|
|
|
|
t.Errorf("Expected ErrChunkNotFound, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 19:07:11 +03:00
|
|
|
func benchmarkMemStorePut(n int, b *testing.B) {
|
2018-06-20 15:06:27 +03:00
|
|
|
m := newTestMemStore()
|
|
|
|
defer m.Close()
|
2019-02-08 19:07:11 +03:00
|
|
|
benchmarkStorePut(m, n, b)
|
2018-06-20 15:06:27 +03:00
|
|
|
}
|
|
|
|
|
2019-02-08 19:07:11 +03:00
|
|
|
func benchmarkMemStoreGet(n int, b *testing.B) {
|
2018-06-20 15:06:27 +03:00
|
|
|
m := newTestMemStore()
|
|
|
|
defer m.Close()
|
2019-02-08 19:07:11 +03:00
|
|
|
benchmarkStoreGet(m, n, b)
|
2018-06-20 15:06:27 +03:00
|
|
|
}
|
|
|
|
|
2019-02-08 19:07:11 +03:00
|
|
|
func BenchmarkMemStorePut_500(b *testing.B) {
|
|
|
|
benchmarkMemStorePut(500, b)
|
2018-06-20 15:06:27 +03:00
|
|
|
}
|
|
|
|
|
2019-02-08 19:07:11 +03:00
|
|
|
func BenchmarkMemStoreGet_500(b *testing.B) {
|
|
|
|
benchmarkMemStoreGet(500, b)
|
2018-06-20 15:06:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemStoreAndLDBStore(t *testing.T) {
|
|
|
|
ldb, cleanup := newLDBStore(t)
|
|
|
|
ldb.setCapacity(4000)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
cacheCap := 200
|
2018-09-13 12:42:19 +03:00
|
|
|
memStore := NewMemStore(NewStoreParams(4000, 200, nil, nil), nil)
|
2018-06-20 15:06:27 +03:00
|
|
|
|
|
|
|
tests := []struct {
|
2018-09-13 12:42:19 +03:00
|
|
|
n int // number of chunks to push to memStore
|
|
|
|
chunkSize int64 // size of chunk (by default in Swarm - 4096)
|
2018-06-20 15:06:27 +03:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
n: 1,
|
|
|
|
chunkSize: 4096,
|
|
|
|
},
|
|
|
|
{
|
2018-10-09 12:45:42 +03:00
|
|
|
n: 101,
|
2018-06-20 15:06:27 +03:00
|
|
|
chunkSize: 4096,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
n: 501,
|
|
|
|
chunkSize: 4096,
|
|
|
|
},
|
|
|
|
{
|
2018-10-09 12:45:42 +03:00
|
|
|
n: 1100,
|
2018-06-20 15:06:27 +03:00
|
|
|
chunkSize: 4096,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
log.Info("running test", "idx", i, "tt", tt)
|
2018-09-13 12:42:19 +03:00
|
|
|
var chunks []Chunk
|
2018-06-20 15:06:27 +03:00
|
|
|
|
|
|
|
for i := 0; i < tt.n; i++ {
|
2018-09-13 12:42:19 +03:00
|
|
|
c := GenerateRandomChunk(tt.chunkSize)
|
2018-06-20 15:06:27 +03:00
|
|
|
chunks = append(chunks, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < tt.n; i++ {
|
2018-09-13 12:42:19 +03:00
|
|
|
err := ldb.Put(context.TODO(), chunks[i])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = memStore.Put(context.TODO(), chunks[i])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-06-20 15:06:27 +03:00
|
|
|
|
|
|
|
if got := memStore.cache.Len(); got > cacheCap {
|
|
|
|
t.Fatalf("expected to get cache capacity less than %v, but got %v", cacheCap, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < tt.n; i++ {
|
2018-09-13 12:42:19 +03:00
|
|
|
_, err := memStore.Get(context.TODO(), chunks[i].Address())
|
2018-06-20 15:06:27 +03:00
|
|
|
if err != nil {
|
|
|
|
if err == ErrChunkNotFound {
|
2018-09-13 12:42:19 +03:00
|
|
|
_, err := ldb.Get(context.TODO(), chunks[i].Address())
|
2018-06-20 15:06:27 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("couldn't get chunk %v from ldb, got error: %v", i, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Fatalf("got error from memstore: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|