1 Star 2 Fork 1

zhijingtech/expr

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
expr_benchmark_test.go 1.73 KB
一键复制 编辑 原始数据 按行查看 历史
ludanfeng 提交于 2024-12-25 12:17 +08:00 . feat():#none: convert to native type
package expr
import (
"testing"
"github.com/google/cel-go/cel"
"github.com/stretchr/testify/assert"
"github.com/zhijingtech/expr/testdata"
)
const (
exprstr = "(current.P1.X-prev.P2.X) <= 1.0 || (current.P1.Y-prev.P2.Y) <= 1.0"
)
//go:generate protoc -I=. --go_out=. testdata/model.proto
func BenchmarkCelGoMap(b *testing.B) {
env, err := NewEnv(
cel.Variable("current", cel.MapType(cel.StringType, cel.AnyType)),
cel.Variable("prev", cel.MapType(cel.StringType, cel.AnyType)),
)
assert.NoError(b, err)
expr, err := NewExpr(exprstr, env)
assert.NoError(b, err)
prevMap := map[string]any{
"P1": map[string]any{"X": 1, "Y": 2},
"P2": map[string]any{"X": 3, "Y": 4},
}
currentMap := map[string]any{
"P1": map[string]any{"X": 5, "Y": 3},
"P2": map[string]any{"X": 7, "Y": 5},
}
iparams := map[string]any{
"current": currentMap,
"prev": prevMap,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
result, err := expr.Eval(iparams)
assert.NoError(b, err)
assert.Equal(b, true, result)
}
}
func BenchmarkCelGoProtoBuf(b *testing.B) {
env, err := NewEnv(
cel.Types(&testdata.Rectangle{}),
cel.Variable("prev", cel.ObjectType("testdata.Rectangle")),
cel.Variable("current", cel.ObjectType("testdata.Rectangle")),
)
assert.NoError(b, err)
expr, err := NewExpr(exprstr, env)
assert.NoError(b, err)
prev := &testdata.Rectangle{
P1: &testdata.Point{X: 1, Y: 2},
P2: &testdata.Point{X: 3, Y: 4},
}
current := &testdata.Rectangle{
P1: &testdata.Point{X: 5, Y: 3},
P2: &testdata.Point{X: 7, Y: 5},
}
iparams := map[string]any{
"prev": prev,
"current": current,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
result, err := expr.Eval(iparams)
assert.NoError(b, err)
assert.Equal(b, true, result)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/zhijingtech/expr.git
git@gitee.com:zhijingtech/expr.git
zhijingtech
expr
expr
main

搜索帮助

371d5123 14472233 46e8bd33 14472233