refactor: move LatencyConfig
Brian Tiger Chow committed
Jan 10, 2015 at 16:45 UTC
007ffd40bd439451193f9084687286e741eff8bf
6 files changed
+72
-64
test/epictest/addcat_test.go
+8
-7
@@ -13,12 +13,13 @@ import (
13
random "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random"
14
mocknet "github.com/jbenet/go-ipfs/p2p/net/mock"
15
errors "github.com/jbenet/go-ipfs/util/debugerror"
16
+ testutil "github.com/jbenet/go-ipfs/util/testutil"
17
)
18
19
const kSeed = 1
20
21
func Test1KBInstantaneous(t *testing.T) {
21
- conf := Config{
22
+ conf := testutil.LatencyConfig{
23
NetworkLatency: 0,
24
RoutingLatency: 0,
25
BlockstoreLatency: 0,
@@ -31,7 +32,7 @@ func Test1KBInstantaneous(t *testing.T) {
32
33
func TestDegenerateSlowBlockstore(t *testing.T) {
34
SkipUnlessEpic(t)
34
- conf := Config{BlockstoreLatency: 50 * time.Millisecond}
35
+ conf := testutil.LatencyConfig{BlockstoreLatency: 50 * time.Millisecond}
36
if err := AddCatPowers(conf, 128); err != nil {
37
t.Fatal(err)
38
}
@@ -39,7 +40,7 @@ func TestDegenerateSlowBlockstore(t *testing.T) {
40
41
func TestDegenerateSlowNetwork(t *testing.T) {
42
SkipUnlessEpic(t)
42
- conf := Config{NetworkLatency: 400 * time.Millisecond}
43
+ conf := testutil.LatencyConfig{NetworkLatency: 400 * time.Millisecond}
44
if err := AddCatPowers(conf, 128); err != nil {
45
t.Fatal(err)
46
}
@@ -47,7 +48,7 @@ func TestDegenerateSlowNetwork(t *testing.T) {
48
49
func TestDegenerateSlowRouting(t *testing.T) {
50
SkipUnlessEpic(t)
50
- conf := Config{RoutingLatency: 400 * time.Millisecond}
51
+ conf := testutil.LatencyConfig{RoutingLatency: 400 * time.Millisecond}
52
if err := AddCatPowers(conf, 128); err != nil {
53
t.Fatal(err)
54
}
@@ -55,13 +56,13 @@ func TestDegenerateSlowRouting(t *testing.T) {
56
57
func Test100MBMacbookCoastToCoast(t *testing.T) {
58
SkipUnlessEpic(t)
58
- conf := Config{}.Network_NYtoSF().Blockstore_SlowSSD2014().Routing_Slow()
59
+ conf := testutil.LatencyConfig{}.Network_NYtoSF().Blockstore_SlowSSD2014().Routing_Slow()
60
if err := DirectAddCat(RandomBytes(100*1024*1024), conf); err != nil {
61
t.Fatal(err)
62
}
63
}
64
64
-func AddCatPowers(conf Config, megabytesMax int64) error {
65
+func AddCatPowers(conf testutil.LatencyConfig, megabytesMax int64) error {
66
var i int64
67
for i = 1; i < megabytesMax; i = i * 2 {
68
fmt.Printf("%d MB\n", i)
@@ -78,7 +79,7 @@ func RandomBytes(n int64) []byte {
79
return data.Bytes()
80
}
81
81
-func DirectAddCat(data []byte, conf Config) error {
82
+func DirectAddCat(data []byte, conf testutil.LatencyConfig) error {
83
ctx, cancel := context.WithCancel(context.Background())
84
defer cancel()
85
const numPeers = 2
test/epictest/bench_test.go
+11
-7
@@ -1,8 +1,12 @@
1
package epictest
2
3
-import "testing"
3
+import (
4
+ "testing"
5
5
-func benchmarkAddCat(numBytes int64, conf Config, b *testing.B) {
6
+ testutil "github.com/jbenet/go-ipfs/util/testutil"
7
+)
8
+
9
+func benchmarkAddCat(numBytes int64, conf testutil.LatencyConfig, b *testing.B) {
10
11
b.StopTimer()
12
b.SetBytes(numBytes)
@@ -16,7 +20,7 @@ func benchmarkAddCat(numBytes int64, conf Config, b *testing.B) {
20
}
21
}
22
19
-var instant = Config{}.All_Instantaneous()
23
+var instant = testutil.LatencyConfig{}.All_Instantaneous()
24
25
func BenchmarkInstantaneousAddCat1KB(b *testing.B) { benchmarkAddCat(1*KB, instant, b) }
26
func BenchmarkInstantaneousAddCat1MB(b *testing.B) { benchmarkAddCat(1*MB, instant, b) }
@@ -29,7 +33,7 @@ func BenchmarkInstantaneousAddCat64MB(b *testing.B) { benchmarkAddCat(64*MB, in
33
func BenchmarkInstantaneousAddCat128MB(b *testing.B) { benchmarkAddCat(128*MB, instant, b) }
34
func BenchmarkInstantaneousAddCat256MB(b *testing.B) { benchmarkAddCat(256*MB, instant, b) }
35
32
-var routing = Config{}.Routing_Slow()
36
+var routing = testutil.LatencyConfig{}.Routing_Slow()
37
38
func BenchmarkRoutingSlowAddCat1MB(b *testing.B) { benchmarkAddCat(1*MB, routing, b) }
39
func BenchmarkRoutingSlowAddCat2MB(b *testing.B) { benchmarkAddCat(2*MB, routing, b) }
@@ -42,7 +46,7 @@ func BenchmarkRoutingSlowAddCat128MB(b *testing.B) { benchmarkAddCat(128*MB, rou
46
func BenchmarkRoutingSlowAddCat256MB(b *testing.B) { benchmarkAddCat(256*MB, routing, b) }
47
func BenchmarkRoutingSlowAddCat512MB(b *testing.B) { benchmarkAddCat(512*MB, routing, b) }
48
45
-var network = Config{}.Network_NYtoSF()
49
+var network = testutil.LatencyConfig{}.Network_NYtoSF()
50
51
func BenchmarkNetworkSlowAddCat1MB(b *testing.B) { benchmarkAddCat(1*MB, network, b) }
52
func BenchmarkNetworkSlowAddCat2MB(b *testing.B) { benchmarkAddCat(2*MB, network, b) }
@@ -54,7 +58,7 @@ func BenchmarkNetworkSlowAddCat64MB(b *testing.B) { benchmarkAddCat(64*MB, netw
58
func BenchmarkNetworkSlowAddCat128MB(b *testing.B) { benchmarkAddCat(128*MB, network, b) }
59
func BenchmarkNetworkSlowAddCat256MB(b *testing.B) { benchmarkAddCat(256*MB, network, b) }
60
57
-var hdd = Config{}.Blockstore_7200RPM()
61
+var hdd = testutil.LatencyConfig{}.Blockstore_7200RPM()
62
63
func BenchmarkBlockstoreSlowAddCat1MB(b *testing.B) { benchmarkAddCat(1*MB, hdd, b) }
64
func BenchmarkBlockstoreSlowAddCat2MB(b *testing.B) { benchmarkAddCat(2*MB, hdd, b) }
@@ -66,7 +70,7 @@ func BenchmarkBlockstoreSlowAddCat64MB(b *testing.B) { benchmarkAddCat(64*MB, h
70
func BenchmarkBlockstoreSlowAddCat128MB(b *testing.B) { benchmarkAddCat(128*MB, hdd, b) }
71
func BenchmarkBlockstoreSlowAddCat256MB(b *testing.B) { benchmarkAddCat(256*MB, hdd, b) }
72
69
-var mixed = Config{}.Network_NYtoSF().Blockstore_SlowSSD2014().Routing_Slow()
73
+var mixed = testutil.LatencyConfig{}.Network_NYtoSF().Blockstore_SlowSSD2014().Routing_Slow()
74
75
func BenchmarkMixedAddCat1MBXX(b *testing.B) { benchmarkAddCat(1*MB, mixed, b) }
76
func BenchmarkMixedAddCat2MBXX(b *testing.B) { benchmarkAddCat(2*MB, mixed, b) }
test/epictest/core.go
+2
-1
@@ -9,6 +9,7 @@ import (
9
10
blockstore "github.com/jbenet/go-ipfs/blocks/blockstore"
11
blockservice "github.com/jbenet/go-ipfs/blockservice"
12
+ testutil "github.com/jbenet/go-ipfs/util/testutil"
13
exchange "github.com/jbenet/go-ipfs/exchange"
14
bitswap "github.com/jbenet/go-ipfs/exchange/bitswap"
15
bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
@@ -130,7 +131,7 @@ func (r *repo) Exchange() exchange.Interface {
131
return r.exchange
132
}
133
133
-func MocknetTestRepo(p peer.ID, h host.Host, conf Config) RepoFactory {
134
+func MocknetTestRepo(p peer.ID, h host.Host, conf testutil.LatencyConfig) RepoFactory {
135
return func(ctx context.Context) (Repo, error) {
136
const kWriteCacheElems = 100
137
const alwaysSendToPeer = true
test/epictest/test_config.go
-47
@@ -1,48 +1 @@
1
package epictest
2
-
3
-import "time"
4
-
5
-type Config struct {
6
- BlockstoreLatency time.Duration
7
- NetworkLatency time.Duration
8
- RoutingLatency time.Duration
9
-}
10
-
11
-func (c Config) All_Instantaneous() Config {
12
- // Could use a zero value but whatever. Consistency of interface
13
- c.NetworkLatency = 0
14
- c.RoutingLatency = 0
15
- c.BlockstoreLatency = 0
16
- return c
17
-}
18
-
19
-func (c Config) Network_NYtoSF() Config {
20
- c.NetworkLatency = 20 * time.Millisecond
21
- return c
22
-}
23
-
24
-func (c Config) Network_IntraDatacenter2014() Config {
25
- c.NetworkLatency = 250 * time.Microsecond
26
- return c
27
-}
28
-
29
-func (c Config) Blockstore_FastSSD2014() Config {
30
- const iops = 100000
31
- c.BlockstoreLatency = (1 / iops) * time.Second
32
- return c
33
-}
34
-
35
-func (c Config) Blockstore_SlowSSD2014() Config {
36
- c.BlockstoreLatency = 150 * time.Microsecond
37
- return c
38
-}
39
-
40
-func (c Config) Blockstore_7200RPM() Config {
41
- c.BlockstoreLatency = 8 * time.Millisecond
42
- return c
43
-}
44
-
45
-func (c Config) Routing_Slow() Config {
46
- c.RoutingLatency = 200 * time.Millisecond
47
- return c
48
-}
test/epictest/three_legged_cat_test.go
+3
-2
@@ -9,10 +9,11 @@ import (
9
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
10
mocknet "github.com/jbenet/go-ipfs/p2p/net/mock"
11
errors "github.com/jbenet/go-ipfs/util/debugerror"
12
+ testutil "github.com/jbenet/go-ipfs/util/testutil"
13
)
14
15
func TestThreeLeggedCat(t *testing.T) {
15
- conf := Config{
16
+ conf := testutil.LatencyConfig{
17
NetworkLatency: 0,
18
RoutingLatency: 0,
19
BlockstoreLatency: 0,
@@ -22,7 +23,7 @@ func TestThreeLeggedCat(t *testing.T) {
23
}
24
}
25
25
-func RunThreeLeggedCat(data []byte, conf Config) error {
26
+func RunThreeLeggedCat(data []byte, conf testutil.LatencyConfig) error {
27
ctx, cancel := context.WithCancel(context.Background())
28
defer cancel()
29
const numPeers = 3
util/testutil/latency_config.go
new
+48
@@ -0,0 +1,48 @@
1
+package testutil
2
+
3
+import "time"
4
+
5
+type LatencyConfig struct {
6
+ BlockstoreLatency time.Duration
7
+ NetworkLatency time.Duration
8
+ RoutingLatency time.Duration
9
+}
10
+
11
+func (c LatencyConfig) All_Instantaneous() LatencyConfig {
12
+ // Could use a zero value but whatever. Consistency of interface
13
+ c.NetworkLatency = 0
14
+ c.RoutingLatency = 0
15
+ c.BlockstoreLatency = 0
16
+ return c
17
+}
18
+
19
+func (c LatencyConfig) Network_NYtoSF() LatencyConfig {
20
+ c.NetworkLatency = 20 * time.Millisecond
21
+ return c
22
+}
23
+
24
+func (c LatencyConfig) Network_IntraDatacenter2014() LatencyConfig {
25
+ c.NetworkLatency = 250 * time.Microsecond
26
+ return c
27
+}
28
+
29
+func (c LatencyConfig) Blockstore_FastSSD2014() LatencyConfig {
30
+ const iops = 100000
31
+ c.BlockstoreLatency = (1 / iops) * time.Second
32
+ return c
33
+}
34
+
35
+func (c LatencyConfig) Blockstore_SlowSSD2014() LatencyConfig {
36
+ c.BlockstoreLatency = 150 * time.Microsecond
37
+ return c
38
+}
39
+
40
+func (c LatencyConfig) Blockstore_7200RPM() LatencyConfig {
41
+ c.BlockstoreLatency = 8 * time.Millisecond
42
+ return c
43
+}
44
+
45
+func (c LatencyConfig) Routing_Slow() LatencyConfig {
46
+ c.RoutingLatency = 200 * time.Millisecond
47
+ return c
48
+}