github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/blockservice/worker/bench/main.go (about) 1 /* 2 Benchmark github.com/ipfs/go-ipfs/blockservice/worker. 3 4 Loop over a range of workers and buffer sizes and measure the time it 5 per block-transfer operation for each value. Run with: 6 7 $ go run "${GOPATH}/src/github.com/ipfs/go-ipfs/blockservice/worker/bench/main.go" 8 */ 9 10 package main 11 12 import ( 13 "log" 14 "math" 15 "testing" 16 "time" 17 18 ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore" 19 ds_sync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync" 20 blocks "github.com/ipfs/go-ipfs/blocks" 21 blockstore "github.com/ipfs/go-ipfs/blocks/blockstore" 22 worker "github.com/ipfs/go-ipfs/blockservice/worker" 23 "github.com/ipfs/go-ipfs/exchange/offline" 24 "github.com/ipfs/go-ipfs/thirdparty/delay" 25 "github.com/ipfs/go-ipfs/util/datastore2" 26 ) 27 28 const kEstRoutingDelay = time.Second 29 30 const kBlocksPerOp = 100 31 32 func main() { 33 var bestConfig worker.Config 34 var quickestNsPerOp int64 = math.MaxInt64 35 for NumWorkers := 1; NumWorkers < 10; NumWorkers++ { 36 for ClientBufferSize := 0; ClientBufferSize < 10; ClientBufferSize++ { 37 for WorkerBufferSize := 0; WorkerBufferSize < 10; WorkerBufferSize++ { 38 c := worker.Config{ 39 NumWorkers: NumWorkers, 40 ClientBufferSize: ClientBufferSize, 41 WorkerBufferSize: WorkerBufferSize, 42 } 43 result := testing.Benchmark(BenchmarkWithConfig(c)) 44 if result.NsPerOp() < quickestNsPerOp { 45 bestConfig = c 46 quickestNsPerOp = result.NsPerOp() 47 } 48 log.Printf("benched %+v \t result: %+v", c, result) 49 } 50 } 51 } 52 log.Println(bestConfig) 53 } 54 55 func BenchmarkWithConfig(c worker.Config) func(b *testing.B) { 56 return func(b *testing.B) { 57 58 routingDelay := delay.Fixed(0) // during setup 59 60 dstore := ds_sync.MutexWrap(datastore2.WithDelay(ds.NewMapDatastore(), routingDelay)) 61 bstore := blockstore.NewBlockstore(dstore) 62 var testdata []*blocks.Block 63 var i int64 64 for i = 0; i < kBlocksPerOp; i++ { 65 testdata = append(testdata, blocks.NewBlock([]byte(string(i)))) 66 } 67 b.ResetTimer() 68 b.SetBytes(kBlocksPerOp) 69 for i := 0; i < b.N; i++ { 70 71 b.StopTimer() 72 w := worker.NewWorker(offline.Exchange(bstore), c) 73 b.StartTimer() 74 75 prev := routingDelay.Set(kEstRoutingDelay) // during measured section 76 77 for _, block := range testdata { 78 if err := w.HasBlock(block); err != nil { 79 b.Fatal(err) 80 } 81 } 82 83 routingDelay.Set(prev) // to hasten the unmeasured close period 84 85 b.StopTimer() 86 w.Close() 87 b.StartTimer() 88 89 } 90 } 91 }