WebSocketBenchmarkComponentSystem.cs 1.6 KB

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