test.mjs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { io } from "socket.io-client";
  2. import customParser from "socket.io-msgpack-parser";
  3. const url = process.argv[2];
  4. const count = process.argv[3];
  5. const userStartId = process.argv[4];
  6. const testSceneNum = process.argv[5] || "t-test";
  7. const roomId = process.argv[6] || "00001";
  8. console.log("argv", userStartId, testSceneNum, roomId);
  9. console.log("socket-info::url:config-->", url);
  10. console.log("socket-info::url:count-->", count);
  11. console.log("socket-info::start-->压力测试正在开始");
  12. const URL = url || process.env.URL;
  13. // http://zhang9394@zhangyupeng:face3d.4dage.com:7005/zhangyupeng/chatIM.git
  14. const MAX_CLIENTS = Number(count) || 500;
  15. const POLLING_PERCENTAGE = 0.05;
  16. const CLIENT_CREATION_INTERVAL_IN_MS = 10;
  17. const EMIT_INTERVAL_IN_MS = 1000;
  18. // wws://test-socket.4dkankan.com/watch
  19. let clientCount = 0;
  20. let lastReport = new Date().getTime();
  21. let packetsSinceLastReport = 0;
  22. // let testSceneNum = "t-test";
  23. // let roomId = "00001";
  24. let userLimitNum = MAX_CLIENTS;
  25. let agentId = 0;
  26. const createAgent = () => {
  27. agentId += 1;
  28. const nickName = `test_name_${userStartId}${agentId}`;
  29. const userId = `${userStartId}${agentId}`;
  30. const role = agentId === 1 ? "leader" : "customer";
  31. createClient({ userId, nickName, from: "0", role: role });
  32. createClient({ userId, nickName, from: "1", role: role });
  33. createClient({ userId, nickName, from: "2", role: role });
  34. };
  35. const createClient = ({ userId, nickName, from, role }) => {
  36. // for demonstration purposes, some clients stay stuck in HTTP long-polling
  37. const socket = io(URL, {
  38. path: "/fsl-node",
  39. transport: ["websocket"],
  40. parser: customParser,
  41. query: {
  42. userId: userId,
  43. from: from || 2,
  44. sceneNum: testSceneNum,
  45. role: role,
  46. nickName: nickName,
  47. roomId: roomId,
  48. voiceStatus: 0,
  49. enableTalk: true,
  50. isAuthMic: 0,
  51. isAllowMic: 0,
  52. userLimitNum,
  53. myHeadUrl:
  54. "http://downza.img.zz314.com/edu/pc/wlgj-1008/2016-06-23/64ec0888b15773e3ba5b5f744b9df16c.jpg",
  55. },
  56. });
  57. setInterval(() => {
  58. socket.emit("client to server event");
  59. }, EMIT_INTERVAL_IN_MS);
  60. socket.on("server to client event", () => {
  61. packetsSinceLastReport++;
  62. });
  63. socket.on("disconnect", (reason) => {
  64. console.log(`disconnect due to ${reason}`);
  65. });
  66. if (++clientCount < MAX_CLIENTS) {
  67. setTimeout(createAgent, CLIENT_CREATION_INTERVAL_IN_MS);
  68. }
  69. };
  70. createAgent();
  71. const printReport = () => {
  72. const now = new Date().getTime();
  73. const durationSinceLastReport = (now - lastReport) / 1000;
  74. const packetsPerSeconds = (
  75. packetsSinceLastReport / durationSinceLastReport
  76. ).toFixed(2);
  77. console.log(
  78. `client count: ${clientCount} ; average packets received per second: ${packetsPerSeconds}`
  79. );
  80. packetsSinceLastReport = 0;
  81. lastReport = now;
  82. };
  83. setInterval(printReport, 5000);