Skip to content

Commit

Permalink
eval/test: Add benchmarks for fn and pipeline execution
Browse files Browse the repository at this point in the history
This demonstrates function calling is cheaper than pipeline execution in
the current implementation.
  • Loading branch information
matheusd authored and sauerbraten committed Dec 16, 2022
1 parent 0683ae6 commit 41ef507
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ func init() {
}

JetTestingSet.AddGlobal("dummy", dummy)
JetTestingSet.AddGlobalFunc("customFn", func(args Arguments) reflect.Value {
args.RequireNumOfArguments("customFn", 1, 1)
return args.Get(0)
})

JetTestingLoader.Set("actionNode_dummy", `hello {{dummy("WORLD")}}`)
JetTestingLoader.Set("noAllocFn", `hello {{ "José" }} {{1}} {{ "José" }}`)
JetTestingLoader.Set("rangeOverUsers", `{{range .}}{{.Name}}-{{.Email}}{{end}}`)
Expand All @@ -91,6 +96,8 @@ func init() {
JetTestingLoader.Set("BenchCustomRanger", "{{range .}}{{.Name}}{{end}}")
JetTestingLoader.Set("BenchIntsRanger", "{{range ints(0, .)}} {{end}}")
JetTestingLoader.Set("BenchCustomRender", "{{range k, v := ints(0, .N)}}{{.Field}}{{end}}")
JetTestingLoader.Set("BenchCallCustomFn", "{{range ints(0, .N)}}{{customFn(.)}}{{end}}")
JetTestingLoader.Set("BenchExecPipeline", "{{range ints(0, .N)}}{{. | customFn}}{{end}}")
}

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

// BenchmarkCallCustomFn benchmarks executing a template that calls a custom
// function repeatedly.
func BenchmarkCallCustomFn(b *testing.B) {
t, _ := JetTestingSet.GetTemplate("BenchCallCustomFn")
execCtx := struct{ N int }{N: b.N}
b.ResetTimer()
err := t.Execute(ww, nil, execCtx)
if err != nil {
b.Error(err.Error())
}

}

// BenchmarkExecPipeline benchmarks executing a template that calls a pipeline
// repeatedly.
func BenchmarkExecPipeline(b *testing.B) {
t, _ := JetTestingSet.GetTemplate("BenchExecPipeline")
execCtx := struct{ N int }{N: b.N}
b.ResetTimer()
err := t.Execute(ww, nil, execCtx)
if err != nil {
b.Error(err.Error())
}

}

// BenchmarkFieldAccess benchmarks executing a template that accesses fields
// in the current context.
//
Expand Down

0 comments on commit 41ef507

Please sign in to comment.