Created
March 15, 2020 15:53
-
-
Save ozanh/368c6383b4e5a90246a829e85217abd2 to your computer and use it in GitHub Desktop.
Benchmark of Tengo VM callable args allocation
This file contains 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
package tengo_test | |
import ( | |
"testing" | |
"github.com/d5/tengo/v2" | |
) | |
func BenchmarkCallAlloc(b *testing.B) { | |
scriptNoArg := ` | |
for i := 0; i < N; i++ { | |
callable() | |
} | |
` | |
script1Arg := ` | |
for i := 0; i < N; i++ { | |
callable(i) | |
} | |
` | |
script2Arg := ` | |
for i := 0; i < N; i++ { | |
callable(i, i) | |
} | |
` | |
var callable = func() *tengo.UserFunction { | |
return &tengo.UserFunction{Name: "user_func", | |
Value: func(args ...tengo.Object) (tengo.Object, error) { | |
return nil, nil | |
}} | |
} | |
b.Run("NoArg", func(b *testing.B) { | |
s := tengo.NewScript([]byte(scriptNoArg)) | |
s.Add("N", b.N) | |
s.Add("callable", callable()) | |
c, err := s.Compile() | |
if err != nil { | |
b.Fatal(err) | |
} | |
b.ResetTimer() | |
err = c.Run() | |
b.StopTimer() | |
if err != nil { | |
b.Fatal(err) | |
} | |
}) | |
b.Run("1Arg", func(b *testing.B) { | |
s := tengo.NewScript([]byte(script1Arg)) | |
s.Add("N", b.N) | |
s.Add("callable", callable()) | |
c, err := s.Compile() | |
if err != nil { | |
b.Fatal(err) | |
} | |
b.ResetTimer() | |
err = c.Run() | |
b.StopTimer() | |
if err != nil { | |
b.Fatal(err) | |
} | |
}) | |
b.Run("2Arg", func(b *testing.B) { | |
s := tengo.NewScript([]byte(script2Arg)) | |
s.Add("N", b.N) | |
s.Add("callable", callable()) | |
c, err := s.Compile() | |
if err != nil { | |
b.Fatal(err) | |
} | |
b.ResetTimer() | |
err = c.Run() | |
b.StopTimer() | |
if err != nil { | |
b.Fatal(err) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment