WebSocketBenchmarkComponentSystem.cs 1.6 KB

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