-
Notifications
You must be signed in to change notification settings - Fork 1
perf(context): skip unused CEL env funcs #2014
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
Closed
Closed
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 hidden or 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,94 @@ | ||
| package bench_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
| "time" | ||
|
|
||
| dutyctx "github.com/flanksource/duty/context" | ||
| "github.com/flanksource/gomplate/v3" | ||
| "github.com/google/cel-go/cel" | ||
| celtypes "github.com/google/cel-go/common/types" | ||
| "github.com/google/cel-go/common/types/ref" | ||
| ) | ||
|
|
||
| // BenchmarkRunTemplateCELCacheHitRegisteredEnvFuncs isolates the duty-side | ||
| // overhead of constructing registered CEL env functions on every RunTemplate | ||
| // call. The expression does not call any registered function; the benchmark only | ||
| // varies how many functions are registered globally. | ||
| func BenchmarkRunTemplateCELCacheHitRegisteredEnvFuncs(b *testing.B) { | ||
| env := map[string]any{ | ||
| "id": "0192f0a4-1234-7000-8000-aaaaaaaaaaaa", | ||
| "namespace": "default", | ||
| "name": "nginx-7c5ddbdf54-abcde", | ||
| "index": 42, | ||
| "config_type": "Kubernetes::Pod", | ||
| } | ||
|
|
||
| for _, registeredFuncs := range []int{0, 18, 64} { | ||
| b.Run(fmt.Sprintf("registered=%02d", registeredFuncs), func(b *testing.B) { | ||
| installBenchmarkCelEnvFuncs(b, registeredFuncs) | ||
|
|
||
| ctx := dutyctx.New() | ||
| tmpl := gomplate.Template{ | ||
| Expression: `config_type == "Kubernetes::Pod"`, | ||
| CacheKey: fmt.Sprintf("benchmark.run-template.cel-env-funcs.%d", registeredFuncs), | ||
| CacheTime: time.Hour, | ||
| } | ||
|
|
||
| // Warm gomplate's compiled CEL program cache. Any measured allocation after | ||
| // this point should be steady-state RunTemplate overhead, not CEL compile. | ||
| if _, err := ctx.RunTemplateBool(tmpl, env); err != nil { | ||
| b.Fatal(err) | ||
| } | ||
|
|
||
| b.ReportAllocs() | ||
| b.ResetTimer() | ||
| for i := 0; i < b.N; i++ { | ||
| ok, err := ctx.RunTemplateBool(tmpl, env) | ||
| if err != nil { | ||
| b.Fatal(err) | ||
| } | ||
| if !ok { | ||
| b.Fatal("expected expression to evaluate true") | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func installBenchmarkCelEnvFuncs(b *testing.B, count int) { | ||
| b.Helper() | ||
|
|
||
| oldCelEnvFuncs := dutyctx.CelEnvFuncs | ||
| oldTemplateFuncs := dutyctx.TemplateFuncs | ||
|
|
||
| dutyctx.CelEnvFuncs = make(map[string]func(dutyctx.Context) cel.EnvOption, count) | ||
| for i := 0; i < count; i++ { | ||
| name := fmt.Sprintf("bench.func_%02d", i) | ||
| dutyctx.CelEnvFuncs[name] = benchmarkCelEnvFunc(name, fmt.Sprintf("bench_func_%02d_string", i)) | ||
| } | ||
| dutyctx.TemplateFuncs = nil | ||
|
|
||
| b.Cleanup(func() { | ||
| dutyctx.CelEnvFuncs = oldCelEnvFuncs | ||
| dutyctx.TemplateFuncs = oldTemplateFuncs | ||
| }) | ||
| } | ||
|
|
||
| func benchmarkCelEnvFunc(name, overloadID string) func(dutyctx.Context) cel.EnvOption { | ||
| return func(ctx dutyctx.Context) cel.EnvOption { | ||
| return cel.Function(name, | ||
| cel.Overload(overloadID, | ||
| []*cel.Type{cel.StringType}, | ||
| cel.StringType, | ||
| cel.UnaryBinding(func(arg ref.Val) ref.Val { | ||
| // Capture ctx like production DB/catalog functions do. This binding is | ||
| // intentionally never called by the benchmark expression. | ||
| _ = ctx | ||
| return celtypes.String(fmt.Sprintf("%s:%v", name, arg.Value())) | ||
| }), | ||
| ), | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
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.
📐 Maintainability & Code Quality | 🟡 Minor
🧩 Analysis chain
🏁 Script executed:
Repository: flanksource/duty
Length of output: 323
🏁 Script executed:
# Read the benchmark test file cat -n bench/template_cel_env_bench_test.goRepository: flanksource/duty
Length of output: 3651
🏁 Script executed:
Repository: flanksource/duty
Length of output: 3149
Use gomega assertions in this
_test.gobenchmark.This benchmark uses assertion-style
b.Fatalchecks instead of gomega expectations. Please switch togomega.NewWithT(b)andExpect(...)for these checks.Proposed patch
Per coding guidelines,
**/*_test.go: "Always usegithub.com/onsi/gomegapackage for assertions in test files" and "use thegomega.NewWithT(t)approach for assertions."📝 Committable suggestion
🤖 Prompt for AI Agents
Source: Coding guidelines