Skip to content

Commit 41ef507

Browse files
matheusdsauerbraten
authored andcommitted
eval/test: Add benchmarks for fn and pipeline execution
This demonstrates function calling is cheaper than pipeline execution in the current implementation.
1 parent 0683ae6 commit 41ef507

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

eval_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ func init() {
8383
}
8484

8585
JetTestingSet.AddGlobal("dummy", dummy)
86+
JetTestingSet.AddGlobalFunc("customFn", func(args Arguments) reflect.Value {
87+
args.RequireNumOfArguments("customFn", 1, 1)
88+
return args.Get(0)
89+
})
90+
8691
JetTestingLoader.Set("actionNode_dummy", `hello {{dummy("WORLD")}}`)
8792
JetTestingLoader.Set("noAllocFn", `hello {{ "José" }} {{1}} {{ "José" }}`)
8893
JetTestingLoader.Set("rangeOverUsers", `{{range .}}{{.Name}}-{{.Email}}{{end}}`)
@@ -91,6 +96,8 @@ func init() {
9196
JetTestingLoader.Set("BenchCustomRanger", "{{range .}}{{.Name}}{{end}}")
9297
JetTestingLoader.Set("BenchIntsRanger", "{{range ints(0, .)}} {{end}}")
9398
JetTestingLoader.Set("BenchCustomRender", "{{range k, v := ints(0, .N)}}{{.Field}}{{end}}")
99+
JetTestingLoader.Set("BenchCallCustomFn", "{{range ints(0, .N)}}{{customFn(.)}}{{end}}")
100+
JetTestingLoader.Set("BenchExecPipeline", "{{range ints(0, .N)}}{{. | customFn}}{{end}}")
94101
}
95102

96103
func RunJetTest(t *testing.T, variables VarMap, context interface{}, testName, testContent, testExpected string) {
@@ -970,6 +977,32 @@ func BenchmarkCustomRender(b *testing.B) {
970977
}
971978
}
972979

980+
// BenchmarkCallCustomFn benchmarks executing a template that calls a custom
981+
// function repeatedly.
982+
func BenchmarkCallCustomFn(b *testing.B) {
983+
t, _ := JetTestingSet.GetTemplate("BenchCallCustomFn")
984+
execCtx := struct{ N int }{N: b.N}
985+
b.ResetTimer()
986+
err := t.Execute(ww, nil, execCtx)
987+
if err != nil {
988+
b.Error(err.Error())
989+
}
990+
991+
}
992+
993+
// BenchmarkExecPipeline benchmarks executing a template that calls a pipeline
994+
// repeatedly.
995+
func BenchmarkExecPipeline(b *testing.B) {
996+
t, _ := JetTestingSet.GetTemplate("BenchExecPipeline")
997+
execCtx := struct{ N int }{N: b.N}
998+
b.ResetTimer()
999+
err := t.Execute(ww, nil, execCtx)
1000+
if err != nil {
1001+
b.Error(err.Error())
1002+
}
1003+
1004+
}
1005+
9731006
// BenchmarkFieldAccess benchmarks executing a template that accesses fields
9741007
// in the current context.
9751008
//

0 commit comments

Comments
 (0)