test(task): cache parsed options in test

This is to work around slow parsing (#484).

This change saves about 2 seconds on tests for task/backend and
task/backend/bolt. It saves around 45 seconds on enterprise tests.
pull/10616/head
Mark Rushakoff 2018-08-07 12:32:00 -07:00 committed by Mark Rushakoff
parent a647f10c14
commit 24ea772ee4
3 changed files with 43 additions and 1 deletions

View File

@ -10,8 +10,14 @@ import (
"github.com/influxdata/platform/task/backend"
boltstore "github.com/influxdata/platform/task/backend/bolt"
"github.com/influxdata/platform/task/backend/storetest"
"github.com/influxdata/platform/task/options"
)
func init() {
// TODO(mr): remove as part of https://github.com/influxdata/platform/issues/484.
options.EnableScriptCacheForTest()
}
func TestBoltStore(t *testing.T) {
var f *os.File
storetest.NewStoreTest(

View File

@ -5,8 +5,14 @@ import (
"github.com/influxdata/platform/task/backend"
"github.com/influxdata/platform/task/backend/storetest"
"github.com/influxdata/platform/task/options"
)
func init() {
// TODO(mr): remove as part of https://github.com/influxdata/platform/issues/484.
options.EnableScriptCacheForTest()
}
func TestInMemStore(t *testing.T) {
storetest.NewStoreTest(
"in-mem store",

View File

@ -1,13 +1,26 @@
// Package options provides ways to extract the task-related options from a Flux script or query Spec.
// Package options provides ways to extract the task-related options from a Flux script.
package options
import (
"errors"
"sync"
"time"
"github.com/influxdata/platform/query"
)
// optionCache is enabled for tests, to work around https://github.com/influxdata/platform/issues/484.
var optionCache map[string]Options
var optionCacheMu sync.Mutex
// EnableScriptCacheForTest is used as a workaround for https://github.com/influxdata/platform/issues/484,
// and should be removed after that issue is addressed.
// Do not call this method in production code, as it will leak memory.
func EnableScriptCacheForTest() {
optionCache = make(map[string]Options)
}
const maxConcurrency = 100
const maxRetry = 10
@ -32,6 +45,16 @@ type Options struct {
// FromScript extracts Options from a Flux script.
func FromScript(script string) (Options, error) {
if optionCache != nil {
optionCacheMu.Lock()
opt, ok := optionCache[script]
optionCacheMu.Unlock()
if ok {
return opt, nil
}
}
opt := Options{Retry: 1, Concurrency: 1}
inter := query.NewInterpreter()
@ -93,5 +116,12 @@ func FromScript(script string) (Options, error) {
}
opt.Retry = retry
}
if optionCache != nil {
optionCacheMu.Lock()
optionCache[script] = opt
optionCacheMu.Unlock()
}
return opt, nil
}