BenchmarkComponentSystem.cs 1.6 KB

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