Skip to content

Commit

Permalink
chore(perf): add benchmark for tm_alltrue and tm_anytrue.
Browse files Browse the repository at this point in the history
Signed-off-by: Tiago Natel <[email protected]>
  • Loading branch information
i4ki committed Dec 5, 2023
1 parent 21255cb commit 121a035
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions stdlib/funcs_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,74 @@ import (
"github.com/terramate-io/terramate/test"
)

func BenchmarkTmAllTrueLiteralList(b *testing.B) {
b.StopTimer()
evalctx := eval.NewContext(stdlib.Functions(test.TempDir(b)))
expr, err := ast.ParseExpression(`tm_alltrue([
false,
tm_element(tm_range(0, 100), 0) == 0,
tm_length(tm_distinct([for i in tm_range(0, 100): 0*i]))==1,
])`, `bench-test`)
assert.NoError(b, err)
b.StartTimer()
for n := 0; n < b.N; n++ {
v, err := evalctx.Eval(expr)
assert.NoError(b, err)
if got := v.True(); got {
b.Fatalf("unexpected value: %t", got)
}
}
}

func BenchmarkTmAllTrueFuncall(b *testing.B) {
b.StopTimer()
evalctx := eval.NewContext(stdlib.Functions(test.TempDir(b)))
expr, err := ast.ParseExpression(`tm_alltrue(tm_distinct([for i in tm_range(0, 3) : i == 2 ? true : false]))`, `bench-test`)
assert.NoError(b, err)
b.StartTimer()
for n := 0; n < b.N; n++ {
v, err := evalctx.Eval(expr)
assert.NoError(b, err)
if got := v.True(); got {
b.Fatalf("unexpected value: %t", got)
}
}
}

func BenchmarkTmAnyTrueLiteralList(b *testing.B) {
b.StopTimer()
evalctx := eval.NewContext(stdlib.Functions(test.TempDir(b)))
expr, err := ast.ParseExpression(`tm_anytrue([
true,
tm_element(tm_range(0, 100), 0) != 0,
tm_length(tm_distinct([for i in tm_range(0, 100): 2*i]))>1,
])`, `bench-test`)
assert.NoError(b, err)
b.StartTimer()
for n := 0; n < b.N; n++ {
v, err := evalctx.Eval(expr)
assert.NoError(b, err)
if got := v.True(); !got {
b.Fatalf("unexpected value: %t", got)
}
}
}

func BenchmarkTmAnyTrueFuncall(b *testing.B) {
b.StopTimer()
evalctx := eval.NewContext(stdlib.Functions(test.TempDir(b)))
expr, err := ast.ParseExpression(`tm_anytrue(tm_distinct([for i in tm_range(0, 3) : i == 2 ? true : false]))`, `bench-test`)
assert.NoError(b, err)
b.StartTimer()
for n := 0; n < b.N; n++ {
v, err := evalctx.Eval(expr)
assert.NoError(b, err)
if got := v.True(); !got {
b.Fatalf("unexpected value: %t", got)
}
}
}

func BenchmarkTmTernary(b *testing.B) {
b.StopTimer()
evalctx := eval.NewContext(stdlib.Functions(test.TempDir(b)))
Expand Down

0 comments on commit 121a035

Please sign in to comment.