chenyc
2026-03-22 7885cede659f3255be56f77c1eef2ada7387d6f1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
const net = require("net");
const config = require("../src/config");
 
const DEVICE_COUNT = Number(process.env.DEVICE_COUNT || 50);
const DURATION_SEC = Number(process.env.DURATION_SEC || 120);
const INTERVAL_MS = Number(process.env.INTERVAL_MS || 1000);
const ALARM_EVERY = Number(process.env.ALARM_EVERY || 30);
const BP_EVERY = Number(process.env.BP_EVERY || 15);
 
const HOST = process.env.TCP_HOST || config.tcp.host || "127.0.0.1";
const PORT = Number(process.env.TCP_PORT || config.tcp.port || 19000);
 
function writeUIntLE(buf, offset, value, bytes) {
  if (bytes === 1) buf.writeUInt8(value, offset);
  if (bytes === 2) buf.writeUInt16LE(value, offset);
  if (bytes === 4) buf.writeUInt32LE(value, offset);
}
 
function writeInt16LE(buf, offset, value) {
  buf.writeInt16LE(value, offset);
}
 
function buildHeader(frameType, machineId) {
  const header = Buffer.alloc(20);
  header[0] = 0x55;
  header[1] = 0x55;
  header[2] = 0x55;
  header[3] = 0x55;
 
  header[4] = 0x31;
 
  let id = Number(machineId) || 0;
  for (let i = 0; i < 5; i++) {
    header[5 + i] = id & 0xff;
    id = Math.floor(id / 256);
  }
 
  header[10] = 0x01;
  header[11] = frameType;
  header[12] = 0x6e;
  return header;
}
 
function buildRunParamsFrame(machineId, elapsedSec) {
  const data = Buffer.alloc(200);
 
  const setTimeSec = 4 * 3600;
  const doneSec = Math.max(0, elapsedSec);
 
  writeUIntLE(data, 0, setTimeSec, 4);
  writeUIntLE(data, 4, doneSec, 4);
  writeUIntLE(data, 8, 280, 2);
  writeUIntLE(data, 10, 1, 1);
  writeUIntLE(data, 11, 1, 1);
  writeUIntLE(data, 12, 1, 1);
  writeUIntLE(data, 13, 500, 2);
  writeUIntLE(data, 15, 20, 2);
 
  const ufTotalMl = 1800;
  const ufDoneMl = Math.min(ufTotalMl, Math.floor(doneSec / 10));
  writeUIntLE(data, 17, ufTotalMl, 4);
  writeUIntLE(data, 21, ufDoneMl, 4);
  writeUIntLE(data, 25, 600, 4);
 
  writeUIntLE(data, 29, 1, 1);
  writeUIntLE(data, 30, 0, 1);
  writeUIntLE(data, 31, 500, 2);
  writeUIntLE(data, 33, 368, 2);
  writeUIntLE(data, 35, 1450, 2);
  writeUIntLE(data, 37, 1500, 4);
  writeUIntLE(data, 41, Math.min(1500, Math.floor(doneSec / 12)), 4);
  writeUIntLE(data, 45, 0, 1);
  writeUIntLE(data, 46, 120, 2);
  writeUIntLE(data, 48, 100, 2);
  writeUIntLE(data, 50, Math.floor(doneSec / 60), 4);
 
  writeInt16LE(data, 54, -120);
  writeInt16LE(data, 56, 130);
  writeInt16LE(data, 58, 90);
  writeInt16LE(data, 60, -8);
 
  writeUIntLE(data, 62, 15, 1);
  writeUIntLE(data, 64, 3500, 2);
  writeUIntLE(data, 66, 365, 2);
  writeUIntLE(data, 68, 366, 2);
  writeUIntLE(data, 69, 82, 1);
 
  return Buffer.concat([buildHeader(0x1f, machineId), data]);
}
 
function buildAlarmFrame(machineId) {
  const data = Buffer.alloc(130);
  writeUIntLE(data, 0, 283, 2);
  writeUIntLE(data, 2, 1, 1);
 
  const now = new Date();
  writeUIntLE(data, 3, now.getFullYear(), 2);
  writeUIntLE(data, 5, now.getMonth() + 1, 1);
  writeUIntLE(data, 6, now.getDate(), 1);
  writeUIntLE(data, 7, now.getHours(), 1);
  writeUIntLE(data, 8, now.getMinutes(), 1);
  writeUIntLE(data, 9, now.getSeconds(), 1);
 
  return Buffer.concat([buildHeader(0x26, machineId), data]);
}
 
