bench-dms-payment-query.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * 测试 DMS 发放明细条件查询 vs 全表翻页
  3. * node scripts/bench-dms-payment-query.js
  4. */
  5. const http = require("http");
  6. const qs = require("querystring");
  7. const HOST = "121.43.55.7";
  8. const PORT = 2101;
  9. const COLUMN_ID = 1849;
  10. const FFMX_TABLE = "column_sjnmtybt_ffmx_payment_detail";
  11. function req(urlPath, fields, token) {
  12. return new Promise((resolve, reject) => {
  13. const body = qs.stringify(fields || {});
  14. const headers = {
  15. "Content-Type": "application/x-www-form-urlencoded",
  16. "Content-Length": Buffer.byteLength(body),
  17. };
  18. if (token) {
  19. headers.Token = token;
  20. headers.token = token;
  21. }
  22. const t0 = Date.now();
  23. const r = http.request(
  24. { hostname: HOST, port: PORT, path: urlPath, method: "POST", headers },
  25. (res) => {
  26. let data = "";
  27. res.on("data", (c) => (data += c));
  28. res.on("end", () => {
  29. const ms = Date.now() - t0;
  30. try {
  31. resolve({ ms, data: JSON.parse(data) });
  32. } catch (e) {
  33. resolve({ ms, data: { raw: data.slice(0, 500), status: res.statusCode } });
  34. }
  35. });
  36. }
  37. );
  38. r.on("error", reject);
  39. r.write(body);
  40. r.end();
  41. });
  42. }
  43. async function login() {
  44. const res = await req("/proxy_oauth/user/login", {
  45. userName: "user_liu",
  46. password: "WE176852439@lmx",
  47. clientId: "1",
  48. });
  49. if (res.data.code != 200) throw new Error("login fail: " + JSON.stringify(res.data));
  50. return res.data.message;
  51. }
  52. async function selectList(token, extra) {
  53. const fields = Object.assign(
  54. {
  55. columnId: String(COLUMN_ID),
  56. page: "0",
  57. pageSize: "100",
  58. states: "0",
  59. },
  60. extra || {}
  61. );
  62. return req("/proxy_dms/content/selectContentList", fields, token);
  63. }
  64. async function groupBy(token, field, paramJson) {
  65. const fields = { columnId: String(COLUMN_ID), field };
  66. if (paramJson) fields.paramJson = JSON.stringify(paramJson);
  67. return req("/proxy_dms/content/selectGroupByCountOrderBy", fields, token);
  68. }
  69. function listSize(data) {
  70. if (!data) return 0;
  71. if (Array.isArray(data.content)) return data.content.length;
  72. const inner = data.content && data.content.data;
  73. if (Array.isArray(inner)) return inner.length;
  74. return 0;
  75. }
  76. function listCount(data) {
  77. if (!data || !data.content) return 0;
  78. if (typeof data.content.count === "number") return data.content.count;
  79. return listSize(data);
  80. }
  81. function firstRow(data) {
  82. const inner = data && data.content && data.content.data;
  83. if (Array.isArray(inner) && inner.length) return inner[0];
  84. if (Array.isArray(data.content) && data.content.length) return data.content[0];
  85. return {};
  86. }
  87. async function countAllPages(token, extra, maxPages) {
  88. let total = 0;
  89. let page = 0;
  90. const t0 = Date.now();
  91. while (page < (maxPages || 500)) {
  92. const fields = Object.assign(
  93. { columnId: String(COLUMN_ID), page: String(page), pageSize: "100", states: "0" },
  94. extra || {}
  95. );
  96. const { data, ms } = await req("/proxy_dms/content/selectContentList", fields, token);
  97. if (data.code === 202) {
  98. return { total, ms: Date.now() - t0, pages: page, lastPageMs: ms };
  99. }
  100. if (data.code !== 200 && data.code !== 0) {
  101. throw new Error("select fail page " + page + ": " + JSON.stringify(data).slice(0, 300));
  102. }
  103. const n = listSize(data);
  104. total += n;
  105. if (n < 100) {
  106. return { total, ms: Date.now() - t0, pages: page + 1, lastPageMs: ms };
  107. }
  108. page++;
  109. }
  110. return { total, ms: Date.now() - t0, pages: maxPages, truncated: true };
  111. }
  112. async function main() {
  113. console.log("=== DMS 发放明细查询压测 ===");
  114. console.log("表名:", FFMX_TABLE);
  115. console.log("columnId:", COLUMN_ID);
  116. console.log("网关:", `http://${HOST}:${PORT}/proxy_dms`);
  117. console.log("");
  118. const loginRes = await req("/proxy_oauth/user/login", {
  119. userName: "user_liu",
  120. password: "WE176852439@lmx",
  121. clientId: "1",
  122. });
  123. console.log("登录:", loginRes.ms + "ms", "code=", loginRes.data.code);
  124. if (loginRes.data.code != 200) {
  125. console.error(JSON.stringify(loginRes.data));
  126. process.exit(1);
  127. }
  128. const token = loginRes.data.message;
  129. // 取样:取 1 条拿 pay_month / batch_id / user_id
  130. const sample = await selectList(token, { pageSize: "1", states: "0,3" });
  131. console.log("\n[1] 取样(无 search, states=0,3, pageSize=1):", sample.ms + "ms", "rows=", listSize(sample.data), "count=", listCount(sample.data));
  132. const row = firstRow(sample.data);
  133. const payMonth = row.c_pay_month || "202608";
  134. const batchId = row.c_batch_id || "";
  135. const userId = row.c_user_id || "";
  136. console.log(" 样本 c_pay_month=", payMonth, "c_batch_id=", batchId, "c_user_id=", userId);
  137. // 无 search 单页
  138. const page0 = await selectList(token, { states: "0,3" });
  139. console.log("\n[2] 无 search 单页(100条, states=0,3):", page0.ms + "ms", "rows=", listSize(page0.data), "count=", listCount(page0.data));
  140. // 无 states 限制(仅 state!=4)
  141. const pageNoState = await req("/proxy_dms/content/selectContentList", {
  142. columnId: String(COLUMN_ID), page: "0", pageSize: "100",
  143. }, token);
  144. console.log("\n[2b] 无 states 参数 单页:", pageNoState.ms + "ms", "rows=", listSize(pageNoState.data), "count=", listCount(pageNoState.data));
  145. // search 等值 pay_month
  146. const searchPayMonth = JSON.stringify([
  147. { field: "c_pay_month", searchType: 1, content: { value: payMonth } },
  148. ]);
  149. const byMonth = await selectList(token, { search: searchPayMonth, pageSize: "100", states: "0,3" });
  150. console.log("\n[3] search c_pay_month=", payMonth, ":", byMonth.ms + "ms", "rows=", listSize(byMonth.data), "count=", listCount(byMonth.data));
  151. // search batch_id
  152. if (batchId) {
  153. const searchBatch = JSON.stringify([
  154. { field: "c_batch_id", searchType: 1, content: { value: batchId } },
  155. ]);
  156. const byBatch = await selectList(token, { search: searchBatch, pageSize: "100", states: "0,3" });
  157. console.log("\n[4] search c_batch_id=", batchId, ":", byBatch.ms + "ms", "rows=", listSize(byBatch.data), "count=", listCount(byBatch.data));
  158. }
  159. // search user_id
  160. if (userId) {
  161. const searchUser = JSON.stringify([
  162. { field: "c_user_id", searchType: 1, content: { value: userId } },
  163. ]);
  164. const byUser = await selectList(token, { search: searchUser, pageSize: "100", states: "0,3" });
  165. console.log("\n[5] search c_user_id=", userId, ":", byUser.ms + "ms", "rows=", listSize(byUser.data), "count=", listCount(byUser.data));
  166. }
  167. // town_id if present
  168. const townId = row.c_town_id;
  169. if (townId) {
  170. const searchTown = JSON.stringify([
  171. { field: "c_pay_month", searchType: 1, content: { value: payMonth } },
  172. { field: "c_town_id", searchType: 1, content: { value: townId } },
  173. ]);
  174. const byTown = await selectList(token, { search: searchTown, pageSize: "100", states: "0,3" });
  175. console.log("\n[6] search pay_month+town:", byTown.ms + "ms", "rows=", listSize(byTown.data), "count=", listCount(byTown.data));
  176. }
  177. // 聚合:按发放月分组计数
  178. const groupRes = await groupBy(token, "c_pay_month");
  179. console.log("\n[7] selectGroupByCountOrderBy(c_pay_month):", groupRes.ms + "ms");
  180. const groups = groupRes.data.content || groupRes.data;
  181. console.log(" 结果:", JSON.stringify(groups).slice(0, 400));
  182. const groupFiltered = await groupBy(token, "c_town_id", { c_pay_month: payMonth });
  183. console.log("\n[7b] groupBy town (filter pay_month):", groupFiltered.ms + "ms", "groups=", Array.isArray(groupFiltered.data.content) ? groupFiltered.data.content.length : "?");
  184. // 全表翻页计数
  185. console.log("\n[8] 全表翻页统计(states=0,3, 无 search)...");
  186. const full = await countAllPages(token, { states: "0,3" }, 500);
  187. console.log(" 总条数:", full.total, "页数:", full.pages, "总耗时:", full.ms + "ms");
  188. console.log("\n[9] 按 c_pay_month 条件翻页统计(states=0,3)...");
  189. const filtered = await countAllPages(token, { search: searchPayMonth, states: "0,3" }, 50);
  190. console.log(" 总条数:", filtered.total, "页数:", filtered.pages, "总耗时:", filtered.ms + "ms");
  191. console.log("\n=== 完成 ===");
  192. }
  193. main().catch((e) => {
  194. console.error(e);
  195. process.exit(1);
  196. });