/** * 测试 DMS 发放明细条件查询 vs 全表翻页 * node scripts/bench-dms-payment-query.js */ const http = require("http"); const qs = require("querystring"); const HOST = "121.43.55.7"; const PORT = 2101; const COLUMN_ID = 1849; const FFMX_TABLE = "column_sjnmtybt_ffmx_payment_detail"; function req(urlPath, fields, token) { return new Promise((resolve, reject) => { const body = qs.stringify(fields || {}); const headers = { "Content-Type": "application/x-www-form-urlencoded", "Content-Length": Buffer.byteLength(body), }; if (token) { headers.Token = token; headers.token = token; } const t0 = Date.now(); const r = http.request( { hostname: HOST, port: PORT, path: urlPath, method: "POST", headers }, (res) => { let data = ""; res.on("data", (c) => (data += c)); res.on("end", () => { const ms = Date.now() - t0; try { resolve({ ms, data: JSON.parse(data) }); } catch (e) { resolve({ ms, data: { raw: data.slice(0, 500), status: res.statusCode } }); } }); } ); r.on("error", reject); r.write(body); r.end(); }); } async function login() { const res = await req("/proxy_oauth/user/login", { userName: "user_liu", password: "WE176852439@lmx", clientId: "1", }); if (res.data.code != 200) throw new Error("login fail: " + JSON.stringify(res.data)); return res.data.message; } async function selectList(token, extra) { const fields = Object.assign( { columnId: String(COLUMN_ID), page: "0", pageSize: "100", states: "0", }, extra || {} ); return req("/proxy_dms/content/selectContentList", fields, token); } async function groupBy(token, field, paramJson) { const fields = { columnId: String(COLUMN_ID), field }; if (paramJson) fields.paramJson = JSON.stringify(paramJson); return req("/proxy_dms/content/selectGroupByCountOrderBy", fields, token); } function listSize(data) { if (!data) return 0; if (Array.isArray(data.content)) return data.content.length; const inner = data.content && data.content.data; if (Array.isArray(inner)) return inner.length; return 0; } function listCount(data) { if (!data || !data.content) return 0; if (typeof data.content.count === "number") return data.content.count; return listSize(data); } function firstRow(data) { const inner = data && data.content && data.content.data; if (Array.isArray(inner) && inner.length) return inner[0]; if (Array.isArray(data.content) && data.content.length) return data.content[0]; return {}; } async function countAllPages(token, extra, maxPages) { let total = 0; let page = 0; const t0 = Date.now(); while (page < (maxPages || 500)) { const fields = Object.assign( { columnId: String(COLUMN_ID), page: String(page), pageSize: "100", states: "0" }, extra || {} ); const { data, ms } = await req("/proxy_dms/content/selectContentList", fields, token); if (data.code === 202) { return { total, ms: Date.now() - t0, pages: page, lastPageMs: ms }; } if (data.code !== 200 && data.code !== 0) { throw new Error("select fail page " + page + ": " + JSON.stringify(data).slice(0, 300)); } const n = listSize(data); total += n; if (n < 100) { return { total, ms: Date.now() - t0, pages: page + 1, lastPageMs: ms }; } page++; } return { total, ms: Date.now() - t0, pages: maxPages, truncated: true }; } async function main() { console.log("=== DMS 发放明细查询压测 ==="); console.log("表名:", FFMX_TABLE); console.log("columnId:", COLUMN_ID); console.log("网关:", `http://${HOST}:${PORT}/proxy_dms`); console.log(""); const loginRes = await req("/proxy_oauth/user/login", { userName: "user_liu", password: "WE176852439@lmx", clientId: "1", }); console.log("登录:", loginRes.ms + "ms", "code=", loginRes.data.code); if (loginRes.data.code != 200) { console.error(JSON.stringify(loginRes.data)); process.exit(1); } const token = loginRes.data.message; // 取样:取 1 条拿 pay_month / batch_id / user_id const sample = await selectList(token, { pageSize: "1", states: "0,3" }); console.log("\n[1] 取样(无 search, states=0,3, pageSize=1):", sample.ms + "ms", "rows=", listSize(sample.data), "count=", listCount(sample.data)); const row = firstRow(sample.data); const payMonth = row.c_pay_month || "202608"; const batchId = row.c_batch_id || ""; const userId = row.c_user_id || ""; console.log(" 样本 c_pay_month=", payMonth, "c_batch_id=", batchId, "c_user_id=", userId); // 无 search 单页 const page0 = await selectList(token, { states: "0,3" }); console.log("\n[2] 无 search 单页(100条, states=0,3):", page0.ms + "ms", "rows=", listSize(page0.data), "count=", listCount(page0.data)); // 无 states 限制(仅 state!=4) const pageNoState = await req("/proxy_dms/content/selectContentList", { columnId: String(COLUMN_ID), page: "0", pageSize: "100", }, token); console.log("\n[2b] 无 states 参数 单页:", pageNoState.ms + "ms", "rows=", listSize(pageNoState.data), "count=", listCount(pageNoState.data)); // search 等值 pay_month const searchPayMonth = JSON.stringify([ { field: "c_pay_month", searchType: 1, content: { value: payMonth } }, ]); const byMonth = await selectList(token, { search: searchPayMonth, pageSize: "100", states: "0,3" }); console.log("\n[3] search c_pay_month=", payMonth, ":", byMonth.ms + "ms", "rows=", listSize(byMonth.data), "count=", listCount(byMonth.data)); // search batch_id if (batchId) { const searchBatch = JSON.stringify([ { field: "c_batch_id", searchType: 1, content: { value: batchId } }, ]); const byBatch = await selectList(token, { search: searchBatch, pageSize: "100", states: "0,3" }); console.log("\n[4] search c_batch_id=", batchId, ":", byBatch.ms + "ms", "rows=", listSize(byBatch.data), "count=", listCount(byBatch.data)); } // search user_id if (userId) { const searchUser = JSON.stringify([ { field: "c_user_id", searchType: 1, content: { value: userId } }, ]); const byUser = await selectList(token, { search: searchUser, pageSize: "100", states: "0,3" }); console.log("\n[5] search c_user_id=", userId, ":", byUser.ms + "ms", "rows=", listSize(byUser.data), "count=", listCount(byUser.data)); } // town_id if present const townId = row.c_town_id; if (townId) { const searchTown = JSON.stringify([ { field: "c_pay_month", searchType: 1, content: { value: payMonth } }, { field: "c_town_id", searchType: 1, content: { value: townId } }, ]); const byTown = await selectList(token, { search: searchTown, pageSize: "100", states: "0,3" }); console.log("\n[6] search pay_month+town:", byTown.ms + "ms", "rows=", listSize(byTown.data), "count=", listCount(byTown.data)); } // 聚合:按发放月分组计数 const groupRes = await groupBy(token, "c_pay_month"); console.log("\n[7] selectGroupByCountOrderBy(c_pay_month):", groupRes.ms + "ms"); const groups = groupRes.data.content || groupRes.data; console.log(" 结果:", JSON.stringify(groups).slice(0, 400)); const groupFiltered = await groupBy(token, "c_town_id", { c_pay_month: payMonth }); console.log("\n[7b] groupBy town (filter pay_month):", groupFiltered.ms + "ms", "groups=", Array.isArray(groupFiltered.data.content) ? groupFiltered.data.content.length : "?"); // 全表翻页计数 console.log("\n[8] 全表翻页统计(states=0,3, 无 search)..."); const full = await countAllPages(token, { states: "0,3" }, 500); console.log(" 总条数:", full.total, "页数:", full.pages, "总耗时:", full.ms + "ms"); console.log("\n[9] 按 c_pay_month 条件翻页统计(states=0,3)..."); const filtered = await countAllPages(token, { search: searchPayMonth, states: "0,3" }, 50); console.log(" 总条数:", filtered.total, "页数:", filtered.pages, "总耗时:", filtered.ms + "ms"); console.log("\n=== 完成 ==="); } main().catch((e) => { console.error(e); process.exit(1); });