WebSocketBenchmarkComponentSystem.cs 1.6 KB

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