Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

加载和执行万条规则时内存优化 #28

Open
5idu opened this issue Dec 20, 2024 · 0 comments
Open

加载和执行万条规则时内存优化 #28

5idu opened this issue Dec 20, 2024 · 0 comments

Comments

@5idu
Copy link

5idu commented Dec 20, 2024

加载万条规则

  • 测试代码
func BenchmarkLoadRule(b *testing.B) {
	ruleNames := []string{}
	conditions := []string{}
	actions := []string{}
	for i := 0; i < 10000; i++ {
		ruleNames = append(ruleNames, fmt.Sprintf("rule%d", i))
		conditions = append(conditions, fmt.Sprintf(`{"OpLogic":"&&","Conditions":[{"Operator":"==","Lhs":{"VarExpr":"fromId"},"Rhs":{"Const":{"StrConst":"HuangShan"}}},{"Operator":"LIST_IN","Lhs":{"VarExpr":"customerGroupId"},"Rhs":{"ConstList":[{"StrConst":"10549"},{"StrConst":"%d"}]}}]}`, i+1))
		actions = append(actions, fmt.Sprintf(`{"ActionName":"Greeting","ParamMap":{"SupplyType":{"Const":{"StrConst":"黄山%d"}}}}`, i+1))
	}

	for i := 0; i < b.N; i++ {
		for i := 0; i < 10000; i++ {
			arishem.NewPriorityRule(ruleNames[i], i+1, conditions[i], actions[i])
		}
	}
}
go test -benchmem -run=^$ -bench ^BenchmarkNewPriorityRule$ antrl/test
  • 输出如下
Running tool: /usr/local/go/bin/go test -benchmem -run=^$ -bench ^BenchmarkLoadRule$ antrl/test

goos: linux
goarch: amd64
pkg: antrl/test
cpu: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
BenchmarkLoadRule-8   	       1	13479184237 ns/op	7858983688 B/op	153870732 allocs/op
PASS
ok  	antrl/test	13.492s

加载五千条规则后执行匹配

  • 测试代码
func BenchmarkExecuteRule(b *testing.B) {
	ruleNames := []string{}
	conditions := []string{}
	actions := []string{}
	for i := 0; i < 10000; i++ {
		ruleNames = append(ruleNames, fmt.Sprintf("rule%d", i))
		conditions = append(conditions, fmt.Sprintf(`{"OpLogic":"&&","Conditions":[{"Operator":"==","Lhs":{"VarExpr":"fromId"},"Rhs":{"Const":{"StrConst":"HuangShan"}}},{"Operator":"LIST_IN","Lhs":{"VarExpr":"customerGroupId"},"Rhs":{"ConstList":[{"StrConst":"10549"},{"StrConst":"%d"}]}}]}`, i+1))
		actions = append(actions, fmt.Sprintf(`{"ActionName":"Greeting","ParamMap":{"SupplyType":{"Const":{"StrConst":"黄山%d"}}}}`, i+1))
	}

	count := 5000
	rules := make([]arishem.RuleTarget, count)
	for i := 0; i < count; i++ {
		rule, _ := arishem.NewPriorityRule(ruleNames[i], i+1, conditions[i], actions[i])
		rules[i] = rule
	}

	for i := 0; i < b.N; i++ {
		dc, err := arishem.DataContext(context.Background(), `
{
    "fromId": "HuangShan",
    "customerGroupId": "10549",
	"provinceId": "157",
	"supplyType": "",
    "item_list": [
        {
            "name": "name@1",
            "price": 1
        },
        {
            "name": "name@2",
             "price": 102.13
        }
    ]
}
`)
		if err != nil {
			panic(err)
		}
		arishem.ExecuteRules(rules, dc)
	}
}
go test -benchmem -run=^$ -bench ^BenchmarkExecuteRule$ antrl/test
  • 输出如下
Running tool: /usr/local/go/bin/go test -benchmem -run=^$ -bench ^BenchmarkExecuteRule$ antrl/test

goos: linux
goarch: amd64
pkg: antrl/test
cpu: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
BenchmarkExecuteRule-8   	       1	7063361703 ns/op	3931025664 B/op	76959585 allocs/op
PASS
ok  	antrl/test	7.095s

请问为什么当规则条数非常大时,内存占用如此高,有什么手段可以优化吗,谢谢

@5idu 5idu changed the title 加载万条规则时内存优化 加载和执行万条规则时内存优化 Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant