mirror of
https://github.com/libp2p/go-libp2p-peerstore.git
synced 2025-04-01 14:10:12 +08:00
run tests against multiple datastores.
leveldb disabled for now, until TTL shim is implemented.
This commit is contained in:
parent
2a27e91a67
commit
61b5355499
@ -2,126 +2,38 @@ package pstoreds
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
bd "github.com/dgraph-io/badger"
|
|
||||||
|
|
||||||
ds "github.com/ipfs/go-datastore"
|
ds "github.com/ipfs/go-datastore"
|
||||||
badger "github.com/ipfs/go-ds-badger"
|
badger "github.com/ipfs/go-ds-badger"
|
||||||
|
leveldb "github.com/ipfs/go-ds-leveldb"
|
||||||
|
|
||||||
pstore "github.com/libp2p/go-libp2p-peerstore"
|
pstore "github.com/libp2p/go-libp2p-peerstore"
|
||||||
pt "github.com/libp2p/go-libp2p-peerstore/test"
|
pt "github.com/libp2p/go-libp2p-peerstore/test"
|
||||||
)
|
)
|
||||||
|
|
||||||
func BenchmarkBaselineBadgerDatastorePutEntry(b *testing.B) {
|
type datastoreFactory func(tb testing.TB) (ds.TxnDatastore, func())
|
||||||
bds, closer := badgerStore(b)
|
|
||||||
defer closer()
|
|
||||||
|
|
||||||
b.ResetTimer()
|
var dstores = map[string]datastoreFactory{
|
||||||
for i := 0; i < b.N; i++ {
|
"Badger": badgerStore,
|
||||||
txn, _ := bds.NewTransaction(false)
|
// TODO: Enable once go-ds-leveldb supports TTL via a shim.
|
||||||
|
// "Leveldb": leveldbStore,
|
||||||
|
}
|
||||||
|
|
||||||
key := ds.RawKey(fmt.Sprintf("/key/%d", i))
|
func TestDsPeerstore(t *testing.T) {
|
||||||
txn.Put(key, []byte(fmt.Sprintf("/value/%d", i)))
|
for name, dsFactory := range dstores {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
txn.Commit()
|
pt.TestPeerstore(t, peerstoreFactory(t, dsFactory, DefaultOpts()))
|
||||||
txn.Discard()
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkBaselineBadgerDatastoreGetEntry(b *testing.B) {
|
func TestDsAddrBook(t *testing.T) {
|
||||||
bds, closer := badgerStore(b)
|
for name, dsFactory := range dstores {
|
||||||
defer closer()
|
t.Run(name, func(t *testing.T) {
|
||||||
|
|
||||||
txn, _ := bds.NewTransaction(false)
|
|
||||||
keys := make([]ds.Key, 1000)
|
|
||||||
for i := 0; i < 1000; i++ {
|
|
||||||
key := ds.RawKey(fmt.Sprintf("/key/%d", i))
|
|
||||||
txn.Put(key, []byte(fmt.Sprintf("/value/%d", i)))
|
|
||||||
keys[i] = key
|
|
||||||
}
|
|
||||||
if err := txn.Commit(); err != nil {
|
|
||||||
b.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
b.ResetTimer()
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
txn, _ := bds.NewTransaction(true)
|
|
||||||
if _, err := txn.Get(keys[i%1000]); err != nil {
|
|
||||||
b.Fatal(err)
|
|
||||||
}
|
|
||||||
txn.Discard()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkBaselineBadgerDirectPutEntry(b *testing.B) {
|
|
||||||
opts := bd.DefaultOptions
|
|
||||||
|
|
||||||
dataPath, err := ioutil.TempDir(os.TempDir(), "badger")
|
|
||||||
if err != nil {
|
|
||||||
b.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
opts.Dir = dataPath
|
|
||||||
opts.ValueDir = dataPath
|
|
||||||
opts.SyncWrites = false
|
|
||||||
|
|
||||||
db, err := bd.Open(opts)
|
|
||||||
if err != nil {
|
|
||||||
b.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
b.ResetTimer()
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
txn := db.NewTransaction(true)
|
|
||||||
txn.Set([]byte(fmt.Sprintf("/key/%d", i)), []byte(fmt.Sprintf("/value/%d", i)))
|
|
||||||
txn.Commit(nil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkBaselineBadgerDirectGetEntry(b *testing.B) {
|
|
||||||
opts := bd.DefaultOptions
|
|
||||||
|
|
||||||
dataPath, err := ioutil.TempDir(os.TempDir(), "badger")
|
|
||||||
if err != nil {
|
|
||||||
b.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
opts.Dir = dataPath
|
|
||||||
opts.ValueDir = dataPath
|
|
||||||
|
|
||||||
db, err := bd.Open(opts)
|
|
||||||
if err != nil {
|
|
||||||
b.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
txn := db.NewTransaction(true)
|
|
||||||
for i := 0; i < 1000; i++ {
|
|
||||||
txn.Set([]byte(fmt.Sprintf("/key/%d", i)), []byte(fmt.Sprintf("/value/%d", i)))
|
|
||||||
}
|
|
||||||
txn.Commit(nil)
|
|
||||||
|
|
||||||
b.ResetTimer()
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
txn := db.NewTransaction(false)
|
|
||||||
txn.Get([]byte(fmt.Sprintf("/key/%d", i%1000)))
|
|
||||||
txn.Discard()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBadgerDsPeerstore(t *testing.T) {
|
|
||||||
pt.TestPeerstore(t, peerstoreFactory(t, DefaultOpts()))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBadgerDsAddrBook(t *testing.T) {
|
|
||||||
t.Run("Cacheful", func(t *testing.T) {
|
t.Run("Cacheful", func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
@ -129,7 +41,7 @@ func TestBadgerDsAddrBook(t *testing.T) {
|
|||||||
opts.TTLInterval = 100 * time.Microsecond
|
opts.TTLInterval = 100 * time.Microsecond
|
||||||
opts.CacheSize = 1024
|
opts.CacheSize = 1024
|
||||||
|
|
||||||
pt.TestAddrBook(t, addressBookFactory(t, opts))
|
pt.TestAddrBook(t, addressBookFactory(t, dsFactory, opts))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Cacheless", func(t *testing.T) {
|
t.Run("Cacheless", func(t *testing.T) {
|
||||||
@ -139,33 +51,43 @@ func TestBadgerDsAddrBook(t *testing.T) {
|
|||||||
opts.TTLInterval = 100 * time.Microsecond
|
opts.TTLInterval = 100 * time.Microsecond
|
||||||
opts.CacheSize = 0
|
opts.CacheSize = 0
|
||||||
|
|
||||||
pt.TestAddrBook(t, addressBookFactory(t, opts))
|
pt.TestAddrBook(t, addressBookFactory(t, dsFactory, opts))
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBadgerDsKeyBook(t *testing.T) {
|
func TestDsKeyBook(t *testing.T) {
|
||||||
pt.TestKeyBook(t, keyBookFactory(t, DefaultOpts()))
|
for name, dsFactory := range dstores {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
pt.TestKeyBook(t, keyBookFactory(t, dsFactory, DefaultOpts()))
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkBadgerDsPeerstore(b *testing.B) {
|
func BenchmarkDsPeerstore(b *testing.B) {
|
||||||
caching := DefaultOpts()
|
caching := DefaultOpts()
|
||||||
caching.CacheSize = 1024
|
caching.CacheSize = 1024
|
||||||
|
|
||||||
cacheless := DefaultOpts()
|
cacheless := DefaultOpts()
|
||||||
cacheless.CacheSize = 0
|
cacheless.CacheSize = 0
|
||||||
|
|
||||||
pt.BenchmarkPeerstore(b, peerstoreFactory(b, caching), "Caching")
|
for name, dsFactory := range dstores {
|
||||||
pt.BenchmarkPeerstore(b, peerstoreFactory(b, cacheless), "Cacheless")
|
b.Run(name, func(b *testing.B) {
|
||||||
|
pt.BenchmarkPeerstore(b, peerstoreFactory(b, dsFactory, caching), "Caching")
|
||||||
|
pt.BenchmarkPeerstore(b, peerstoreFactory(b, dsFactory, cacheless), "Cacheless")
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func badgerStore(t testing.TB) (ds.TxnDatastore, func()) {
|
func badgerStore(tb testing.TB) (ds.TxnDatastore, func()) {
|
||||||
dataPath, err := ioutil.TempDir(os.TempDir(), "badger")
|
dataPath, err := ioutil.TempDir(os.TempDir(), "badger")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
tb.Fatal(err)
|
||||||
}
|
}
|
||||||
store, err := badger.NewDatastore(dataPath, nil)
|
store, err := badger.NewDatastore(dataPath, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
tb.Fatal(err)
|
||||||
}
|
}
|
||||||
closer := func() {
|
closer := func() {
|
||||||
store.Close()
|
store.Close()
|
||||||
@ -174,9 +96,25 @@ func badgerStore(t testing.TB) (ds.TxnDatastore, func()) {
|
|||||||
return store, closer
|
return store, closer
|
||||||
}
|
}
|
||||||
|
|
||||||
func peerstoreFactory(tb testing.TB, opts Options) pt.PeerstoreFactory {
|
func leveldbStore(tb testing.TB) (ds.TxnDatastore, func()) {
|
||||||
|
dataPath, err := ioutil.TempDir(os.TempDir(), "leveldb")
|
||||||
|
if err != nil {
|
||||||
|
tb.Fatal(err)
|
||||||
|
}
|
||||||
|
store, err := leveldb.NewDatastore(dataPath, nil)
|
||||||
|
if err != nil {
|
||||||
|
tb.Fatal(err)
|
||||||
|
}
|
||||||
|
closer := func() {
|
||||||
|
store.Close()
|
||||||
|
os.RemoveAll(dataPath)
|
||||||
|
}
|
||||||
|
return store, closer
|
||||||
|
}
|
||||||
|
|
||||||
|
func peerstoreFactory(tb testing.TB, storeFactory datastoreFactory, opts Options) pt.PeerstoreFactory {
|
||||||
return func() (pstore.Peerstore, func()) {
|
return func() (pstore.Peerstore, func()) {
|
||||||
store, closeFunc := badgerStore(tb)
|
store, closeFunc := storeFactory(tb)
|
||||||
|
|
||||||
ps, err := NewPeerstore(context.Background(), store, opts)
|
ps, err := NewPeerstore(context.Background(), store, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -187,9 +125,9 @@ func peerstoreFactory(tb testing.TB, opts Options) pt.PeerstoreFactory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func addressBookFactory(tb testing.TB, opts Options) pt.AddrBookFactory {
|
func addressBookFactory(tb testing.TB, storeFactory datastoreFactory, opts Options) pt.AddrBookFactory {
|
||||||
return func() (pstore.AddrBook, func()) {
|
return func() (pstore.AddrBook, func()) {
|
||||||
store, closeFunc := badgerStore(tb)
|
store, closeFunc := storeFactory(tb)
|
||||||
|
|
||||||
ab, err := NewAddrBook(context.Background(), store, opts)
|
ab, err := NewAddrBook(context.Background(), store, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -200,10 +138,15 @@ func addressBookFactory(tb testing.TB, opts Options) pt.AddrBookFactory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func keyBookFactory(tb testing.TB, opts Options) pt.KeyBookFactory {
|
func keyBookFactory(tb testing.TB, storeFactory datastoreFactory, opts Options) pt.KeyBookFactory {
|
||||||
return func() (pstore.KeyBook, func()) {
|
return func() (pstore.KeyBook, func()) {
|
||||||
store, closeFunc := badgerStore(tb)
|
store, closeFunc := storeFactory(tb)
|
||||||
kb, _ := NewKeyBook(context.Background(), store, opts)
|
|
||||||
|
kb, err := NewKeyBook(context.Background(), store, opts)
|
||||||
|
if err != nil {
|
||||||
|
tb.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
return kb, closeFunc
|
return kb, closeFunc
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user