benchmark-financial-submit-payload.js
4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* 离线估算财务规则保存接口 JSON 体积与 JSON.stringify 耗时。
* 用法:node scripts/benchmark-financial-submit-payload.js
* 可按需修改 RULE_COUNT / USE_HEAVY_RULE 贴近生产。
*
* 参考(Node 一次跑数,轻量规则约 12 条分账行/ tab):1000 tab ≈ 4.2MB UTF-8,stringify 约数十 ms 级;
* 浏览器主线程略慢,且真实类目/ nested children 更大时需以 USE_HEAVY_RULE=true 再测。
*/
const RULE_COUNT = 1000
const USE_HEAVY_RULE = process.argv.includes("--heavy")
const sampleRuleLight = {
expenseCategoryRules: [
{
expenseCategoryCalculateType: 1,
expenseCategoryValue: 2,
expenseCategoryId: 1,
expenseCategoryName: "流量费",
formType: 1,
calculateMode: 1,
nodeType: 1,
targetType: 1,
children: []
}
],
shop4sDeductChargeFlag: "Y",
partnerDeductChargeFlag: "Y",
virtualAccountCostFlag: "Y",
recoveryCostFlag: "Y",
groupDivisionRuleId: 38108,
djCostFlag: "Y",
thirdPartyCostFlag: "Y",
goodsCostFlag: "Y",
goodsSource: 1,
goodsCostConvertRate: 100,
virtualAccountCostConvertRate: 100,
recoveryCostConvertRate: 100,
djCostConvertRate: 100,
thirdPartyCostConvertRate: 100,
groupType: 2,
goodsTypes: [],
merchantDistributionRules: Array.from({ length: 12 }, (_, i) => ({
applicationObjectType: i % 3 || 1,
id: i,
expenseCategoryId: i,
applicationObjectName: `项${i}`,
merchantId: "-1",
merchantName: "示例",
targetMerchantType: 1
})),
owningMerchantDistributionRules: Array.from({ length: 12 }, (_, i) => ({
applicationObjectType: i % 3 || 1,
id: i,
expenseCategoryId: i,
applicationObjectName: `项${i}`,
merchantId: "-1",
merchantName: "示例",
targetMerchantType: 1
}))
}
/** 更接近接口示例:多条 expenseCategory + children + 两套分账表各 ~12 行 */
const sampleRuleHeavy = {
expenseCategoryRules: [
{
expenseCategoryCalculateType: 1,
expenseCategoryValue: 2,
expenseCategoryId: 1,
expenseCategoryName: "流量费",
formType: 1,
calculateMode: 1,
nodeType: 1,
targetType: 1,
targetMerchant: {},
children: []
},
{
expenseCategoryCalculateType: 2,
expenseCategoryValue: 2,
expenseCategoryId: 302,
expenseCategoryName: "资源费",
formType: 1,
calculateMode: 1,
nodeType: 1,
targetType: 1,
children: []
},
{
expenseCategoryCalculateType: 1,
expenseCategoryValue: 2,
expenseCategoryId: 1151,
expenseCategoryName: "自定义类目2",
formType: 2,
calculateMode: 1,
nodeType: 2,
targetType: 2,
targetMerchant: {},
children: [
{
expenseCategoryCalculateType: 3,
expenseCategoryValue: 222,
expenseCategoryId: 1152,
expenseCategoryName: "自定义1",
calculateMode: 1,
nodeType: 3,
categoryToSort: 1,
targetType: 2,
targetMerchant: {}
},
{
expenseCategoryCalculateType: "",
expenseCategoryValue: "",
expenseCategoryId: 1153,
expenseCategoryName: "自定义2",
calculateMode: 1,
nodeType: 4,
categoryToSort: 2,
targetType: 2,
targetMerchant: {}
}
]
}
],
shop4sDeductChargeFlag: "Y",
partnerDeductChargeFlag: "Y",
virtualAccountCostFlag: "Y",
recoveryCostFlag: "Y",
groupDivisionRuleId: 38108,
djCostFlag: "Y",
thirdPartyCostFlag: "Y",
goodsCostFlag: "Y",
goodsSource: 1,
goodsCostConvertRate: 100,
virtualAccountCostConvertRate: 100,
recoveryCostConvertRate: 100,
djCostConvertRate: 100,
thirdPartyCostConvertRate: 100,
groupType: 2,
goodsTypes: [{ goodsType: 4, goodsTypeName: "示例品类", categoryType: 1 }],
goodsTypeGroupRelations: [{ goodsType: 4, goodsTypeName: "示例品类", categoryType: 1 }],
merchantDistributionRules: sampleRuleLight.merchantDistributionRules,
owningMerchantDistributionRules: sampleRuleLight.owningMerchantDistributionRules
}
const sampleRule = USE_HEAVY_RULE ? sampleRuleHeavy : sampleRuleLight
const payload = {
merchantId: 44218596,
realityPlatformMerchantId: "-1",
settleRuleId: 6058,
divisionRuleId: 6016,
goodsTypeGroupRules: Array.from({ length: RULE_COUNT }, (_, i) => ({
...sampleRule,
groupDivisionRuleId: 38100 + i,
groupType: i === 0 ? 1 : 2
})),
divisionRuleAutos: [],
autoFlag: "N",
settleTimeType: 1,
sceneType: 1
}
const t0 = Date.now()
const json = JSON.stringify(payload)
const ms = Date.now() - t0
const utf8Bytes = Buffer.byteLength(json, "utf8")
// eslint-disable-next-line no-console
console.log(JSON.stringify({
ruleCount: RULE_COUNT,
heavy: USE_HEAVY_RULE,
stringifyMs: ms,
jsonChars: json.length,
utf8Bytes,
utf8MB: (utf8Bytes / (1024 * 1024)).toFixed(2)
}, null, 2))