@cryptotaxi247 / kubo / commits / e47eaed41

fix(epictest) generate data before starting benchmark

License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>

Brian Tiger Chow committed Dec 14, 2014 at 18:10 UTC e47eaed41976856354f653b1b9336c04525bea89
5 files changed +92 -57
epictest/addcat_test.go
+14 -14
@@ -8,6 +8,7 @@ import (
8 "testing"
9 "time"
10
11 + "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random"
12 blockservice "github.com/jbenet/go-ipfs/blockservice"
13 bitswap "github.com/jbenet/go-ipfs/exchange/bitswap"
14 tn "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
@@ -20,7 +21,6 @@ import (
21 util "github.com/jbenet/go-ipfs/util"
22 errors "github.com/jbenet/go-ipfs/util/debugerror"
23 delay "github.com/jbenet/go-ipfs/util/delay"
23 - "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random"
24 )
25
26 const kSeed = 1
@@ -34,10 +34,9 @@ func Test100MBInstantaneous(t *testing.T) {
34 NetworkLatency: 0,
35 RoutingLatency: 0,
36 BlockstoreLatency: 0,
37 - DataAmountBytes: 100 * 1024 * 1024,
37 }
38
40 - AddCatBytes(conf)
39 + AddCatBytes(RandomBytes(100*1024*1024), conf)
40 }
41
42 func TestDegenerateSlowBlockstore(t *testing.T) {
@@ -77,11 +76,9 @@ func Test100MBMacbookCoastToCoast(t *testing.T) {
76 SkipUnlessEpic(t)
77 t.Parallel()
78
80 - conf := Config{
81 - DataAmountBytes: 100 * 1024 * 1024,
82 - }.Network_NYtoSF().Blockstore_SlowSSD2014().Routing_Slow()
79 + conf := Config{}.Network_NYtoSF().Blockstore_SlowSSD2014().Routing_Slow()
80
84 - if err := AddCatBytes(conf); err != nil {
81 + if err := AddCatBytes(RandomBytes(100*1024*1024), conf); err != nil {
82 t.Fatal(err)
83 }
84 }
@@ -90,15 +87,20 @@ func AddCatPowers(conf Config, megabytesMax int64) error {
87 var i int64
88 for i = 1; i < megabytesMax; i = i * 2 {
89 fmt.Printf("%d MB\n", i)
93 - conf.DataAmountBytes = i * 1024 * 1024
94 - if err := AddCatBytes(conf); err != nil {
90 + if err := AddCatBytes(RandomBytes(i*1024*1024), conf); err != nil {
91 return err
92 }
93 }
94 return nil
95 }
96
101 -func AddCatBytes(conf Config) error {
97 +func RandomBytes(n int64) []byte {
98 + var data bytes.Buffer
99 + random.WritePseudoRandomBytes(n, &data, kSeed)
100 + return data.Bytes()
101 +}
102 +
103 +func AddCatBytes(data []byte, conf Config) error {
104
105 sessionGenerator := bitswap.NewSessionGenerator(
106 tn.VirtualNetwork(delay.Fixed(conf.NetworkLatency)), // TODO rename VirtualNetwork
@@ -111,9 +113,7 @@ func AddCatBytes(conf Config) error {
113 catter.SetBlockstoreLatency(conf.BlockstoreLatency)
114
115 adder.SetBlockstoreLatency(0) // disable blockstore latency during add operation
114 - var data bytes.Buffer
115 - random.WritePseudoRandomBytes(conf.DataAmountBytes, &data, kSeed) // FIXME get a lazy reader
116 - keyAdded, err := add(adder, bytes.NewReader(data.Bytes()))
116 + keyAdded, err := add(adder, bytes.NewReader(data))
117 if err != nil {
118 return err
119 }
@@ -127,7 +127,7 @@ func AddCatBytes(conf Config) error {
127 // verify
128 var bufout bytes.Buffer
129 io.Copy(&bufout, readerCatted)
130 - if 0 != bytes.Compare(bufout.Bytes(), data.Bytes()) {
130 + if 0 != bytes.Compare(bufout.Bytes(), data) {
131 return errors.New("catted data does not match added data")
132 }
133 return nil
epictest/bench_test.go
+41 -36
@@ -2,10 +2,15 @@ package epictest
2
3 import "testing"
4
5 -func benchmarkAddCat(conf Config, b *testing.B) {
6 - b.SetBytes(conf.DataAmountBytes)
5 +func benchmarkAddCat(numBytes int64, conf Config, b *testing.B) {
6 +
7 + b.StopTimer()
8 + b.SetBytes(numBytes)
9 + data := RandomBytes(numBytes) // we don't want to measure the time it takes to generate this data
10 + b.StartTimer()
11 +
12 for n := 0; n < b.N; n++ {
8 - if err := AddCatBytes(conf); err != nil {
13 + if err := AddCatBytes(data, conf); err != nil {
14 b.Fatal(err)
15 }
16 }
@@ -13,45 +18,45 @@ func benchmarkAddCat(conf Config, b *testing.B) {
18
19 var instant = Config{}.All_Instantaneous()
20
16 -func BenchmarkInstantaneousAddCat1MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(1), b) }
17 -func BenchmarkInstantaneousAddCat2MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(2), b) }
18 -func BenchmarkInstantaneousAddCat4MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(4), b) }
19 -func BenchmarkInstantaneousAddCat8MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(8), b) }
20 -func BenchmarkInstantaneousAddCat16MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(16), b) }
21 -func BenchmarkInstantaneousAddCat32MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(32), b) }
22 -func BenchmarkInstantaneousAddCat64MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(64), b) }
23 -func BenchmarkInstantaneousAddCat128MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(128), b) }
24 -func BenchmarkInstantaneousAddCat256MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(256), b) }
21 +func BenchmarkInstantaneousAddCat1MB(b *testing.B) { benchmarkAddCat(1*MB, instant, b) }
22 +func BenchmarkInstantaneousAddCat2MB(b *testing.B) { benchmarkAddCat(2*MB, instant, b) }
23 +func BenchmarkInstantaneousAddCat4MB(b *testing.B) { benchmarkAddCat(4*MB, instant, b) }
24 +func BenchmarkInstantaneousAddCat8MB(b *testing.B) { benchmarkAddCat(8*MB, instant, b) }
25 +func BenchmarkInstantaneousAddCat16MB(b *testing.B) { benchmarkAddCat(16*MB, instant, b) }
26 +func BenchmarkInstantaneousAddCat32MB(b *testing.B) { benchmarkAddCat(32*MB, instant, b) }
27 +func BenchmarkInstantaneousAddCat64MB(b *testing.B) { benchmarkAddCat(64*MB, instant, b) }
28 +func BenchmarkInstantaneousAddCat128MB(b *testing.B) { benchmarkAddCat(128*MB, instant, b) }
29 +func BenchmarkInstantaneousAddCat256MB(b *testing.B) { benchmarkAddCat(256*MB, instant, b) }
30
31 var routing = Config{}.Routing_Slow()
32
28 -func BenchmarkRoutingSlowAddCat1MB(b *testing.B) { benchmarkAddCat(routing.Megabytes(1), b) }
29 -func BenchmarkRoutingSlowAddCat2MB(b *testing.B) { benchmarkAddCat(routing.Megabytes(2), b) }
30 -func BenchmarkRoutingSlowAddCat4MB(b *testing.B) { benchmarkAddCat(routing.Megabytes(4), b) }
31 -func BenchmarkRoutingSlowAddCat8MB(b *testing.B) { benchmarkAddCat(routing.Megabytes(8), b) }
32 -func BenchmarkRoutingSlowAddCat16MB(b *testing.B) { benchmarkAddCat(routing.Megabytes(16), b) }
33 -func BenchmarkRoutingSlowAddCat32MB(b *testing.B) { benchmarkAddCat(routing.Megabytes(32), b) }
33 +func BenchmarkRoutingSlowAddCat1MB(b *testing.B) { benchmarkAddCat(1*MB, routing, b) }
34 +func BenchmarkRoutingSlowAddCat2MB(b *testing.B) { benchmarkAddCat(2*MB, routing, b) }
35 +func BenchmarkRoutingSlowAddCat4MB(b *testing.B) { benchmarkAddCat(4*MB, routing, b) }
36 +func BenchmarkRoutingSlowAddCat8MB(b *testing.B) { benchmarkAddCat(8*MB, routing, b) }
37 +func BenchmarkRoutingSlowAddCat16MB(b *testing.B) { benchmarkAddCat(16*MB, routing, b) }
38 +func BenchmarkRoutingSlowAddCat32MB(b *testing.B) { benchmarkAddCat(32*MB, routing, b) }
39
40 var network = Config{}.Network_NYtoSF()
41
37 -func BenchmarkNetworkSlowAddCat1MB(b *testing.B) { benchmarkAddCat(network.Megabytes(1), b) }
38 -func BenchmarkNetworkSlowAddCat2MB(b *testing.B) { benchmarkAddCat(network.Megabytes(2), b) }
39 -func BenchmarkNetworkSlowAddCat4MB(b *testing.B) { benchmarkAddCat(network.Megabytes(4), b) }
40 -func BenchmarkNetworkSlowAddCat8MB(b *testing.B) { benchmarkAddCat(network.Megabytes(8), b) }
41 -func BenchmarkNetworkSlowAddCat16MB(b *testing.B) { benchmarkAddCat(network.Megabytes(16), b) }
42 -func BenchmarkNetworkSlowAddCat32MB(b *testing.B) { benchmarkAddCat(network.Megabytes(32), b) }
43 -func BenchmarkNetworkSlowAddCat64MB(b *testing.B) { benchmarkAddCat(network.Megabytes(64), b) }
44 -func BenchmarkNetworkSlowAddCat128MB(b *testing.B) { benchmarkAddCat(network.Megabytes(128), b) }
45 -func BenchmarkNetworkSlowAddCat256MB(b *testing.B) { benchmarkAddCat(network.Megabytes(256), b) }
42 +func BenchmarkNetworkSlowAddCat1MB(b *testing.B) { benchmarkAddCat(1*MB, network, b) }
43 +func BenchmarkNetworkSlowAddCat2MB(b *testing.B) { benchmarkAddCat(2*MB, network, b) }
44 +func BenchmarkNetworkSlowAddCat4MB(b *testing.B) { benchmarkAddCat(4*MB, network, b) }
45 +func BenchmarkNetworkSlowAddCat8MB(b *testing.B) { benchmarkAddCat(8*MB, network, b) }
46 +func BenchmarkNetworkSlowAddCat16MB(b *testing.B) { benchmarkAddCat(16*MB, network, b) }
47 +func BenchmarkNetworkSlowAddCat32MB(b *testing.B) { benchmarkAddCat(32*MB, network, b) }
48 +func BenchmarkNetworkSlowAddCat64MB(b *testing.B) { benchmarkAddCat(64*MB, network, b) }
49 +func BenchmarkNetworkSlowAddCat128MB(b *testing.B) { benchmarkAddCat(128*MB, network, b) }
50 +func BenchmarkNetworkSlowAddCat256MB(b *testing.B) { benchmarkAddCat(256*MB, network, b) }
51
52 var blockstore = Config{}.Blockstore_7200RPM()
53
49 -func BenchmarkBlockstoreSlowAddCat1MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(1), b) }
50 -func BenchmarkBlockstoreSlowAddCat2MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(2), b) }
51 -func BenchmarkBlockstoreSlowAddCat4MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(4), b) }
52 -func BenchmarkBlockstoreSlowAddCat8MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(8), b) }
53 -func BenchmarkBlockstoreSlowAddCat16MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(16), b) }
54 -func BenchmarkBlockstoreSlowAddCat32MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(32), b) }
55 -func BenchmarkBlockstoreSlowAddCat64MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(64), b) }
56 -func BenchmarkBlockstoreSlowAddCat128MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(128), b) }
57 -func BenchmarkBlockstoreSlowAddCat256MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(256), b) }
54 +func BenchmarkBlockstoreSlowAddCat1MB(b *testing.B) { benchmarkAddCat(1*MB, blockstore, b) }
55 +func BenchmarkBlockstoreSlowAddCat2MB(b *testing.B) { benchmarkAddCat(2*MB, blockstore, b) }
56 +func BenchmarkBlockstoreSlowAddCat4MB(b *testing.B) { benchmarkAddCat(4*MB, blockstore, b) }
57 +func BenchmarkBlockstoreSlowAddCat8MB(b *testing.B) { benchmarkAddCat(8*MB, blockstore, b) }
58 +func BenchmarkBlockstoreSlowAddCat16MB(b *testing.B) { benchmarkAddCat(16*MB, blockstore, b) }
59 +func BenchmarkBlockstoreSlowAddCat32MB(b *testing.B) { benchmarkAddCat(32*MB, blockstore, b) }
60 +func BenchmarkBlockstoreSlowAddCat64MB(b *testing.B) { benchmarkAddCat(64*MB, blockstore, b) }
61 +func BenchmarkBlockstoreSlowAddCat128MB(b *testing.B) { benchmarkAddCat(128*MB, blockstore, b) }
62 +func BenchmarkBlockstoreSlowAddCat256MB(b *testing.B) { benchmarkAddCat(256*MB, blockstore, b) }
epictest/test_config.go
-7
@@ -6,7 +6,6 @@ type Config struct {
6 BlockstoreLatency time.Duration
7 NetworkLatency time.Duration
8 RoutingLatency time.Duration
9 - DataAmountBytes int64
9 }
10
11 func (c Config) All_Instantaneous() Config {
@@ -47,9 +46,3 @@ func (c Config) Routing_Slow() Config {
46 c.BlockstoreLatency = 200 * time.Millisecond
47 return c
48 }
50 -
51 -// Megabytes is a convenience method to set DataAmountBytes
52 -func (c Config) Megabytes(mb int64) Config {
53 - c.DataAmountBytes = mb * 1024 * 1024
54 - return c
55 -}
epictest/unit.go new
+11
@@ -0,0 +1,11 @@
1 +package epictest
2 +
3 +const (
4 + _ = iota // ignore first value by assigning to blank identifier
5 + KB = 1 << (10 * iota)
6 + MB
7 + GB
8 + TB
9 + PB
10 + EB
11 +)
epictest/unit_test.go new
+26
@@ -0,0 +1,26 @@
1 +package epictest
2 +
3 +import "testing"
4 +
5 +// and the award for most meta goes to...
6 +
7 +func TestByteSizeUnit(t *testing.T) {
8 + if 1*KB != 1*1024 {
9 + t.Fatal(1 * KB)
10 + }
11 + if 1*MB != 1*1024*1024 {
12 + t.Fail()
13 + }
14 + if 1*GB != 1*1024*1024*1024 {
15 + t.Fail()
16 + }
17 + if 1*TB != 1*1024*1024*1024*1024 {
18 + t.Fail()
19 + }
20 + if 1*PB != 1*1024*1024*1024*1024*1024 {
21 + t.Fail()
22 + }
23 + if 1*EB != 1*1024*1024*1024*1024*1024*1024 {
24 + t.Fail()
25 + }
26 +}