Last active
October 1, 2021 16:47
-
-
Save bothra90/fb1798ee655c0e5ed02a45acf3a40ef7 to your computer and use it in GitHub Desktop.
CEL Benchmark in Go
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 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() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: