import { io } from "socket.io-client"; import customParser from "socket.io-msgpack-parser"; const url = process.argv[2]; const count = process.argv[3]; const userStartId = process.argv[4]; const testSceneNum = process.argv[5] || "t-test"; const roomId = process.argv[6] || "00001"; console.log("argv", userStartId, testSceneNum, roomId); console.log("socket-info::url:config-->", url); console.log("socket-info::url:count-->", count); console.log("socket-info::start-->压力测试正在开始"); const URL = url || process.env.URL; // http://zhang9394@zhangyupeng:face3d.4dage.com:7005/zhangyupeng/chatIM.git const MAX_CLIENTS = Number(count) || 500; const POLLING_PERCENTAGE = 0.05; const CLIENT_CREATION_INTERVAL_IN_MS = 10; const EMIT_INTERVAL_IN_MS = 1000; // wws://test-socket.4dkankan.com/watch let clientCount = 0; let lastReport = new Date().getTime(); let packetsSinceLastReport = 0; // let testSceneNum = "t-test"; // let roomId = "00001"; let userLimitNum = MAX_CLIENTS; let agentId = 0; const createAgent = () => { agentId += 1; const nickName = `test_name_${agentId}`; const userId = `${userStartId}${agentId}`; const role = agentId === 1 ? "leader" : "customer"; createClient({ userId, nickName, from: "0", role: role }); createClient({ userId, nickName, from: "1", role: role }); createClient({ userId, nickName, from: "2", role: role }); }; const createClient = ({ userId, nickName, from, role }) => { // for demonstration purposes, some clients stay stuck in HTTP long-polling const socket = io(URL, { path: "/fsl-node", transport: ["websocket"], parser: customParser, query: { userId: userId, from: from || 2, sceneNum: testSceneNum, role: role, nickName: nickName, roomId: roomId, voiceStatus: 0, enableTalk: true, isAuthMic: 0, isAllowMic: 0, userLimitNum, myHeadUrl: "http://downza.img.zz314.com/edu/pc/wlgj-1008/2016-06-23/64ec0888b15773e3ba5b5f744b9df16c.jpg", }, }); setInterval(() => { socket.emit("client to server event"); }, EMIT_INTERVAL_IN_MS); socket.on("server to client event", () => { packetsSinceLastReport++; }); socket.on("disconnect", (reason) => { console.log(`disconnect due to ${reason}`); }); if (++clientCount < MAX_CLIENTS) { setTimeout(createAgent, CLIENT_CREATION_INTERVAL_IN_MS); } }; createAgent(); const printReport = () => { const now = new Date().getTime(); const durationSinceLastReport = (now - lastReport) / 1000; const packetsPerSeconds = ( packetsSinceLastReport / durationSinceLastReport ).toFixed(2); console.log( `client count: ${clientCount} ; average packets received per second: ${packetsPerSeconds}` ); packetsSinceLastReport = 0; lastReport = now; }; setInterval(printReport, 5000);