-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: optimize test execution time on large repeated data set (#122)
* perf: parse configuration once * feat: notes on future improvements * perf: implement query cache optimization
- Loading branch information
Showing
12 changed files
with
363 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package engine | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/Azure/ShieldGuard/sg/internal/result" | ||
) | ||
|
||
func (k queryCacheKey) cacheKey() string { | ||
return fmt.Sprintf( | ||
"%s:%d:%s", | ||
k.compilerKey, | ||
k.parsedInput.Hash(), | ||
k.query, | ||
) | ||
} | ||
|
||
type noopQueryCacheT struct{} | ||
|
||
func (noopQueryCacheT) set(_ queryCacheKey, _ []result.Result) {} | ||
|
||
func (noopQueryCacheT) get(_ queryCacheKey) ([]result.Result, bool) { | ||
return nil, false | ||
} | ||
|
||
var noopQueryCache = noopQueryCacheT{} | ||
|
||
type queryCache struct { | ||
mu *sync.RWMutex | ||
items map[string][]result.Result | ||
} | ||
|
||
// NewQueryCache creates a new QueryCache. | ||
func NewQueryCache() QueryCache { | ||
return &queryCache{ | ||
mu: &sync.RWMutex{}, | ||
items: make(map[string][]result.Result), | ||
} | ||
} | ||
|
||
var _ QueryCache = (*queryCache)(nil) | ||
|
||
func (qc *queryCache) set(key queryCacheKey, value []result.Result) { | ||
qc.mu.Lock() | ||
defer qc.mu.Unlock() | ||
|
||
qc.items[key.cacheKey()] = value | ||
} | ||
|
||
func (qc *queryCache) get(key queryCacheKey) ([]result.Result, bool) { | ||
qc.mu.RLock() | ||
defer qc.mu.RUnlock() | ||
|
||
rv, ok := qc.items[key.cacheKey()] | ||
return rv, ok | ||
} |
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,100 @@ | ||
package engine | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
|
||
"github.com/Azure/ShieldGuard/sg/internal/result" | ||
"github.com/open-policy-agent/opa/ast" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_noopQueryCacheT(t *testing.T) { | ||
t.Parallel() | ||
|
||
_, ok := noopQueryCache.get(queryCacheKey{}) | ||
assert.False(t, ok) | ||
noopQueryCache.set(queryCacheKey{}, nil) | ||
_, ok = noopQueryCache.get(queryCacheKey{}) | ||
assert.False(t, ok) | ||
} | ||
|
||
func Test_QueryCache(t *testing.T) { | ||
t.Run("basic", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
cacheKey1 := queryCacheKey{ | ||
compilerKey: "compilerKey1", | ||
parsedInput: ast.String("input1"), | ||
query: "query1", | ||
} | ||
cacheKey2 := queryCacheKey{ | ||
compilerKey: "compilerKey2", | ||
parsedInput: ast.String("input2"), | ||
query: "query2", | ||
} | ||
queryResult := []result.Result{ | ||
{ | ||
Query: "query", | ||
}, | ||
} | ||
|
||
qc := NewQueryCache() | ||
|
||
{ | ||
_, ok := qc.get(cacheKey1) | ||
assert.False(t, ok) | ||
_, ok = qc.get(cacheKey2) | ||
assert.False(t, ok) | ||
} | ||
|
||
{ | ||
qc.set(cacheKey1, queryResult) | ||
cached, ok := qc.get(cacheKey1) | ||
assert.True(t, ok) | ||
assert.Equal(t, queryResult, cached) | ||
|
||
_, ok = qc.get(cacheKey2) | ||
assert.False(t, ok) | ||
} | ||
}) | ||
|
||
t.Run("concurrent access", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
cacheKey := queryCacheKey{ | ||
compilerKey: "compilerKey", | ||
parsedInput: ast.String("input"), | ||
query: "query", | ||
} | ||
queryResult := []result.Result{ | ||
{ | ||
Query: "query", | ||
}, | ||
} | ||
|
||
qc := NewQueryCache() | ||
|
||
{ | ||
_, ok := qc.get(cacheKey) | ||
assert.False(t, ok) | ||
} | ||
|
||
var wg sync.WaitGroup | ||
for i := 0; i < 100; i++ { | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
|
||
qc.set(cacheKey, queryResult) | ||
}() | ||
} | ||
wg.Wait() | ||
|
||
{ | ||
cached, ok := qc.get(cacheKey) | ||
assert.True(t, ok) | ||
assert.Equal(t, queryResult, cached) | ||
} | ||
}) | ||
} |
Oops, something went wrong.