diff --git a/test/epictest/addcat_test.go b/test/epictest/addcat_test.go index ff6e899db..d214f0bab 100644 --- a/test/epictest/addcat_test.go +++ b/test/epictest/addcat_test.go @@ -13,12 +13,13 @@ import ( random "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random" mocknet "github.com/jbenet/go-ipfs/p2p/net/mock" errors "github.com/jbenet/go-ipfs/util/debugerror" + testutil "github.com/jbenet/go-ipfs/util/testutil" ) const kSeed = 1 func Test1KBInstantaneous(t *testing.T) { - conf := Config{ + conf := testutil.LatencyConfig{ NetworkLatency: 0, RoutingLatency: 0, BlockstoreLatency: 0, @@ -31,7 +32,7 @@ func Test1KBInstantaneous(t *testing.T) { func TestDegenerateSlowBlockstore(t *testing.T) { SkipUnlessEpic(t) - conf := Config{BlockstoreLatency: 50 * time.Millisecond} + conf := testutil.LatencyConfig{BlockstoreLatency: 50 * time.Millisecond} if err := AddCatPowers(conf, 128); err != nil { t.Fatal(err) } @@ -39,7 +40,7 @@ func TestDegenerateSlowBlockstore(t *testing.T) { func TestDegenerateSlowNetwork(t *testing.T) { SkipUnlessEpic(t) - conf := Config{NetworkLatency: 400 * time.Millisecond} + conf := testutil.LatencyConfig{NetworkLatency: 400 * time.Millisecond} if err := AddCatPowers(conf, 128); err != nil { t.Fatal(err) } @@ -47,7 +48,7 @@ func TestDegenerateSlowNetwork(t *testing.T) { func TestDegenerateSlowRouting(t *testing.T) { SkipUnlessEpic(t) - conf := Config{RoutingLatency: 400 * time.Millisecond} + conf := testutil.LatencyConfig{RoutingLatency: 400 * time.Millisecond} if err := AddCatPowers(conf, 128); err != nil { t.Fatal(err) } @@ -55,13 +56,13 @@ func TestDegenerateSlowRouting(t *testing.T) { func Test100MBMacbookCoastToCoast(t *testing.T) { SkipUnlessEpic(t) - conf := Config{}.Network_NYtoSF().Blockstore_SlowSSD2014().Routing_Slow() + conf := testutil.LatencyConfig{}.Network_NYtoSF().Blockstore_SlowSSD2014().Routing_Slow() if err := DirectAddCat(RandomBytes(100*1024*1024), conf); err != nil { t.Fatal(err) } } -func AddCatPowers(conf Config, megabytesMax int64) error { +func AddCatPowers(conf testutil.LatencyConfig, megabytesMax int64) error { var i int64 for i = 1; i < megabytesMax; i = i * 2 { fmt.Printf("%d MB\n", i) @@ -78,7 +79,7 @@ func RandomBytes(n int64) []byte { return data.Bytes() } -func DirectAddCat(data []byte, conf Config) error { +func DirectAddCat(data []byte, conf testutil.LatencyConfig) error { ctx, cancel := context.WithCancel(context.Background()) defer cancel() const numPeers = 2 diff --git a/test/epictest/bench_test.go b/test/epictest/bench_test.go index 323ad6265..72c82871b 100644 --- a/test/epictest/bench_test.go +++ b/test/epictest/bench_test.go @@ -1,8 +1,12 @@ package epictest -import "testing" +import ( + "testing" -func benchmarkAddCat(numBytes int64, conf Config, b *testing.B) { + testutil "github.com/jbenet/go-ipfs/util/testutil" +) + +func benchmarkAddCat(numBytes int64, conf testutil.LatencyConfig, b *testing.B) { b.StopTimer() b.SetBytes(numBytes) @@ -16,7 +20,7 @@ func benchmarkAddCat(numBytes int64, conf Config, b *testing.B) { } } -var instant = Config{}.All_Instantaneous() +var instant = testutil.LatencyConfig{}.All_Instantaneous() func BenchmarkInstantaneousAddCat1KB(b *testing.B) { benchmarkAddCat(1*KB, instant, b) } func BenchmarkInstantaneousAddCat1MB(b *testing.B) { benchmarkAddCat(1*MB, instant, b) } @@ -29,7 +33,7 @@ func BenchmarkInstantaneousAddCat64MB(b *testing.B) { benchmarkAddCat(64*MB, in func BenchmarkInstantaneousAddCat128MB(b *testing.B) { benchmarkAddCat(128*MB, instant, b) } func BenchmarkInstantaneousAddCat256MB(b *testing.B) { benchmarkAddCat(256*MB, instant, b) } -var routing = Config{}.Routing_Slow() +var routing = testutil.LatencyConfig{}.Routing_Slow() func BenchmarkRoutingSlowAddCat1MB(b *testing.B) { benchmarkAddCat(1*MB, routing, b) } func BenchmarkRoutingSlowAddCat2MB(b *testing.B) { benchmarkAddCat(2*MB, routing, b) } @@ -42,7 +46,7 @@ func BenchmarkRoutingSlowAddCat128MB(b *testing.B) { benchmarkAddCat(128*MB, rou func BenchmarkRoutingSlowAddCat256MB(b *testing.B) { benchmarkAddCat(256*MB, routing, b) } func BenchmarkRoutingSlowAddCat512MB(b *testing.B) { benchmarkAddCat(512*MB, routing, b) } -var network = Config{}.Network_NYtoSF() +var network = testutil.LatencyConfig{}.Network_NYtoSF() func BenchmarkNetworkSlowAddCat1MB(b *testing.B) { benchmarkAddCat(1*MB, network, b) } func BenchmarkNetworkSlowAddCat2MB(b *testing.B) { benchmarkAddCat(2*MB, network, b) } @@ -54,7 +58,7 @@ func BenchmarkNetworkSlowAddCat64MB(b *testing.B) { benchmarkAddCat(64*MB, netw func BenchmarkNetworkSlowAddCat128MB(b *testing.B) { benchmarkAddCat(128*MB, network, b) } func BenchmarkNetworkSlowAddCat256MB(b *testing.B) { benchmarkAddCat(256*MB, network, b) } -var hdd = Config{}.Blockstore_7200RPM() +var hdd = testutil.LatencyConfig{}.Blockstore_7200RPM() func BenchmarkBlockstoreSlowAddCat1MB(b *testing.B) { benchmarkAddCat(1*MB, hdd, b) } func BenchmarkBlockstoreSlowAddCat2MB(b *testing.B) { benchmarkAddCat(2*MB, hdd, b) } @@ -66,7 +70,7 @@ func BenchmarkBlockstoreSlowAddCat64MB(b *testing.B) { benchmarkAddCat(64*MB, h func BenchmarkBlockstoreSlowAddCat128MB(b *testing.B) { benchmarkAddCat(128*MB, hdd, b) } func BenchmarkBlockstoreSlowAddCat256MB(b *testing.B) { benchmarkAddCat(256*MB, hdd, b) } -var mixed = Config{}.Network_NYtoSF().Blockstore_SlowSSD2014().Routing_Slow() +var mixed = testutil.LatencyConfig{}.Network_NYtoSF().Blockstore_SlowSSD2014().Routing_Slow() func BenchmarkMixedAddCat1MBXX(b *testing.B) { benchmarkAddCat(1*MB, mixed, b) } func BenchmarkMixedAddCat2MBXX(b *testing.B) { benchmarkAddCat(2*MB, mixed, b) } diff --git a/test/epictest/core.go b/test/epictest/core.go index e89db1522..4493e3350 100644 --- a/test/epictest/core.go +++ b/test/epictest/core.go @@ -9,6 +9,7 @@ import ( blockstore "github.com/jbenet/go-ipfs/blocks/blockstore" blockservice "github.com/jbenet/go-ipfs/blockservice" + testutil "github.com/jbenet/go-ipfs/util/testutil" exchange "github.com/jbenet/go-ipfs/exchange" bitswap "github.com/jbenet/go-ipfs/exchange/bitswap" bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network" @@ -130,7 +131,7 @@ func (r *repo) Exchange() exchange.Interface { return r.exchange } -func MocknetTestRepo(p peer.ID, h host.Host, conf Config) RepoFactory { +func MocknetTestRepo(p peer.ID, h host.Host, conf testutil.LatencyConfig) RepoFactory { return func(ctx context.Context) (Repo, error) { const kWriteCacheElems = 100 const alwaysSendToPeer = true diff --git a/test/epictest/test_config.go b/test/epictest/test_config.go index e3d34110e..619ed8ae4 100644 --- a/test/epictest/test_config.go +++ b/test/epictest/test_config.go @@ -1,48 +1 @@ package epictest - -import "time" - -type Config struct { - BlockstoreLatency time.Duration - NetworkLatency time.Duration - RoutingLatency time.Duration -} - -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.RoutingLatency = 200 * time.Millisecond - return c -} diff --git a/test/epictest/three_legged_cat_test.go b/test/epictest/three_legged_cat_test.go index 38147e426..7ce396a34 100644 --- a/test/epictest/three_legged_cat_test.go +++ b/test/epictest/three_legged_cat_test.go @@ -9,10 +9,11 @@ import ( context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" mocknet "github.com/jbenet/go-ipfs/p2p/net/mock" errors "github.com/jbenet/go-ipfs/util/debugerror" + testutil "github.com/jbenet/go-ipfs/util/testutil" ) func TestThreeLeggedCat(t *testing.T) { - conf := Config{ + conf := testutil.LatencyConfig{ NetworkLatency: 0, RoutingLatency: 0, BlockstoreLatency: 0, @@ -22,7 +23,7 @@ func TestThreeLeggedCat(t *testing.T) { } } -func RunThreeLeggedCat(data []byte, conf Config) error { +func RunThreeLeggedCat(data []byte, conf testutil.LatencyConfig) error { ctx, cancel := context.WithCancel(context.Background()) defer cancel() const numPeers = 3 diff --git a/util/testutil/latency_config.go b/util/testutil/latency_config.go new file mode 100644 index 000000000..767b9a638 --- /dev/null +++ b/util/testutil/latency_config.go @@ -0,0 +1,48 @@ +package testutil + +import "time" + +type LatencyConfig struct { + BlockstoreLatency time.Duration + NetworkLatency time.Duration + RoutingLatency time.Duration +} + +func (c LatencyConfig) All_Instantaneous() LatencyConfig { + // Could use a zero value but whatever. Consistency of interface + c.NetworkLatency = 0 + c.RoutingLatency = 0 + c.BlockstoreLatency = 0 + return c +} + +func (c LatencyConfig) Network_NYtoSF() LatencyConfig { + c.NetworkLatency = 20 * time.Millisecond + return c +} + +func (c LatencyConfig) Network_IntraDatacenter2014() LatencyConfig { + c.NetworkLatency = 250 * time.Microsecond + return c +} + +func (c LatencyConfig) Blockstore_FastSSD2014() LatencyConfig { + const iops = 100000 + c.BlockstoreLatency = (1 / iops) * time.Second + return c +} + +func (c LatencyConfig) Blockstore_SlowSSD2014() LatencyConfig { + c.BlockstoreLatency = 150 * time.Microsecond + return c +} + +func (c LatencyConfig) Blockstore_7200RPM() LatencyConfig { + c.BlockstoreLatency = 8 * time.Millisecond + return c +} + +func (c LatencyConfig) Routing_Slow() LatencyConfig { + c.RoutingLatency = 200 * time.Millisecond + return c +}