BenchmarkComponentSystem.cs 1.5 KB

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