-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize !=
string allocation return results
#32
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"encoding/hex" | ||
"fmt" | ||
"math/rand" | ||
"runtime" | ||
"strings" | ||
"sync" | ||
"testing" | ||
|
@@ -120,7 +121,7 @@ func TestEvaluate_Strings(t *testing.T) { | |
ctx := context.Background() | ||
parser := NewTreeParser(NewCachingCompiler(newEnv(), nil)) | ||
|
||
expected := tex(`event.data.account_id == "yes" && event.data.match == "true"`) | ||
expected := tex(`event.data.account_id == "yes" && event.data.another == "ok" && event.data.match == "true"`) | ||
loader := newEvalLoader() | ||
loader.AddEval(expected) | ||
|
||
|
@@ -145,6 +146,7 @@ func TestEvaluate_Strings(t *testing.T) { | |
"event": map[string]any{ | ||
"data": map[string]any{ | ||
"account_id": "yes", | ||
"another": "ok", | ||
"match": "true", | ||
}, | ||
}, | ||
|
@@ -166,6 +168,7 @@ func TestEvaluate_Strings(t *testing.T) { | |
"event": map[string]any{ | ||
"data": map[string]any{ | ||
"account_id": "yes", | ||
"another": "ok", | ||
"match": "no", | ||
}, | ||
}, | ||
|
@@ -181,10 +184,11 @@ func TestEvaluate_Strings(t *testing.T) { | |
} | ||
|
||
func TestEvaluate_Strings_Inequality(t *testing.T) { | ||
|
||
ctx := context.Background() | ||
parser := NewTreeParser(NewCachingCompiler(newEnv(), nil)) | ||
|
||
expected := tex(`event.data.account_id == "yes" && event.data.neq != "neq"`) | ||
expected := tex(`event.data.account_id == "yes" && event.data.another == "ok" && event.data.neq != "neq"`) | ||
loader := newEvalLoader() | ||
loader.AddEval(expected) | ||
|
||
|
@@ -194,17 +198,19 @@ func TestEvaluate_Strings_Inequality(t *testing.T) { | |
require.NoError(t, err) | ||
|
||
n := 100_000 | ||
|
||
addOtherExpressions(n, e, loader) | ||
|
||
require.EqualValues(t, n+1, e.Len()) | ||
|
||
mem := getMem() | ||
printMem(mem, "no matches") | ||
|
||
t.Run("It matches items", func(t *testing.T) { | ||
pre := time.Now() | ||
evals, matched, err := e.Evaluate(ctx, map[string]any{ | ||
"event": map[string]any{ | ||
"data": map[string]any{ | ||
"account_id": "yes", | ||
"another": "ok", | ||
"match": "true", | ||
"neq": "nah", | ||
}, | ||
|
@@ -222,12 +228,15 @@ func TestEvaluate_Strings_Inequality(t *testing.T) { | |
require.GreaterOrEqual(t, matched, int32(1)) | ||
}) | ||
|
||
printMem(getMem(), "first match") | ||
|
||
t.Run("It handles non-matching data", func(t *testing.T) { | ||
pre := time.Now() | ||
evals, matched, err := e.Evaluate(ctx, map[string]any{ | ||
"event": map[string]any{ | ||
"data": map[string]any{ | ||
"account_id": "yes", | ||
"another": "ok", | ||
"match": "no", | ||
"neq": "nah", | ||
}, | ||
|
@@ -241,6 +250,8 @@ func TestEvaluate_Strings_Inequality(t *testing.T) { | |
require.EqualValues(t, 1, len(evals)) | ||
require.EqualValues(t, 1, matched) | ||
}) | ||
|
||
printMem(getMem(), "second match") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove? |
||
} | ||
|
||
func TestEvaluate_Numbers(t *testing.T) { | ||
|
@@ -1189,3 +1200,33 @@ func addOtherExpressions(n int, e AggregateEvaluator, loader *evalLoader) { | |
} | ||
wg.Wait() | ||
} | ||
|
||
func getMem() runtime.MemStats { | ||
var m runtime.MemStats | ||
runtime.ReadMemStats(&m) | ||
return m | ||
} | ||
|
||
//nolint:all | ||
func deltaMem(prev runtime.MemStats) runtime.MemStats { | ||
next := getMem() | ||
|
||
return runtime.MemStats{ | ||
HeapAlloc: next.HeapAlloc - prev.HeapAlloc, | ||
Alloc: next.Alloc - prev.Alloc, | ||
TotalAlloc: next.TotalAlloc - prev.TotalAlloc, | ||
} | ||
} | ||
|
||
func printMem(m runtime.MemStats, label ...string) { | ||
if len(label) > 0 { | ||
fmt.Printf("\t%s\n", label[0]) | ||
} | ||
|
||
fmt.Printf("\tAlloc: %d MiB\n", bToMb(m.Alloc)) | ||
fmt.Printf("\tTotalAlloc: %d MiB\n", bToMb(m.TotalAlloc)) | ||
} | ||
|
||
func bToMb(b uint64) uint64 { | ||
return b / 1024 / 1024 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove?