bench-dms-payment-query-v2.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * 加索引后对比:单页多次均值 + 镇+月全量翻页
  3. */
  4. const http = require("http");
  5. const qs = require("querystring");
  6. const HOST = "121.43.55.7";
  7. const PORT = 2101;
  8. const COLUMN_ID = "1849";
  9. function req(urlPath, fields, token) {
  10. return new Promise((resolve, reject) => {
  11. const body = qs.stringify(fields || {});
  12. const t0 = Date.now();
  13. const r = http.request(
  14. {
  15. hostname: HOST,
  16. port: PORT,
  17. path: urlPath,
  18. method: "POST",
  19. headers: {
  20. "Content-Type": "application/x-www-form-urlencoded",
  21. "Content-Length": Buffer.byteLength(body),
  22. ...(token ? { Token: token, token: token } : {}),
  23. },
  24. },
  25. (res) => {
  26. let data = "";
  27. res.on("data", (c) => (data += c));
  28. res.on("end", () => {
  29. try {
  30. resolve({ ms: Date.now() - t0, data: JSON.parse(data) });
  31. } catch (e) {
  32. resolve({ ms: Date.now() - t0, data: { parseError: true, raw: data.slice(0, 200) } });
  33. }
  34. });
  35. }
  36. );
  37. r.on("error", reject);
  38. r.write(body);
  39. r.end();
  40. });
  41. }
  42. function rowsOf(data) {
  43. return data && data.content && data.content.data ? data.content.data.length : 0;
  44. }
  45. function countOf(data) {
  46. return data && data.content && data.content.count != null ? data.content.count : -1;
  47. }
  48. async function bench(label, fields, token, rounds = 5) {
  49. const times = [];
  50. let count = -1;
  51. for (let i = 0; i < rounds; i++) {
  52. const r = await req("/proxy_dms/content/selectContentList", fields, token);
  53. times.push(r.ms);
  54. count = countOf(r.data);
  55. }
  56. times.sort((a, b) => a - b);
  57. console.log(
  58. `${label}: min=${times[0]}ms p50=${times[Math.floor(times.length / 2)]}ms max=${times[times.length - 1]}ms count=${count}`
  59. );
  60. }
  61. async function pageAll(label, fields, token) {
  62. let total = 0;
  63. let p = 0;
  64. const t0 = Date.now();
  65. while (p < 500) {
  66. const f = Object.assign({}, fields, { page: String(p), pageSize: "100" });
  67. const r = await req("/proxy_dms/content/selectContentList", f, token);
  68. if (r.data.code === 202) break;
  69. if (r.data.code !== 200) {
  70. console.log(label, "FAIL", JSON.stringify(r.data).slice(0, 200));
  71. break;
  72. }
  73. const n = rowsOf(r.data);
  74. total += n;
  75. if (n < 100) break;
  76. p++;
  77. }
  78. console.log(`${label}: rows=${total} pages=${p + 1} totalMs=${Date.now() - t0}`);
  79. }
  80. async function main() {
  81. console.log("=== 加索引后细测 ===\n");
  82. const login = await req("/proxy_oauth/user/login", {
  83. userName: "user_liu",
  84. password: "WE176852439@lmx",
  85. clientId: "1",
  86. });
  87. if (login.data.code != 200) {
  88. console.error("login fail", login.data);
  89. process.exit(1);
  90. }
  91. const token = login.data.message;
  92. const sample = await req(
  93. "/proxy_dms/content/selectContentList",
  94. { columnId: COLUMN_ID, page: "0", pageSize: "1", states: "0,3" },
  95. token
  96. );
  97. const row = (sample.data.content && sample.data.content.data && sample.data.content.data[0]) || {};
  98. const payMonth = row.c_pay_month || "202608";
  99. const townId = row.c_town_id || "";
  100. const userId = row.c_user_id || "P3233";
  101. const batchId = row.c_batch_id || "DB202608";
  102. const mk = (arr) => JSON.stringify(arr);
  103. console.log(`样本: payMonth=${payMonth} town=${townId} user=${userId}\n`);
  104. console.log("--- 加索引前参考(上次) ---");
  105. console.log("按人单页 ~113ms | 镇+月单页 ~159ms | 全表翻页 ~52328ms | 无search单页 ~237ms\n");
  106. console.log("--- 单页 5 次均值 ---");
  107. await bench(
  108. "按人 c_user_id",
  109. {
  110. columnId: COLUMN_ID,
  111. page: "0",
  112. pageSize: "100",
  113. states: "0,3",
  114. search: mk([{ field: "c_user_id", searchType: 1, content: { value: userId } }]),
  115. },
  116. token
  117. );
  118. if (townId) {
  119. await bench(
  120. "镇+月",
  121. {
  122. columnId: COLUMN_ID,
  123. page: "0",
  124. pageSize: "100",
  125. states: "0,3",
  126. search: mk([
  127. { field: "c_pay_month", searchType: 1, content: { value: payMonth } },
  128. { field: "c_town_id", searchType: 1, content: { value: townId } },
  129. ]),
  130. },
  131. token
  132. );
  133. }
  134. await bench(
  135. "仅 pay_month",
  136. {
  137. columnId: COLUMN_ID,
  138. page: "0",
  139. pageSize: "100",
  140. states: "0,3",
  141. search: mk([{ field: "c_pay_month", searchType: 1, content: { value: payMonth } }]),
  142. },
  143. token
  144. );
  145. await bench(
  146. "无 search",
  147. { columnId: COLUMN_ID, page: "0", pageSize: "100", states: "0,3" },
  148. token
  149. );
  150. await bench(
  151. "按 batch_id",
  152. {
  153. columnId: COLUMN_ID,
  154. page: "0",
  155. pageSize: "100",
  156. states: "0,3",
  157. search: mk([{ field: "c_batch_id", searchType: 1, content: { value: batchId } }]),
  158. },
  159. token
  160. );
  161. console.log("\n--- 全量翻页 ---");
  162. if (townId) {
  163. await pageAll("镇+月 全翻", {
  164. columnId: COLUMN_ID,
  165. states: "0,3",
  166. search: mk([
  167. { field: "c_pay_month", searchType: 1, content: { value: payMonth } },
  168. { field: "c_town_id", searchType: 1, content: { value: townId } },
  169. ]),
  170. }, token);
  171. }
  172. await pageAll("按人 全翻", {
  173. columnId: COLUMN_ID,
  174. states: "0,3",
  175. search: mk([{ field: "c_user_id", searchType: 1, content: { value: userId } }]),
  176. }, token);
  177. await pageAll("全表 全翻", { columnId: COLUMN_ID, states: "0,3" }, token);
  178. }
  179. main().catch((e) => {
  180. console.error(e);
  181. process.exit(1);
  182. });