BenchmarkComponent.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Net;
  3. using System.Threading.Tasks;
  4. namespace Model
  5. {
  6. [ObjectEvent]
  7. public class BenchmarkComponentEvent : ObjectEvent<BenchmarkComponent>, IAwake<IPEndPoint>
  8. {
  9. public void Awake(IPEndPoint ipEndPoint)
  10. {
  11. this.Get().Awake(ipEndPoint);
  12. }
  13. }
  14. public class BenchmarkComponent: Component
  15. {
  16. private int k;
  17. private long time1 = TimeHelper.ClientNow();
  18. public async void Awake(IPEndPoint ipEndPoint)
  19. {
  20. try
  21. {
  22. NetOuterComponent networkComponent = Game.Scene.GetComponent<NetOuterComponent>();
  23. for (int i = 0; i < 1000; i++)
  24. {
  25. await Game.Scene.GetComponent<TimerComponent>().WaitAsync(100);
  26. this.TestAsync(networkComponent, ipEndPoint, i);
  27. }
  28. }
  29. catch (Exception e)
  30. {
  31. Log.Error(e.ToString());
  32. }
  33. }
  34. public async void TestAsync(NetOuterComponent networkComponent, IPEndPoint ipEndPoint, int j)
  35. {
  36. try
  37. {
  38. using (Session session = networkComponent.Create(ipEndPoint))
  39. {
  40. int i = 0;
  41. while (i < 100000000)
  42. {
  43. ++i;
  44. await this.Send(session, j);
  45. }
  46. }
  47. }
  48. catch (RpcException e)
  49. {
  50. Log.Error(e.ToString());
  51. }
  52. catch (Exception e)
  53. {
  54. Log.Error(e.ToString());
  55. }
  56. }
  57. public async Task Send(Session session, int j)
  58. {
  59. try
  60. {
  61. await session.Call<R2C_Ping>(new C2R_Ping());
  62. ++this.k;
  63. if (this.k % 100000 != 0)
  64. {
  65. return;
  66. }
  67. long time2 = TimeHelper.ClientNow();
  68. long time = time2 - this.time1;
  69. this.time1 = time2;
  70. Log.Info($"Benchmark k: {this.k} 每10W次耗时: {time} ms");
  71. }
  72. catch (Exception e)
  73. {
  74. Log.Error(e.ToString());
  75. }
  76. }
  77. public override void Dispose()
  78. {
  79. if (this.Id == 0)
  80. {
  81. return;
  82. }
  83. base.Dispose();
  84. }
  85. }
  86. }