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 }, }