BenchmarkComponentSystem.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 < 2000; 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 (Exception e)
  48. {
  49. Log.Error(e);
  50. }
  51. }
  52. public static async Task Send(this BenchmarkComponent self, Session session, int j)
  53. {
  54. try
  55. {
  56. await session.Call(new C2R_Ping());
  57. ++self.k;
  58. if (self.k % 100000 != 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} 每10W次耗时: {time} ms {session.Network.Count}");
  66. }
  67. catch (Exception e)
  68. {
  69. Log.Error(e);
  70. }
  71. }
  72. }
  73. }