test.mjs 2.6 KB

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