提交 bddae0c5 作者: Brian Tiger Chow

Merge pull request #449 from jbenet/fix/benchmark-timer

fix(epictest) generate data before starting benchmark
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random"
blockservice "github.com/jbenet/go-ipfs/blockservice" blockservice "github.com/jbenet/go-ipfs/blockservice"
bitswap "github.com/jbenet/go-ipfs/exchange/bitswap" bitswap "github.com/jbenet/go-ipfs/exchange/bitswap"
tn "github.com/jbenet/go-ipfs/exchange/bitswap/testnet" tn "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
...@@ -20,7 +21,6 @@ import ( ...@@ -20,7 +21,6 @@ import (
util "github.com/jbenet/go-ipfs/util" util "github.com/jbenet/go-ipfs/util"
errors "github.com/jbenet/go-ipfs/util/debugerror" errors "github.com/jbenet/go-ipfs/util/debugerror"
delay "github.com/jbenet/go-ipfs/util/delay" delay "github.com/jbenet/go-ipfs/util/delay"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random"
) )
const kSeed = 1 const kSeed = 1
...@@ -34,10 +34,9 @@ func Test100MBInstantaneous(t *testing.T) { ...@@ -34,10 +34,9 @@ func Test100MBInstantaneous(t *testing.T) {
NetworkLatency: 0, NetworkLatency: 0,
RoutingLatency: 0, RoutingLatency: 0,
BlockstoreLatency: 0, BlockstoreLatency: 0,
DataAmountBytes: 100 * 1024 * 1024,
} }
AddCatBytes(conf) AddCatBytes(RandomBytes(100*1024*1024), conf)
} }
func TestDegenerateSlowBlockstore(t *testing.T) { func TestDegenerateSlowBlockstore(t *testing.T) {
...@@ -77,11 +76,9 @@ func Test100MBMacbookCoastToCoast(t *testing.T) { ...@@ -77,11 +76,9 @@ func Test100MBMacbookCoastToCoast(t *testing.T) {
SkipUnlessEpic(t) SkipUnlessEpic(t)
t.Parallel() t.Parallel()
conf := Config{ conf := Config{}.Network_NYtoSF().Blockstore_SlowSSD2014().Routing_Slow()
DataAmountBytes: 100 * 1024 * 1024,
}.Network_NYtoSF().Blockstore_SlowSSD2014().Routing_Slow()
if err := AddCatBytes(conf); err != nil { if err := AddCatBytes(RandomBytes(100*1024*1024), conf); err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
...@@ -90,15 +87,20 @@ func AddCatPowers(conf Config, megabytesMax int64) error { ...@@ -90,15 +87,20 @@ func AddCatPowers(conf Config, megabytesMax int64) error {
var i int64 var i int64
for i = 1; i < megabytesMax; i = i * 2 { for i = 1; i < megabytesMax; i = i * 2 {
fmt.Printf("%d MB\n", i) fmt.Printf("%d MB\n", i)
conf.DataAmountBytes = i * 1024 * 1024 if err := AddCatBytes(RandomBytes(i*1024*1024), conf); err != nil {
if err := AddCatBytes(conf); err != nil {
return err return err
} }
} }
return nil return nil
} }
func AddCatBytes(conf Config) error { func RandomBytes(n int64) []byte {
var data bytes.Buffer
random.WritePseudoRandomBytes(n, &data, kSeed)
return data.Bytes()
}
func AddCatBytes(data []byte, conf Config) error {
sessionGenerator := bitswap.NewSessionGenerator( sessionGenerator := bitswap.NewSessionGenerator(
tn.VirtualNetwork(delay.Fixed(conf.NetworkLatency)), // TODO rename VirtualNetwork tn.VirtualNetwork(delay.Fixed(conf.NetworkLatency)), // TODO rename VirtualNetwork
...@@ -111,9 +113,7 @@ func AddCatBytes(conf Config) error { ...@@ -111,9 +113,7 @@ func AddCatBytes(conf Config) error {
catter.SetBlockstoreLatency(conf.BlockstoreLatency) catter.SetBlockstoreLatency(conf.BlockstoreLatency)
adder.SetBlockstoreLatency(0) // disable blockstore latency during add operation adder.SetBlockstoreLatency(0) // disable blockstore latency during add operation
var data bytes.Buffer keyAdded, err := add(adder, bytes.NewReader(data))
random.WritePseudoRandomBytes(conf.DataAmountBytes, &data, kSeed) // FIXME get a lazy reader
keyAdded, err := add(adder, bytes.NewReader(data.Bytes()))
if err != nil { if err != nil {
return err return err
} }
...@@ -127,7 +127,7 @@ func AddCatBytes(conf Config) error { ...@@ -127,7 +127,7 @@ func AddCatBytes(conf Config) error {
// verify // verify
var bufout bytes.Buffer var bufout bytes.Buffer
io.Copy(&bufout, readerCatted) io.Copy(&bufout, readerCatted)
if 0 != bytes.Compare(bufout.Bytes(), data.Bytes()) { if 0 != bytes.Compare(bufout.Bytes(), data) {
return errors.New("catted data does not match added data") return errors.New("catted data does not match added data")
} }
return nil return nil
......
...@@ -2,10 +2,15 @@ package epictest ...@@ -2,10 +2,15 @@ package epictest
import "testing" import "testing"
func benchmarkAddCat(conf Config, b *testing.B) { func benchmarkAddCat(numBytes int64, conf Config, b *testing.B) {
b.SetBytes(conf.DataAmountBytes)
b.StopTimer()
b.SetBytes(numBytes)
data := RandomBytes(numBytes) // we don't want to measure the time it takes to generate this data
b.StartTimer()
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
if err := AddCatBytes(conf); err != nil { if err := AddCatBytes(data, conf); err != nil {
b.Fatal(err) b.Fatal(err)
} }
} }
...@@ -13,45 +18,45 @@ func benchmarkAddCat(conf Config, b *testing.B) { ...@@ -13,45 +18,45 @@ func benchmarkAddCat(conf Config, b *testing.B) {
var instant = Config{}.All_Instantaneous() var instant = Config{}.All_Instantaneous()
func BenchmarkInstantaneousAddCat1MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(1), b) } func BenchmarkInstantaneousAddCat1MB(b *testing.B) { benchmarkAddCat(1*MB, instant, b) }
func BenchmarkInstantaneousAddCat2MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(2), b) } func BenchmarkInstantaneousAddCat2MB(b *testing.B) { benchmarkAddCat(2*MB, instant, b) }
func BenchmarkInstantaneousAddCat4MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(4), b) } func BenchmarkInstantaneousAddCat4MB(b *testing.B) { benchmarkAddCat(4*MB, instant, b) }
func BenchmarkInstantaneousAddCat8MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(8), b) } func BenchmarkInstantaneousAddCat8MB(b *testing.B) { benchmarkAddCat(8*MB, instant, b) }
func BenchmarkInstantaneousAddCat16MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(16), b) } func BenchmarkInstantaneousAddCat16MB(b *testing.B) { benchmarkAddCat(16*MB, instant, b) }
func BenchmarkInstantaneousAddCat32MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(32), b) } func BenchmarkInstantaneousAddCat32MB(b *testing.B) { benchmarkAddCat(32*MB, instant, b) }
func BenchmarkInstantaneousAddCat64MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(64), b) } func BenchmarkInstantaneousAddCat64MB(b *testing.B) { benchmarkAddCat(64*MB, instant, b) }
func BenchmarkInstantaneousAddCat128MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(128), b) } func BenchmarkInstantaneousAddCat128MB(b *testing.B) { benchmarkAddCat(128*MB, instant, b) }
func BenchmarkInstantaneousAddCat256MB(b *testing.B) { benchmarkAddCat(instant.Megabytes(256), b) } func BenchmarkInstantaneousAddCat256MB(b *testing.B) { benchmarkAddCat(256*MB, instant, b) }
var routing = Config{}.Routing_Slow() var routing = Config{}.Routing_Slow()
func BenchmarkRoutingSlowAddCat1MB(b *testing.B) { benchmarkAddCat(routing.Megabytes(1), b) } func BenchmarkRoutingSlowAddCat1MB(b *testing.B) { benchmarkAddCat(1*MB, routing, b) }
func BenchmarkRoutingSlowAddCat2MB(b *testing.B) { benchmarkAddCat(routing.Megabytes(2), b) } func BenchmarkRoutingSlowAddCat2MB(b *testing.B) { benchmarkAddCat(2*MB, routing, b) }
func BenchmarkRoutingSlowAddCat4MB(b *testing.B) { benchmarkAddCat(routing.Megabytes(4), b) } func BenchmarkRoutingSlowAddCat4MB(b *testing.B) { benchmarkAddCat(4*MB, routing, b) }
func BenchmarkRoutingSlowAddCat8MB(b *testing.B) { benchmarkAddCat(routing.Megabytes(8), b) } func BenchmarkRoutingSlowAddCat8MB(b *testing.B) { benchmarkAddCat(8*MB, routing, b) }
func BenchmarkRoutingSlowAddCat16MB(b *testing.B) { benchmarkAddCat(routing.Megabytes(16), b) } func BenchmarkRoutingSlowAddCat16MB(b *testing.B) { benchmarkAddCat(16*MB, routing, b) }
func BenchmarkRoutingSlowAddCat32MB(b *testing.B) { benchmarkAddCat(routing.Megabytes(32), b) } func BenchmarkRoutingSlowAddCat32MB(b *testing.B) { benchmarkAddCat(32*MB, routing, b) }
var network = Config{}.Network_NYtoSF() var network = Config{}.Network_NYtoSF()
func BenchmarkNetworkSlowAddCat1MB(b *testing.B) { benchmarkAddCat(network.Megabytes(1), b) } func BenchmarkNetworkSlowAddCat1MB(b *testing.B) { benchmarkAddCat(1*MB, network, b) }
func BenchmarkNetworkSlowAddCat2MB(b *testing.B) { benchmarkAddCat(network.Megabytes(2), b) } func BenchmarkNetworkSlowAddCat2MB(b *testing.B) { benchmarkAddCat(2*MB, network, b) }
func BenchmarkNetworkSlowAddCat4MB(b *testing.B) { benchmarkAddCat(network.Megabytes(4), b) } func BenchmarkNetworkSlowAddCat4MB(b *testing.B) { benchmarkAddCat(4*MB, network, b) }
func BenchmarkNetworkSlowAddCat8MB(b *testing.B) { benchmarkAddCat(network.Megabytes(8), b) } func BenchmarkNetworkSlowAddCat8MB(b *testing.B) { benchmarkAddCat(8*MB, network, b) }
func BenchmarkNetworkSlowAddCat16MB(b *testing.B) { benchmarkAddCat(network.Megabytes(16), b) } func BenchmarkNetworkSlowAddCat16MB(b *testing.B) { benchmarkAddCat(16*MB, network, b) }
func BenchmarkNetworkSlowAddCat32MB(b *testing.B) { benchmarkAddCat(network.Megabytes(32), b) } func BenchmarkNetworkSlowAddCat32MB(b *testing.B) { benchmarkAddCat(32*MB, network, b) }
func BenchmarkNetworkSlowAddCat64MB(b *testing.B) { benchmarkAddCat(network.Megabytes(64), b) } func BenchmarkNetworkSlowAddCat64MB(b *testing.B) { benchmarkAddCat(64*MB, network, b) }
func BenchmarkNetworkSlowAddCat128MB(b *testing.B) { benchmarkAddCat(network.Megabytes(128), b) } func BenchmarkNetworkSlowAddCat128MB(b *testing.B) { benchmarkAddCat(128*MB, network, b) }
func BenchmarkNetworkSlowAddCat256MB(b *testing.B) { benchmarkAddCat(network.Megabytes(256), b) } func BenchmarkNetworkSlowAddCat256MB(b *testing.B) { benchmarkAddCat(256*MB, network, b) }
var blockstore = Config{}.Blockstore_7200RPM() var blockstore = Config{}.Blockstore_7200RPM()
func BenchmarkBlockstoreSlowAddCat1MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(1), b) } func BenchmarkBlockstoreSlowAddCat1MB(b *testing.B) { benchmarkAddCat(1*MB, blockstore, b) }
func BenchmarkBlockstoreSlowAddCat2MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(2), b) } func BenchmarkBlockstoreSlowAddCat2MB(b *testing.B) { benchmarkAddCat(2*MB, blockstore, b) }
func BenchmarkBlockstoreSlowAddCat4MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(4), b) } func BenchmarkBlockstoreSlowAddCat4MB(b *testing.B) { benchmarkAddCat(4*MB, blockstore, b) }
func BenchmarkBlockstoreSlowAddCat8MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(8), b) } func BenchmarkBlockstoreSlowAddCat8MB(b *testing.B) { benchmarkAddCat(8*MB, blockstore, b) }
func BenchmarkBlockstoreSlowAddCat16MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(16), b) } func BenchmarkBlockstoreSlowAddCat16MB(b *testing.B) { benchmarkAddCat(16*MB, blockstore, b) }
func BenchmarkBlockstoreSlowAddCat32MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(32), b) } func BenchmarkBlockstoreSlowAddCat32MB(b *testing.B) { benchmarkAddCat(32*MB, blockstore, b) }
func BenchmarkBlockstoreSlowAddCat64MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(64), b) } func BenchmarkBlockstoreSlowAddCat64MB(b *testing.B) { benchmarkAddCat(64*MB, blockstore, b) }
func BenchmarkBlockstoreSlowAddCat128MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(128), b) } func BenchmarkBlockstoreSlowAddCat128MB(b *testing.B) { benchmarkAddCat(128*MB, blockstore, b) }
func BenchmarkBlockstoreSlowAddCat256MB(b *testing.B) { benchmarkAddCat(blockstore.Megabytes(256), b) } func BenchmarkBlockstoreSlowAddCat256MB(b *testing.B) { benchmarkAddCat(256*MB, blockstore, b) }
...@@ -6,7 +6,6 @@ type Config struct { ...@@ -6,7 +6,6 @@ type Config struct {
BlockstoreLatency time.Duration BlockstoreLatency time.Duration
NetworkLatency time.Duration NetworkLatency time.Duration
RoutingLatency time.Duration RoutingLatency time.Duration
DataAmountBytes int64
} }
func (c Config) All_Instantaneous() Config { func (c Config) All_Instantaneous() Config {
...@@ -47,9 +46,3 @@ func (c Config) Routing_Slow() Config { ...@@ -47,9 +46,3 @@ func (c Config) Routing_Slow() Config {
c.BlockstoreLatency = 200 * time.Millisecond c.BlockstoreLatency = 200 * time.Millisecond
return c return c
} }
// Megabytes is a convenience method to set DataAmountBytes
func (c Config) Megabytes(mb int64) Config {
c.DataAmountBytes = mb * 1024 * 1024
return c
}
package epictest
const (
_ = iota // ignore first value by assigning to blank identifier
KB = 1 << (10 * iota)
MB
GB
TB
PB
EB
)
package epictest
import "testing"
// and the award for most meta goes to...
func TestByteSizeUnit(t *testing.T) {
if 1*KB != 1*1024 {
t.Fatal(1 * KB)
}
if 1*MB != 1*1024*1024 {
t.Fail()
}
if 1*GB != 1*1024*1024*1024 {
t.Fail()
}
if 1*TB != 1*1024*1024*1024*1024 {
t.Fail()
}
if 1*PB != 1*1024*1024*1024*1024*1024 {
t.Fail()
}
if 1*EB != 1*1024*1024*1024*1024*1024*1024 {
t.Fail()
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论