| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- /**
- * 加索引后对比:单页多次均值 + 镇+月全量翻页
- */
- const http = require("http");
- const qs = require("querystring");
- const HOST = "121.43.55.7";
- const PORT = 2101;
- const COLUMN_ID = "1849";
- function req(urlPath, fields, token) {
- return new Promise((resolve, reject) => {
- const body = qs.stringify(fields || {});
- const t0 = Date.now();
- const r = http.request(
- {
- hostname: HOST,
- port: PORT,
- path: urlPath,
- method: "POST",
- headers: {
- "Content-Type": "application/x-www-form-urlencoded",
- "Content-Length": Buffer.byteLength(body),
- ...(token ? { Token: token, token: token } : {}),
- },
- },
- (res) => {
- let data = "";
- res.on("data", (c) => (data += c));
- res.on("end", () => {
- try {
- resolve({ ms: Date.now() - t0, data: JSON.parse(data) });
- } catch (e) {
- resolve({ ms: Date.now() - t0, data: { parseError: true, raw: data.slice(0, 200) } });
- }
- });
- }
- );
- r.on("error", reject);
- r.write(body);
- r.end();
- });
- }
- function rowsOf(data) {
- return data && data.content && data.content.data ? data.content.data.length : 0;
- }
- function countOf(data) {
- return data && data.content && data.content.count != null ? data.content.count : -1;
- }
- async function bench(label, fields, token, rounds = 5) {
- const times = [];
- let count = -1;
- for (let i = 0; i < rounds; i++) {
- const r = await req("/proxy_dms/content/selectContentList", fields, token);
- times.push(r.ms);
- count = countOf(r.data);
- }
- times.sort((a, b) => a - b);
- console.log(
- `${label}: min=${times[0]}ms p50=${times[Math.floor(times.length / 2)]}ms max=${times[times.length - 1]}ms count=${count}`
- );
- }
- async function pageAll(label, fields, token) {
- let total = 0;
- let p = 0;
- const t0 = Date.now();
- while (p < 500) {
- const f = Object.assign({}, fields, { page: String(p), pageSize: "100" });
- const r = await req("/proxy_dms/content/selectContentList", f, token);
- if (r.data.code === 202) break;
- if (r.data.code !== 200) {
- console.log(label, "FAIL", JSON.stringify(r.data).slice(0, 200));
- break;
- }
- const n = rowsOf(r.data);
- total += n;
- if (n < 100) break;
- p++;
- }
- console.log(`${label}: rows=${total} pages=${p + 1} totalMs=${Date.now() - t0}`);
- }
- async function main() {
- console.log("=== 加索引后细测 ===\n");
- const login = await req("/proxy_oauth/user/login", {
- userName: "user_liu",
- password: "WE176852439@lmx",
- clientId: "1",
- });
- if (login.data.code != 200) {
- console.error("login fail", login.data);
- process.exit(1);
- }
- const token = login.data.message;
- const sample = await req(
- "/proxy_dms/content/selectContentList",
- { columnId: COLUMN_ID, page: "0", pageSize: "1", states: "0,3" },
- token
- );
- const row = (sample.data.content && sample.data.content.data && sample.data.content.data[0]) || {};
- const payMonth = row.c_pay_month || "202608";
- const townId = row.c_town_id || "";
- const userId = row.c_user_id || "P3233";
- const batchId = row.c_batch_id || "DB202608";
- const mk = (arr) => JSON.stringify(arr);
- console.log(`样本: payMonth=${payMonth} town=${townId} user=${userId}\n`);
- console.log("--- 加索引前参考(上次) ---");
- console.log("按人单页 ~113ms | 镇+月单页 ~159ms | 全表翻页 ~52328ms | 无search单页 ~237ms\n");
- console.log("--- 单页 5 次均值 ---");
- await bench(
- "按人 c_user_id",
- {
- columnId: COLUMN_ID,
- page: "0",
- pageSize: "100",
- states: "0,3",
- search: mk([{ field: "c_user_id", searchType: 1, content: { value: userId } }]),
- },
- token
- );
- if (townId) {
- await bench(
- "镇+月",
- {
- columnId: COLUMN_ID,
- page: "0",
- pageSize: "100",
- states: "0,3",
- search: mk([
- { field: "c_pay_month", searchType: 1, content: { value: payMonth } },
- { field: "c_town_id", searchType: 1, content: { value: townId } },
- ]),
- },
- token
- );
- }
- await bench(
- "仅 pay_month",
- {
- columnId: COLUMN_ID,
- page: "0",
- pageSize: "100",
- states: "0,3",
- search: mk([{ field: "c_pay_month", searchType: 1, content: { value: payMonth } }]),
- },
- token
- );
- await bench(
- "无 search",
- { columnId: COLUMN_ID, page: "0", pageSize: "100", states: "0,3" },
- token
- );
- await bench(
- "按 batch_id",
- {
- columnId: COLUMN_ID,
- page: "0",
- pageSize: "100",
- states: "0,3",
- search: mk([{ field: "c_batch_id", searchType: 1, content: { value: batchId } }]),
- },
- token
- );
- console.log("\n--- 全量翻页 ---");
- if (townId) {
- await pageAll("镇+月 全翻", {
- columnId: COLUMN_ID,
- states: "0,3",
- search: mk([
- { field: "c_pay_month", searchType: 1, content: { value: payMonth } },
- { field: "c_town_id", searchType: 1, content: { value: townId } },
- ]),
- }, token);
- }
- await pageAll("按人 全翻", {
- columnId: COLUMN_ID,
- states: "0,3",
- search: mk([{ field: "c_user_id", searchType: 1, content: { value: userId } }]),
- }, token);
- await pageAll("全表 全翻", { columnId: COLUMN_ID, states: "0,3" }, token);
- }
- main().catch((e) => {
- console.error(e);
- process.exit(1);
- });
|