package valuate import ( "fmt" "testing" "github.com/Knetic/govaluate" "github.com/spf13/cast" ) func TestExpression1(t *testing.T) { expr, err := govaluate.NewEvaluableExpression("(requests_made * requests_succeeded / 100) >= 90") if err != nil { t.Error(err) } params := make(map[string]any, 8) params["requests_made"] = 100 params["requests_succeeded"] = 80 result, err := expr.Evaluate(params) fmt.Println(result, err) } func testExpression() { expression, _ := govaluate.NewEvaluableExpression("(mem_used / total_mem) * 100") parameters := map[string]any{ "total_mem": 1024, "mem_used": 512, } evaluate, err := expression.Evaluate(parameters) if err != nil { return } result := cast.ToInt32(evaluate) fmt.Println(result) } func TestExpression2(t *testing.T) { testExpression() } func BenchmarkEvaluate(b *testing.B) { for i := 0; i < b.N; i++ { testExpression() } } func BenchmarkEvaluate1(b *testing.B) { var x float64 for i := 0; i < b.N; i++ { x = (1024 / 512) * 100 } if x == 300 { fmt.Println(x) } } func testFunction() { functions := map[string]govaluate.ExpressionFunction{ "strlen": func(args ...any) (any, error) { length := len(args[0].(string)) return (float64)(length), nil }, } expString := "strlen('someReallyLongInputString') <= 16" expression, _ := govaluate.NewEvaluableExpressionWithFunctions(expString, functions) //result, err := expression.Evaluate(nil) //fmt.Println(result, err) expression.Evaluate(nil) } func TestFunction(t *testing.T) { testFunction() } func BenchmarkFunction(b *testing.B) { for i := 0; i < b.N; i++ { testFunction() } }