提交 be982269 作者: Brian Tiger Chow

feat(epictest) tests and benchmarks

License: MIT
Signed-off-by: 's avatarBrian Tiger Chow <brian@perfmode.com>
上级 cf43cdd5
package epictest
import (
"bytes"
randcrypto "crypto/rand"
"fmt"
"io"
"os"
"testing"
"time"
blockservice "github.com/jbenet/go-ipfs/blockservice"
bitswap "github.com/jbenet/go-ipfs/exchange/bitswap"
tn "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
importer "github.com/jbenet/go-ipfs/importer"
chunk "github.com/jbenet/go-ipfs/importer/chunk"
merkledag "github.com/jbenet/go-ipfs/merkledag"
path "github.com/jbenet/go-ipfs/path"
mockrouting "github.com/jbenet/go-ipfs/routing/mock"
uio "github.com/jbenet/go-ipfs/unixfs/io"
util "github.com/jbenet/go-ipfs/util"
errors "github.com/jbenet/go-ipfs/util/debugerror"
delay "github.com/jbenet/go-ipfs/util/delay"
)
func Test100MBInstantaneous(t *testing.T) {
t.Log("a sanity check")
t.Parallel()
conf := Config{
NetworkLatency: 0,
RoutingLatency: 0,
BlockstoreLatency: 0,
DataAmountBytes: 100 * 1024 * 1024,
}
AddCatBytes(conf)
}
func TestDegenerateSlowBlockstore(t *testing.T) {
SkipUnlessEpic(t)
t.Parallel()
conf := Config{BlockstoreLatency: 50 * time.Millisecond}
if err := AddCatPowers(conf, 128); err != nil {
t.Fatal(err)
}
}
func TestDegenerateSlowNetwork(t *testing.T) {
SkipUnlessEpic(t)
t.Parallel()
conf := Config{NetworkLatency: 400 * time.Millisecond}
if err := AddCatPowers(conf, 128); err != nil {
t.Fatal(err)
}
}
func TestDegenerateSlowRouting(t *testing.T) {
SkipUnlessEpic(t)
t.Parallel()
conf := Config{RoutingLatency: 400 * time.Millisecond}
if err := AddCatPowers(conf, 128); err != nil {
t.Fatal(err)
}
}
func Test100MBMacbookCoastToCoast(t *testing.T) {
SkipUnlessEpic(t)
t.Parallel()
conf := Config{
DataAmountBytes: 100 * 1024 * 1024,
}.Network_NYtoSF().Blockstore_SlowSSD2014().Routing_Slow()
if err := AddCatBytes(conf); err != nil {
t.Fatal(err)
}
}
func AddCatPowers(conf Config, megabytesMax int64) error {
var i int64
for i = 1; i < megabytesMax; i = i * 2 {
fmt.Printf("%d MB\n", i)
conf.DataAmountBytes = i * 1024 * 1024
if err := AddCatBytes(conf); err != nil {
return err
}
}
return nil
}
func AddCatBytes(conf Config) error {
sessionGenerator := bitswap.NewSessionGenerator(
tn.VirtualNetwork(delay.Fixed(conf.NetworkLatency)), // TODO rename VirtualNetwork
mockrouting.NewServerWithDelay(delay.Fixed(conf.RoutingLatency)),
)
adder := sessionGenerator.Next()
catter := sessionGenerator.Next()
catter.SetBlockstoreLatency(conf.BlockstoreLatency)
adder.SetBlockstoreLatency(0) // disable blockstore latency during add operation
var data bytes.Buffer
// FIXME replace with a random data generator that reproduces data given a seed value
io.Copy(&data, &io.LimitedReader{R: randcrypto.Reader, N: conf.DataAmountBytes})
keyAdded, err := add(adder, bytes.NewReader(data.Bytes()))
if err != nil {
return err
}
adder.SetBlockstoreLatency(conf.BlockstoreLatency) // add some blockstore delay to make the catter wait
readerCatted, err := cat(catter, keyAdded)
if err != nil {
return err
}
// verify
var bufout bytes.Buffer
io.Copy(&bufout, readerCatted)
if 0 != bytes.Compare(bufout.Bytes(), data.Bytes()) {
return errors.New("catted data does not match added data")
}
return nil
}
func cat(catter bitswap.Instance, k util.Key) (io.Reader, error) {
catterdag := merkledag.NewDAGService(&blockservice.BlockService{catter.Blockstore(), catter.Exchange})
nodeCatted, err := (&path.Resolver{catterdag}).ResolvePath(k.String())
if err != nil {
return nil, err
}
return uio.NewDagReader(nodeCatted, catterdag)
}
func add(adder bitswap.Instance, r io.Reader) (util.Key, error) {
nodeAdded, err := importer.BuildDagFromReader(
r,
merkledag.NewDAGService(&blockservice.BlockService{adder.Blockstore(), adder.Exchange}),
nil,
chunk.DefaultSplitter,
)
if err != nil {
return "", err
}
return nodeAdded.Key()
}
func SkipUnlessEpic(t *testing.T) {
if os.Getenv("IPFS_EPIC_TEST") == "" {
t.SkipNow()
}
}
package epictest
import "testing"
func benchmarkAddCat(conf Config, b *testing.B) {
b.SetBytes(conf.DataAmountBytes)
for n := 0; n < b.N; n++ {
if err := AddCatBytes(conf); err != nil {
b.Fatal(err)
}
}
}
var instant = Config{}.All_Instantaneous()
func BenchmarkInstantaneousAddCat1MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(1), b) }
func BenchmarkInstantaneousAddCat2MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(2), b) }
func BenchmarkInstantaneousAddCat4MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(4), b) }
func BenchmarkInstantaneousAddCat8MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(8), b) }
func BenchmarkInstantaneousAddCat16MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(16), b) }
func BenchmarkInstantaneousAddCat32MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(32), b) }
func BenchmarkInstantaneousAddCat64MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(64), b) }
func BenchmarkInstantaneousAddCat128MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(128), b) }
func BenchmarkInstantaneousAddCat256MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(256), b) }
var routing = Config{}.Routing_Slow()
func BenchmarkRoutingSlowAddCat1MB(b *testing.B) { benchmarkAddCat(routing.Megabytes(1), b) }
func BenchmarkRoutingSlowAddCat2MB(b *testing.B) { benchmarkAddCat(routing.Megabytes(2), b) }
func BenchmarkRoutingSlowAddCat4MB(b *testing.B) { benchmarkAddCat(routing.Megabytes(4), b) }
func BenchmarkRoutingSlowAddCat8MB(b *testing.B) { benchmarkAddCat(routing.Megabytes(8), b) }
func BenchmarkRoutingSlowAddCat16MB(b *testing.B) { benchmarkAddCat(routing.Megabytes(16), b) }
func BenchmarkRoutingSlowAddCat32MB(b *testing.B) { benchmarkAddCat(routing.Megabytes(32), b) }
var network = Config{}.Network_NYtoSF()
func BenchmarkNetworkSlowAddCat1MB(b *testing.B) { benchmarkAddCat(network.Megabytes(1), b) }
func BenchmarkNetworkSlowAddCat2MB(b *testing.B) { benchmarkAddCat(network.Megabytes(2), b) }
func BenchmarkNetworkSlowAddCat4MB(b *testing.B) { benchmarkAddCat(network.Megabytes(4), b) }
func BenchmarkNetworkSlowAddCat8MB(b *testing.B) { benchmarkAddCat(network.Megabytes(8), b) }
func BenchmarkNetworkSlowAddCat16MB(b *testing.B) { benchmarkAddCat(network.Megabytes(16), b) }
func BenchmarkNetworkSlowAddCat32MB(b *testing.B) { benchmarkAddCat(network.Megabytes(32), b) }
func BenchmarkNetworkSlowAddCat64MB(b *testing.B) { benchmarkAddCat(network.Megabytes(64), b) }
func BenchmarkNetworkSlowAddCat128MB(b *testing.B) { benchmarkAddCat(network.Megabytes(128), b) }
func BenchmarkNetworkSlowAddCat256MB(b *testing.B) { benchmarkAddCat(network.Megabytes(256), b) }
var blockstore = Config{}.Blockstore_7200RPM()
func BenchmarkBlockstoreSlowAddCat1MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(1), b) }
func BenchmarkBlockstoreSlowAddCat2MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(2), b) }
func BenchmarkBlockstoreSlowAddCat4MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(4), b) }
func BenchmarkBlockstoreSlowAddCat8MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(8), b) }
func BenchmarkBlockstoreSlowAddCat16MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(16), b) }
func BenchmarkBlockstoreSlowAddCat32MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(32), b) }
func BenchmarkBlockstoreSlowAddCat64MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(64), b) }
func BenchmarkBlockstoreSlowAddCat128MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(128), b) }
func BenchmarkBlockstoreSlowAddCat256MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(256), b) }
package epictest
import "time"
type Config struct {
BlockstoreLatency time.Duration
NetworkLatency time.Duration
RoutingLatency time.Duration
DataAmountBytes int64
}
func (c Config) All_Instantaneous() Config {
// Could use a zero value but whatever. Consistency of interface
c.NetworkLatency = 0
c.RoutingLatency = 0
c.BlockstoreLatency = 0
return c
}
func (c Config) Network_NYtoSF() Config {
c.NetworkLatency = 20 * time.Millisecond
return c
}
func (c Config) Network_IntraDatacenter2014() Config {
c.NetworkLatency = 250 * time.Microsecond
return c
}
func (c Config) Blockstore_FastSSD2014() Config {
const iops = 100000
c.BlockstoreLatency = (1 / iops) * time.Second
return c
}
func (c Config) Blockstore_SlowSSD2014() Config {
c.BlockstoreLatency = 150 * time.Microsecond
return c
}
func (c Config) Blockstore_7200RPM() Config {
c.BlockstoreLatency = 8 * time.Millisecond
return c
}
func (c Config) Routing_Slow() Config {
c.BlockstoreLatency = 200 * time.Millisecond
return c
}
// Megabytes is a convenience method to set DataAmountBytes
func (c Config) Megabytes(mb int64) Config {
c.DataAmountBytes = mb * 1024 * 1024
return c
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论