benchmark-financial-submit-payload.js 4.87 KB
/**
 * 离线估算财务规则保存接口 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))