-
-
Save Jun10ng/b7be36c97d092ed5a7f4d1b2e5d608e1 to your computer and use it in GitHub Desktop.
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 go_rule_eng_research | |
import ( | |
"testing" | |
"github.com/bilibili/gengine/engine" | |
) | |
// 引入规则引擎造成的性能损耗 | |
type ReqStruct struct { | |
A string | |
B string | |
C *ReqStruct | |
} | |
func newReq() *ReqStruct { | |
req := &ReqStruct{ | |
A: "A", | |
B: "B", | |
C: &ReqStruct{"A", "B", nil}, | |
} | |
return req | |
} | |
func BenchmarkGo(b *testing.B) { | |
req := newReq() | |
b.ResetTimer() // 重置计时器,忽略前面的准备时间 | |
for n := 0; n < b.N; n++ { | |
ValidateByGo(req) | |
} | |
} | |
func ValidateByGo(req *ReqStruct) bool { | |
if req.A != "A" { | |
return false | |
} | |
if req.B != "B" { | |
return false | |
} | |
if req.C.A != "A" { | |
return false | |
} | |
if req.C.B != "B" { | |
return false | |
} | |
return true | |
} | |
const rulereq = ` | |
rule "rule1" | |
begin | |
if req.A != "A"{return "A"} | |
return "" | |
end | |
rule "rule2" | |
begin | |
if req.B != "B"{return "B"} | |
return "" | |
end | |
rule "rule3" | |
begin | |
if req.C.A != "A"{return "CA"} | |
return "" | |
end | |
rule "rule4" | |
begin | |
if req.C.B != "B"{return "CB"} | |
return "" | |
end | |
` | |
var pool =&engine.GenginePool{} | |
func BenchmarkGengine(b *testing.B) { | |
req := newReq() | |
var err error | |
pool, err = engine.NewGenginePool(10, 100, 2, rulereq, make(map[string]interface{})) | |
if err != nil { | |
b.Fatal(err) | |
} | |
b.ResetTimer() // 重置计时器,忽略前面的准备时间 | |
for n := 0; n < b.N; n++ { | |
_ = ValidateByGengine(req) | |
} | |
} | |
func ValidateByGengine(req *ReqStruct) bool { | |
datamap:= map[string]interface{}{"req":req} | |
_, res := pool.ExecuteConcurrent(datamap) | |
for _, v := range res { | |
if v != "" { | |
return false | |
} | |
} | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment