BenchmarkComponentSystem.cs 1.6 KB

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