forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scope reusing & smaller scopes (influxdata#496)
For each expression we are creating "scope pool", which is object pool of scopes - with some extra magic. By doing quick analysis on the node AST I know which tags and fields he requires. so we put only the required ones. For example: "value" > 10, I fill only "value" from field or tag. name old time/op new time/op delta _T10_P500_AlertTask-4 139ms ± 2% 132ms ± 3% -5.23% (p=0.008 n=5+5) _T10_P50000_AlertTask-4 14.6s ± 1% 13.3s ± 1% -8.92% (p=0.008 n=5+5) _T1000_P500_AlertTask-4 13.8s ± 1% 13.1s ± 2% -4.87% (p=0.008 n=5+5) name old alloc/op new alloc/op delta _T10_P500_AlertTask-4 32.1MB ± 0% 25.9MB ± 0% -19.52% (p=0.008 n=5+5) _T10_P50000_AlertTask-4 3.26GB ± 0% 2.62GB ± 0% -19.78% (p=0.008 n=5+5) _T1000_P500_AlertTask-4 3.21GB ± 0% 2.61GB ± 0% -18.71% (p=0.008 n=5+5) name old allocs/op new allocs/op delta _T10_P500_AlertTask-4 406k ± 0% 333k ± 0% -17.96% (p=0.008 n=5+5) _T10_P50000_AlertTask-4 41.4M ± 0% 34.0M ± 0% -18.05% (p=0.008 n=5+5) _T1000_P500_AlertTask-4 40.2M ± 0% 33.1M ± 0% -17.65% (p=0.008 n=5+5)
- Loading branch information
Showing
8 changed files
with
210 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package stateful | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/influxdata/kapacitor/tick" | ||
) | ||
|
||
// ScopePool - pooling mechanism for tick.Scope | ||
// The idea behind scope pool is to pool scopes and to put them only | ||
// the needed variables for execution. | ||
type ScopePool interface { | ||
Get() *tick.Scope | ||
Put(scope *tick.Scope) | ||
|
||
ReferenceVariables() []string | ||
} | ||
|
||
type scopePool struct { | ||
referenceVariables []string | ||
pool sync.Pool | ||
} | ||
|
||
// NewScopePool - creates new ScopePool for the given Node | ||
func NewScopePool(referenceVariables []string) ScopePool { | ||
scopePool := &scopePool{ | ||
referenceVariables: referenceVariables, | ||
} | ||
|
||
scopePool.pool = sync.Pool{ | ||
New: func() interface{} { | ||
scope := tick.NewScope() | ||
for _, refVariable := range scopePool.referenceVariables { | ||
scope.Set(refVariable, nil) | ||
} | ||
|
||
return scope | ||
}, | ||
} | ||
|
||
return scopePool | ||
} | ||
|
||
func (s *scopePool) ReferenceVariables() []string { | ||
return s.referenceVariables | ||
} | ||
|
||
// Get - returns a scope from a pool with the needed reference variables | ||
// (with nil values/old values) in the scope | ||
func (s *scopePool) Get() *tick.Scope { | ||
return s.pool.Get().(*tick.Scope) | ||
} | ||
|
||
// Put - put used scope back to the pool | ||
func (s *scopePool) Put(scope *tick.Scope) { | ||
s.pool.Put(scope) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package stateful_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/influxdata/kapacitor/tick" | ||
"github.com/influxdata/kapacitor/tick/stateful" | ||
) | ||
|
||
func TestScopePool_Sanity(t *testing.T) { | ||
n := stateful.NewScopePool([]string{"value"}) | ||
|
||
// first | ||
scope := n.Get() | ||
|
||
_, existsErr := scope.Get("value") | ||
|
||
if existsErr != nil { | ||
t.Errorf("First: Expected \"value\" to exist in the scope, but go an error: %v", existsErr) | ||
} | ||
|
||
// second, after put | ||
n.Put(scope) | ||
|
||
scope = n.Get() | ||
_, existsErr = scope.Get("value") | ||
|
||
if existsErr != nil { | ||
t.Errorf("Second: Expected \"value\" to exist in the scope, but go an error: %v", existsErr) | ||
} | ||
} | ||
|
||
func TestExpression_RefernceVariables(t *testing.T) { | ||
|
||
type expectation struct { | ||
node tick.Node | ||
refVariables []string | ||
} | ||
|
||
expectations := []expectation{ | ||
{node: &tick.NumberNode{IsFloat: true}, refVariables: make([]string, 0)}, | ||
{node: &tick.BoolNode{}, refVariables: make([]string, 0)}, | ||
|
||
{node: &tick.ReferenceNode{Reference: "yosi"}, refVariables: []string{"yosi"}}, | ||
{node: &tick.BinaryNode{Left: &tick.ReferenceNode{Reference: "value"}, Right: &tick.NumberNode{IsInt: true}}, refVariables: []string{"value"}}, | ||
} | ||
|
||
for i, expect := range expectations { | ||
refVariables := stateful.FindReferenceVariables(expect.node) | ||
if !reflect.DeepEqual(refVariables, expect.refVariables) { | ||
t.Errorf("[Iteration: %v, Node: %T] Got unexpected result:\ngot: %v\nexpected: %v", i+1, expect.node, refVariables, expect.refVariables) | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters