WebSocketBenchmarkComponentSystem.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using ETModel;
  3. namespace ETHotfix
  4. {
  5. [ObjectSystem]
  6. public class WebSocketBenchmarkComponentSystem : AwakeSystem<WebSocketBenchmarkComponent, string>
  7. {
  8. public override void Awake(WebSocketBenchmarkComponent self, string address)
  9. {
  10. self.Awake(address);
  11. }
  12. }
  13. public static class WebSocketBenchmarkComponentHelper
  14. {
  15. public static void Awake(this WebSocketBenchmarkComponent self, string address)
  16. {
  17. try
  18. {
  19. NetOuterComponent networkComponent = Game.Scene.GetComponent<NetOuterComponent>();
  20. for (int i = 0; i < 1000; i++)
  21. {
  22. self.TestAsync(networkComponent, i, address).Coroutine();
  23. }
  24. }
  25. catch (Exception e)
  26. {
  27. Log.Error(e);
  28. }
  29. }
  30. public static async ETVoid TestAsync(this WebSocketBenchmarkComponent self, NetOuterComponent networkComponent, int j, string address)
  31. {
  32. try
  33. {
  34. using (Session session = networkComponent.Create(address))
  35. {
  36. int i = 0;
  37. while (i < 100000000)
  38. {
  39. ++i;
  40. await self.Send(session, j);
  41. }
  42. }
  43. }
  44. catch (RpcException e)
  45. {
  46. Log.Error(e);
  47. }
  48. catch (Exception e)
  49. {
  50. Log.Error(e);
  51. }
  52. }
  53. public static async ETTask Send(this WebSocketBenchmarkComponent self, Session session, int j)
  54. {
  55. try
  56. {
  57. await session.Call(new C2R_Ping());
  58. ++self.k;
  59. if (self.k % 10000 != 0)
  60. {
  61. return;
  62. }
  63. long time2 = TimeHelper.ClientNow();
  64. long time = time2 - self.time1;
  65. self.time1 = time2;
  66. Log.Info($"Benchmark k: {self.k} 每1W次耗时: {time} ms {session.Network.Count}");
  67. }
  68. catch (Exception e)
  69. {
  70. Log.Error(e);
  71. }
  72. }
  73. }
  74. }