Skip to content

Commit bd6d7a4

Browse files
test(lua): benchmark compiled script cache on vs off
Use the bundled argoproj.io/Rollout health script (~5 KB) with real testdata so the benchmark reflects compile cost on representative checks. Co-authored-by: Cursor <cursoragent@cursor.com> Signed-off-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com>
1 parent c1fecdc commit bd6d7a4

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

util/lua/script_cache_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@ package lua
22

33
import (
44
"fmt"
5+
"os"
56
"testing"
67

78
lua "github.com/yuin/gopher-lua"
9+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
10+
"sigs.k8s.io/yaml"
811

912
"github.com/stretchr/testify/assert"
1013
"github.com/stretchr/testify/require"
1114
)
1215

16+
const (
17+
// Rollout is a representative built-in health script: ~5 KB of Lua with helper
18+
// functions and multiple code paths, similar in complexity to hot-path checks.
19+
scriptCacheBenchScriptPath = "../../resource_customizations/argoproj.io/Rollout/health.lua"
20+
scriptCacheBenchObjPath = "../../resource_customizations/argoproj.io/Rollout/testdata/canary/healthy_executedAllSteps.yaml"
21+
)
22+
1323
const cacheTestScriptA = `local hs = {}
1424
hs.status = "Healthy"
1525
hs.message = "a"
@@ -81,3 +91,50 @@ func TestCompiledScriptCache_OnOffParity(t *testing.T) {
8191

8292
assert.Equal(t, off, on, "cache must not change health output")
8393
}
94+
95+
func loadScriptCacheBenchFixtures(tb testing.TB) (script string, obj *unstructured.Unstructured) {
96+
tb.Helper()
97+
scriptBytes, err := os.ReadFile(scriptCacheBenchScriptPath)
98+
require.NoError(tb, err)
99+
yamlBytes, err := os.ReadFile(scriptCacheBenchObjPath)
100+
require.NoError(tb, err)
101+
objMap := make(map[string]any)
102+
require.NoError(tb, yaml.Unmarshal(yamlBytes, &objMap))
103+
return string(scriptBytes), &unstructured.Unstructured{Object: objMap}
104+
}
105+
106+
func BenchmarkExecuteHealthLuaScriptCache(b *testing.B) {
107+
script, obj := loadScriptCacheBenchFixtures(b)
108+
vm := VM{}
109+
110+
for _, mode := range []struct {
111+
name string
112+
enabled bool
113+
}{
114+
{"cacheOff", false},
115+
{"cacheOn", true},
116+
} {
117+
b.Run(mode.name, func(b *testing.B) {
118+
origEnabled := scriptCacheEnabled
119+
origCache := compiledScripts
120+
scriptCacheEnabled = mode.enabled
121+
if mode.enabled {
122+
compiledScripts = newCompiledScriptCache()
123+
if _, err := vm.ExecuteHealthLua(obj, script); err != nil {
124+
b.Fatal(err)
125+
}
126+
}
127+
b.Cleanup(func() {
128+
scriptCacheEnabled = origEnabled
129+
compiledScripts = origCache
130+
})
131+
132+
b.ReportAllocs()
133+
for b.Loop() {
134+
if _, err := vm.ExecuteHealthLua(obj, script); err != nil {
135+
b.Fatal(err)
136+
}
137+
}
138+
})
139+
}
140+
}

0 commit comments

Comments
 (0)