mirror of
https://github.com/libp2p/go-libp2p-peerstore.git
synced 2025-01-01 00:20:11 +08:00
7d8b6c9843
Also adjust test logic. Last changes were ticking the ttl manager too often unnecessarily. This was skewing benchmarks.
83 lines
1.7 KiB
Go
83 lines
1.7 KiB
Go
package pstoreds
|
|
|
|
import (
|
|
"context"
|
|
"io/ioutil"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/ipfs/go-datastore"
|
|
"github.com/ipfs/go-ds-badger"
|
|
"github.com/libp2p/go-libp2p-peerstore"
|
|
"github.com/libp2p/go-libp2p-peerstore/test"
|
|
)
|
|
|
|
func TestBadgerDsPeerstore(t *testing.T) {
|
|
test.TestPeerstore(t, peerstoreFactory(t, DefaultOpts()))
|
|
}
|
|
|
|
func TestBadgerDsAddrBook(t *testing.T) {
|
|
opts := DefaultOpts()
|
|
opts.TTLInterval = 100 * time.Microsecond
|
|
|
|
test.TestAddrBook(t, addressBookFactory(t, opts))
|
|
}
|
|
|
|
func BenchmarkBadgerDsPeerstore(b *testing.B) {
|
|
caching := DefaultOpts()
|
|
caching.CacheSize = 1024
|
|
|
|
cacheless := DefaultOpts()
|
|
cacheless.CacheSize = 0
|
|
|
|
test.BenchmarkPeerstore(b, peerstoreFactory(b, caching), "Caching")
|
|
test.BenchmarkPeerstore(b, peerstoreFactory(b, cacheless), "Cacheless")
|
|
}
|
|
|
|
func badgerStore(t testing.TB) (datastore.TxnDatastore, func()) {
|
|
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
|
|
}
|
|
|
|
func peerstoreFactory(tb testing.TB, opts PeerstoreOpts) test.PeerstoreFactory {
|
|
return func() (peerstore.Peerstore, func()) {
|
|
ds, closeFunc := badgerStore(tb)
|
|
|
|
ps, err := NewPeerstore(context.Background(), ds, opts)
|
|
if err != nil {
|
|
tb.Fatal(err)
|
|
}
|
|
|
|
return ps, closeFunc
|
|
}
|
|
}
|
|
|
|
func addressBookFactory(tb testing.TB, opts PeerstoreOpts) test.AddrBookFactory {
|
|
return func() (peerstore.AddrBook, func()) {
|
|
ds, closeDB := badgerStore(tb)
|
|
|
|
mgr, err := NewAddrBook(context.Background(), ds, opts)
|
|
if err != nil {
|
|
tb.Fatal(err)
|
|
}
|
|
|
|
closeFunc := func() {
|
|
mgr.Stop()
|
|
closeDB()
|
|
}
|
|
return mgr, closeFunc
|
|
}
|
|
}
|