function buildBpFrame(machineId) {
  const data = Buffer.alloc(130);
 
  writeUIntLE(data, 0, 1, 1);
  writeUIntLE(data, 1, 1, 1);
 
  const now = new Date();
  writeUIntLE(data, 2, now.getFullYear(), 2);
  writeUIntLE(data, 4, now.getMonth() + 1, 1);
  writeUIntLE(data, 5, now.getDate(), 1);
  writeUIntLE(data, 6, now.getHours(), 1);
  writeUIntLE(data, 7, now.getMinutes(), 1);
  writeUIntLE(data, 8, now.getSeconds(), 1);
 
  writeUIntLE(data, 9, 140, 2);
  writeUIntLE(data, 11, 90, 2);
  writeUIntLE(data, 13, 80, 2);
  writeUIntLE(data, 15, 105, 2);
 
  return Buffer.concat([buildHeader(0x29, machineId), data]);
}
 
const sockets = [];
let sentFrames = 0;
let sentBytes = 0;
let connectOk = 0;
let connectErr = 0;
let runtimeSocketErr = 0;
let shutdownIgnoredErr = 0;
let stopping = false;
 
const startTs = Date.now();
 
for (let i = 0; i < DEVICE_COUNT; i++) {
  const machineId = 6220903000 + i;
  const socket = new net.Socket();
  sockets.push(socket);
 
  let tick = 0;
  let timer = null;
 
  socket.connect(PORT, HOST, () => {
    connectOk += 1;
    timer = setInterval(() => {
      tick += 1;
      const elapsedSec = Math.floor((Date.now() - startTs) / 1000);
 
      const runFrame = buildRunParamsFrame(machineId, elapsedSec);
      socket.write(runFrame);
      sentFrames += 1;
      sentBytes += runFrame.length;
 
      if (ALARM_EVERY > 0 && tick % ALARM_EVERY === 0) {
        const alarm = buildAlarmFrame(machineId);
        socket.write(alarm);
        sentFrames += 1;
        sentBytes += alarm.length;
      }
 
      if (BP_EVERY > 0 && tick % BP_EVERY === 0) {
        const bp = buildBpFrame(machineId);
        socket.write(bp);
        sentFrames += 1;
        sentBytes += bp.length;
      }
    }, INTERVAL_MS);
  });
 
  socket.on("error", (err) => {
    const msg = (err && (err.message || String(err))) || "";
    const code = err && err.code;
    const isExpectedDuringStop =
      stopping && (code === "ECONNRESET" || code === "EPIPE" || code === "ECONNABORTED");
 
    if (isExpectedDuringStop) {
      shutdownIgnoredErr += 1;
      return;
    }
 
    runtimeSocketErr += 1;
    connectErr += 1;
    console.error(`[device ${machineId}] socket error:`, code || msg);
  });
 
  socket.on("close", () => {
    if (timer) clearInterval(timer);
  });
}
 
setTimeout(() => {
  stopping = true;
 
  for (const socket of sockets) {
    try {
      socket.end();
      socket.destroy();
    } catch (_) {}
  }
 
  const duration = Math.max(1, Math.floor((Date.now() - startTs) / 1000));
  const fps = (sentFrames / duration).toFixed(2);
  const kbps = ((sentBytes / 1024) / duration).toFixed(2);
 
  console.log("\n=== Stress Test Summary ===");
  console.log("host:", HOST, "port:", PORT);
  console.log("devices:", DEVICE_COUNT, "duration(s):", duration);
  console.log("connected:", connectOk, "errors:", connectErr);
  console.log("runtimeSocketErrors:", runtimeSocketErr);
  console.log("ignoredDuringShutdown:", shutdownIgnoredErr);
  console.log("sentFrames:", sentFrames, "fps:", fps);
  console.log("throughput(KB/s):", kbps);
  process.exit(0);
}, DURATION_SEC * 1000);
 
console.log(
  `Starting stress test: devices=${DEVICE_COUNT}, duration=${DURATION_SEC}s, interval=${INTERVAL_MS}ms, target=${HOST}:${PORT}`
);