BenchmarkComponent.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Threading.Tasks;
  3. namespace Model
  4. {
  5. [ObjectEvent]
  6. public class BenchmarkComponentEvent : ObjectEvent<BenchmarkComponent>, IAwake<string>
  7. {
  8. public void Awake(string address)
  9. {
  10. this.Get().Awake(address);
  11. }
  12. }
  13. public class BenchmarkComponent: Component
  14. {
  15. private int k;
  16. private long time1 = TimeHelper.ClientNow();
  17. public async void Awake(string address)
  18. {
  19. try
  20. {
  21. NetOuterComponent networkComponent = Game.Scene.GetComponent<NetOuterComponent>();
  22. for (int i = 0; i < 100; i++)
  23. {
  24. await Game.Scene.GetComponent<TimerComponent>().WaitAsync(1000);
  25. this.TestAsync(networkComponent, address, i);
  26. }
  27. }
  28. catch (Exception e)
  29. {
  30. Log.Error(e.ToString());
  31. }
  32. }
  33. public async void TestAsync(NetOuterComponent networkComponent, string address, int j)
  34. {
  35. try
  36. {
  37. using (Session session = networkComponent.Create(address))
  38. {
  39. int i = 0;
  40. while (i < 100000000)
  41. {
  42. ++i;
  43. await Game.Scene.GetComponent<TimerComponent>().WaitAsync(10);
  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 % 10000 != 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. }