From d560ab020a802fd898f83d3bdb1466703848fd3e Mon Sep 17 00:00:00 2001 From: Tim Raymond Date: Tue, 22 Aug 2017 10:23:24 -0400 Subject: [PATCH] Improve benchmark, fix offset bug This ensures that setup time does not impact the results of the benchmark since profiling showed that much time was spent setting up the test array of tasks. This also uses `make` to build that slice instead. Also, the tests revealed that there was a bug where offsets were pre-incremented rather than post-incremented, omitting the first 100 results. --- kapacitor/kapa_client.go | 2 +- kapacitor/kapa_client_benchmark_test.go | 23 ++++++++++++++--------- kapacitor/kapa_client_test.go | 4 ++++ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/kapacitor/kapa_client.go b/kapacitor/kapa_client.go index 31f51bc96..f0d6c9e48 100644 --- a/kapacitor/kapa_client.go +++ b/kapacitor/kapa_client.go @@ -51,7 +51,6 @@ func (p *PaginatingKapaClient) ListTasks(opts *client.ListTasksOptions) ([]clien } for { - opts.Offset = p.FetchRate + opts.Offset select { case <-done: close(optChan) @@ -59,6 +58,7 @@ func (p *PaginatingKapaClient) ListTasks(opts *client.ListTasksOptions) ([]clien case optChan <- *opts: // nop } + opts.Offset = p.FetchRate + opts.Offset } }() diff --git a/kapacitor/kapa_client_benchmark_test.go b/kapacitor/kapa_client_benchmark_test.go index 83bb88028..bca4cbebb 100644 --- a/kapacitor/kapa_client_benchmark_test.go +++ b/kapacitor/kapa_client_benchmark_test.go @@ -8,21 +8,23 @@ import ( client "github.com/influxdata/kapacitor/client/v1" ) -func BenchmarkKapaClient100(b *testing.B) { benchmark_PaginatingKapaClient(100, b) } -func BenchmarkKapaClient1000(b *testing.B) { benchmark_PaginatingKapaClient(1000, b) } -func BenchmarkKapaClient10000(b *testing.B) { benchmark_PaginatingKapaClient(10000, b) } +func BenchmarkKapaClient100(b *testing.B) { benchmark_PaginatingKapaClient(100, b) } +func BenchmarkKapaClient1000(b *testing.B) { benchmark_PaginatingKapaClient(1000, b) } +func BenchmarkKapaClient10000(b *testing.B) { benchmark_PaginatingKapaClient(10000, b) } +func BenchmarkKapaClient100000(b *testing.B) { benchmark_PaginatingKapaClient(100000, b) } + +var tasks []client.Task func benchmark_PaginatingKapaClient(taskCount int, b *testing.B) { + + b.StopTimer() // eliminate setup time + // create a mock client that will return a huge response from ListTasks mockClient := &mocks.KapaClient{ ListTasksF: func(opts *client.ListTasksOptions) ([]client.Task, error) { // create all the tasks - allTasks := []client.Task{} + allTasks := make([]client.Task, taskCount) - // create N tasks from the benchmark runner - for i := 0; i < taskCount; i++ { - allTasks = append(allTasks, client.Task{}) - } begin := opts.Offset end := opts.Offset + opts.Limit @@ -42,8 +44,11 @@ func benchmark_PaginatingKapaClient(taskCount int, b *testing.B) { opts := &client.ListTasksOptions{} + b.StartTimer() // eliminate setup time + // let the benchmark runner run ListTasks until it's satisfied for n := 0; n < b.N; n++ { - _, _ = pkap.ListTasks(opts) + // assignment is to avoid having the call optimized away + tasks, _ = pkap.ListTasks(opts) } } diff --git a/kapacitor/kapa_client_test.go b/kapacitor/kapa_client_test.go index 63e1546ec..9dda521e5 100644 --- a/kapacitor/kapa_client_test.go +++ b/kapacitor/kapa_client_test.go @@ -26,6 +26,10 @@ func Test_Kapacitor_PaginatingKapaClient(t *testing.T) { end = len(allTasks) } + if begin > len(allTasks) { + begin = end + } + return allTasks[begin:end], nil }, }