Skip to content

Instantly share code, notes, and snippets.

@bothra90
Last active October 1, 2021 16:47
Show Gist options
  • Save bothra90/fb1798ee655c0e5ed02a45acf3a40ef7 to your computer and use it in GitHub Desktop.
Save bothra90/fb1798ee655c0e5ed02a45acf3a40ef7 to your computer and use it in GitHub Desktop.
CEL Benchmark in Go
package main
import (
"log"
"time"
"github.com/google/cel-go/cel"
"github.com/google/cel-go/checker/decls"
)
func bench_native() {
var r float64
failure := 2000.0
count := 10000.0
start := time.Now()
for i := 0; i < 10_000; i++ {
r = failure / count
}
_ = r
end := time.Now()
log.Printf("Duration native: %s", end.Sub(start))
}
func bench_cel() {
env, err := cel.NewEnv(
cel.Declarations(
decls.NewVar("failure", decls.Double),
decls.NewVar("count", decls.Double)))
if err != nil {
log.Fatalf("%v", err)
}
ast, issues := env.Compile("failure/count")
if issues != nil && issues.Err() != nil {
log.Fatalf("type-check error: %s", issues.Err())
}
prg, err := env.Program(ast)
if err != nil {
log.Fatalf("program construction error: %s", err)
}
params := make(map[string]interface{})
start := time.Now()
for i := 0; i < 10_000; i++ {
params["failure"] = 200.0
params["count"] = 1000.0
_, details, err := prg.Eval(params)
if err != nil {
log.Fatalf("Err: %v, Details; %v", err, details)
}
}
end := time.Now()
log.Printf("Duration CEL: %s", end.Sub(start))
}
func main() {
bench_native()
bench_cel()
}
@bothra90
Copy link
Author

bothra90 commented Oct 1, 2021

Output:

2021/10/01 00:45:06 Duration native: 32.505µs
2021/10/01 00:45:06 Duration CEL: 21.494471ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment