BenchmarkComponentSystem.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Net;
  3. using System.Threading.Tasks;
  4. namespace ET
  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 < 2000; i++)
  23. {
  24. self.TestAsync(networkComponent, ipEndPoint, i);
  25. }
  26. }
  27. catch (Exception e)
  28. {
  29. Log.Error(e);
  30. }
  31. }
  32. public static async void 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 (Exception e)
  47. {
  48. Log.Error(e);
  49. }
  50. }
  51. public static async Task Send(this BenchmarkComponent self, Session session, int j)
  52. {
  53. try
  54. {
  55. await session.Call(new C2R_Ping());
  56. ++self.k;
  57. if (self.k % 100000 != 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} 每10W次耗时: {time} ms {session.Network.Count}");
  65. }
  66. catch (Exception e)
  67. {
  68. Log.Error(e);
  69. }
  70. }
  71. }
  72. }