main.go 2.4 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
Benchmark github.com/ipfs/go-ipfs/blockservice/worker.

Loop over a range of workers and buffer sizes and measure the time it
per block-transfer operation for each value.  Run with:

  $ go run "${GOPATH}/src/github.com/ipfs/go-ipfs/blockservice/worker/bench/main.go"
*/

10 11 12 13 14 15 16 17
package main

import (
	"log"
	"math"
	"testing"
	"time"

18 19 20 21 22 23 24 25
	ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
	ds_sync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
	blocks "github.com/ipfs/go-ipfs/blocks"
	blockstore "github.com/ipfs/go-ipfs/blocks/blockstore"
	worker "github.com/ipfs/go-ipfs/blockservice/worker"
	"github.com/ipfs/go-ipfs/exchange/offline"
	"github.com/ipfs/go-ipfs/thirdparty/delay"
	"github.com/ipfs/go-ipfs/util/datastore2"
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
)

const kEstRoutingDelay = time.Second

const kBlocksPerOp = 100

func main() {
	var bestConfig worker.Config
	var quickestNsPerOp int64 = math.MaxInt64
	for NumWorkers := 1; NumWorkers < 10; NumWorkers++ {
		for ClientBufferSize := 0; ClientBufferSize < 10; ClientBufferSize++ {
			for WorkerBufferSize := 0; WorkerBufferSize < 10; WorkerBufferSize++ {
				c := worker.Config{
					NumWorkers:       NumWorkers,
					ClientBufferSize: ClientBufferSize,
					WorkerBufferSize: WorkerBufferSize,
				}
				result := testing.Benchmark(BenchmarkWithConfig(c))
				if result.NsPerOp() < quickestNsPerOp {
					bestConfig = c
					quickestNsPerOp = result.NsPerOp()
				}
				log.Printf("benched %+v \t result: %+v", c, result)
			}
		}
	}
	log.Println(bestConfig)
}

func BenchmarkWithConfig(c worker.Config) func(b *testing.B) {
	return func(b *testing.B) {

		routingDelay := delay.Fixed(0) // during setup

		dstore := ds_sync.MutexWrap(datastore2.WithDelay(ds.NewMapDatastore(), routingDelay))
		bstore := blockstore.NewBlockstore(dstore)
		var testdata []*blocks.Block
		var i int64
		for i = 0; i < kBlocksPerOp; i++ {
			testdata = append(testdata, blocks.NewBlock([]byte(string(i))))
		}
		b.ResetTimer()
		b.SetBytes(kBlocksPerOp)
		for i := 0; i < b.N; i++ {

			b.StopTimer()
			w := worker.NewWorker(offline.Exchange(bstore), c)
			b.StartTimer()

			prev := routingDelay.Set(kEstRoutingDelay) // during measured section

			for _, block := range testdata {
				if err := w.HasBlock(block); err != nil {
					b.Fatal(err)
				}
			}

			routingDelay.Set(prev) // to hasten the unmeasured close period

			b.StopTimer()
			w.Close()
			b.StartTimer()

		}
	}
}