2018-09-02 19:03:02 +08:00
|
|
|
package pstoreds
|
2018-08-30 20:44:50 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-09-02 19:10:55 +08:00
|
|
|
"github.com/ipfs/go-datastore"
|
|
|
|
"github.com/ipfs/go-ds-badger"
|
2018-08-30 23:24:09 +08:00
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p-peerstore"
|
|
|
|
"github.com/libp2p/go-libp2p-peerstore/test"
|
2018-08-30 20:44:50 +08:00
|
|
|
)
|
|
|
|
|
2018-09-01 01:35:23 +08:00
|
|
|
func setupBadgerDatastore(t testing.TB) (datastore.TxnDatastore, func()) {
|
2018-08-30 20:44:50 +08:00
|
|
|
dataPath, err := ioutil.TempDir(os.TempDir(), "badger")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ds, err := badger.NewDatastore(dataPath, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
closer := func() {
|
|
|
|
ds.Close()
|
|
|
|
os.RemoveAll(dataPath)
|
|
|
|
}
|
|
|
|
return ds, closer
|
|
|
|
}
|
|
|
|
|
2018-09-04 18:34:55 +08:00
|
|
|
func cachingPeerstore(tb testing.TB) test.PeerstoreFactory {
|
|
|
|
opts := DefaultOpts()
|
|
|
|
opts.CacheSize = 1024
|
|
|
|
opts.TTLInterval = 100 * time.Microsecond
|
|
|
|
|
|
|
|
return func() (peerstore.Peerstore, func()) {
|
|
|
|
ds, closeFunc := setupBadgerDatastore(tb)
|
|
|
|
|
|
|
|
ps, err := NewPeerstore(context.Background(), ds, opts)
|
|
|
|
if err != nil {
|
|
|
|
tb.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ps, closeFunc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cachelessPeerstore(tb testing.TB) test.PeerstoreFactory {
|
|
|
|
opts := DefaultOpts()
|
|
|
|
opts.CacheSize = 0
|
|
|
|
opts.TTLInterval = 100 * time.Microsecond
|
|
|
|
|
2018-08-30 20:44:50 +08:00
|
|
|
return func() (peerstore.Peerstore, func()) {
|
|
|
|
ds, closeFunc := setupBadgerDatastore(tb)
|
|
|
|
|
2018-09-04 18:34:55 +08:00
|
|
|
ps, err := NewPeerstore(context.Background(), ds, opts)
|
2018-08-30 20:44:50 +08:00
|
|
|
if err != nil {
|
|
|
|
tb.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ps, closeFunc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBadgerDsPeerstore(t *testing.T) {
|
2018-09-04 18:34:55 +08:00
|
|
|
test.TestPeerstore(t, cachingPeerstore(t))
|
2018-08-30 20:44:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBadgerDsAddrBook(t *testing.T) {
|
2018-09-04 18:34:55 +08:00
|
|
|
opts := DefaultOpts()
|
|
|
|
opts.CacheSize = 0
|
|
|
|
opts.TTLInterval = 100 * time.Microsecond
|
|
|
|
|
2018-09-05 22:08:33 +08:00
|
|
|
test.TestAddrBook(t, func() (peerstore.AddrBook, func()) {
|
2018-08-30 20:44:50 +08:00
|
|
|
ds, closeDB := setupBadgerDatastore(t)
|
|
|
|
|
2018-09-04 18:34:55 +08:00
|
|
|
mgr, err := NewAddrBook(context.Background(), ds, opts)
|
2018-08-30 20:44:50 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
closeFunc := func() {
|
|
|
|
mgr.Stop()
|
|
|
|
closeDB()
|
|
|
|
}
|
|
|
|
return mgr, closeFunc
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkBadgerDsPeerstore(b *testing.B) {
|
2018-09-04 18:34:55 +08:00
|
|
|
test.BenchmarkPeerstore(b, cachingPeerstore(b), "Caching")
|
|
|
|
test.BenchmarkPeerstore(b, cachelessPeerstore(b), "Cacheless")
|
2018-08-30 22:24:06 +08:00
|
|
|
}
|