From 9c717e2eeec1f551cc7bcfd2593090df6c20d332 Mon Sep 17 00:00:00 2001 From: jaffee Date: Wed, 4 Jan 2017 09:11:53 -0600 Subject: [PATCH] remove unused parallelBenchmark and move Serial --- bench/bench.go | 105 +----------------------------------------- cmd/pilosactl/main.go | 53 ++++++++++++++++++++- 2 files changed, 53 insertions(+), 105 deletions(-) diff --git a/bench/bench.go b/bench/bench.go index 23e49fb3e..0ffcc0a40 100644 --- a/bench/bench.go +++ b/bench/bench.go @@ -1,12 +1,6 @@ package bench -import ( - "context" - "fmt" - "strconv" - "sync" - "time" -) +import "context" // Benchmark is an interface to guide the creation of new pilosa benchmarks or // benchmark components. It defines 2 methods, Init, and Run. These are separate @@ -43,100 +37,3 @@ type Command interface { func agentizeNum(n, iterations, agentNum int) int { return n + (agentNum * iterations) } - -type parallelBenchmark struct { - benchmarkers []Benchmark -} - -// Init calls Init for each benchmark. If there are any errors, it will return a -// non-nil error value. -func (pb *parallelBenchmark) Init(hosts []string, agentNum int) error { - var g ErrGroup - for i, _ := range pb.benchmarkers { - b := pb.benchmarkers[i] - g.Go(func() error { - return b.Init(hosts, agentNum) - }) - } - return g.Wait() -} - -// Run runs the parallel benchmark and returns it's results in a nested map - the -// top level keys are the indices of each benchmark in the list of benchmarks, -// and the values are the results of each benchmark's Run method. -func (pb *parallelBenchmark) Run(ctx context.Context, agentNum int) map[string]interface{} { - wg := sync.WaitGroup{} - results := make(map[string]interface{}, len(pb.benchmarkers)) - resultsLock := sync.Mutex{} - for i, b := range pb.benchmarkers { - wg.Add(1) - go func(i int, b Benchmark) { - defer wg.Done() - ret := b.Run(ctx, agentNum) - resultsLock.Lock() - results[strconv.Itoa(i)] = ret - resultsLock.Unlock() - }(i, b) - } - wg.Wait() - return results -} - -// Parallel takes a variable number of Benchmarks and returns a Benchmark -// which combines them and will run them in parallel. -func Parallel(bs ...Benchmark) Benchmark { - return ¶llelBenchmark{ - benchmarkers: bs, - } -} - -type serialBenchmark struct { - benchmarkers []Benchmark -} - -// Init calls Init for each benchmark. If there are any errors, it will return a -// non-nil error value. -func (sb *serialBenchmark) Init(hosts []string, agentNum int) error { - errors := make([]error, len(sb.benchmarkers)) - hadErr := false - for i, b := range sb.benchmarkers { - errors[i] = b.Init(hosts, agentNum) - if errors[i] != nil { - hadErr = true - } - } - if hadErr { - return fmt.Errorf("Had errs in serialBenchmark.Init: %v", errors) - } - return nil -} - -// Run runs the serial benchmark and returns it's results in a nested map - the -// top level keys are the indices of each benchmark in the list of benchmarks, -// and the values are the results of each benchmark's Run method. -func (sb *serialBenchmark) Run(ctx context.Context, agentNum int) map[string]interface{} { - benchmarks := make([]map[string]interface{}, len(sb.benchmarkers)) - results := map[string]interface{}{"benchmarks": benchmarks} - - total_start := time.Now() - for i, b := range sb.benchmarkers { - start := time.Now() - output := b.Run(ctx, agentNum) - if _, ok := output["runtime"]; ok { - panic(fmt.Sprintf("Benchmark %v added 'runtime' to its results", b)) - } - output["runtime"] = time.Now().Sub(start) - ret := map[string]interface{}{"output": output, "metadata": b} - benchmarks[i] = ret - } - results["total_runtime"] = time.Now().Sub(total_start) - return results -} - -// Serial takes a variable number of Benchmarks and returns a Benchmark -// which combines then and will run each serially. -func Serial(bs ...Benchmark) Benchmark { - return &serialBenchmark{ - benchmarkers: bs, - } -} diff --git a/cmd/pilosactl/main.go b/cmd/pilosactl/main.go index 27d4002a8..8aefee899 100644 --- a/cmd/pilosactl/main.go +++ b/cmd/pilosactl/main.go @@ -1286,7 +1286,7 @@ The following arguments are available: // Run executes the benchmark agent. func (cmd *BagentCommand) Run(ctx context.Context) error { - sbm := bench.Serial(cmd.Benchmarks...) + sbm := serial(cmd.Benchmarks...) err := sbm.Init(cmd.Hosts, cmd.AgentNum) if err != nil { return fmt.Errorf("in cmd.Run initialization: %v", err) @@ -1517,6 +1517,57 @@ func (cmd *BspawnCommand) spawnLocal(ctx context.Context, runUUID uuid.UUID) err return nil } +type serialBenchmark struct { + benchmarkers []bench.Benchmark +} + +// Init calls Init for each benchmark. If there are any errors, it will return a +// non-nil error value. +func (sb *serialBenchmark) Init(hosts []string, agentNum int) error { + errors := make([]error, len(sb.benchmarkers)) + hadErr := false + for i, b := range sb.benchmarkers { + errors[i] = b.Init(hosts, agentNum) + if errors[i] != nil { + hadErr = true + } + } + if hadErr { + return fmt.Errorf("Had errs in serialBenchmark.Init: %v", errors) + } + return nil +} + +// Run runs the serial benchmark and returns it's results in a nested map - the +// top level keys are the indices of each benchmark in the list of benchmarks, +// and the values are the results of each benchmark's Run method. +func (sb *serialBenchmark) Run(ctx context.Context, agentNum int) map[string]interface{} { + benchmarks := make([]map[string]interface{}, len(sb.benchmarkers)) + results := map[string]interface{}{"benchmarks": benchmarks} + + total_start := time.Now() + for i, b := range sb.benchmarkers { + start := time.Now() + output := b.Run(ctx, agentNum) + if _, ok := output["runtime"]; ok { + panic(fmt.Sprintf("Benchmark %v added 'runtime' to its results", b)) + } + output["runtime"] = time.Now().Sub(start) + ret := map[string]interface{}{"output": output, "metadata": b} + benchmarks[i] = ret + } + results["total_runtime"] = time.Now().Sub(total_start) + return results +} + +// serial takes a variable number of Benchmarks and returns a Benchmark +// which combines then and will run each serially. +func serial(bs ...bench.Benchmark) bench.Benchmark { + return &serialBenchmark{ + benchmarkers: bs, + } +} + // readCSVRow reads a bitmap/profile pair from a CSV row. func readCSVRow(r *csv.Reader) (bitmapID, profileID uint64, err error) { // Read CSV row.