BenchmarkComponent.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using Model;
  3. namespace Hotfix
  4. {
  5. public class BenchmarkComponent: Component
  6. {
  7. private int k;
  8. private long time1 = TimeHelper.ClientNow();
  9. private async void Awake(string address)
  10. {
  11. try
  12. {
  13. NetOuterComponent networkComponent = Game.Scene.GetComponent<NetOuterComponent>();
  14. for (int i = 0; i < 100; i++)
  15. {
  16. await Game.Scene.GetComponent<TimerComponent>().WaitAsync(10);
  17. this.TestAsync(networkComponent, address, i);
  18. }
  19. }
  20. catch (Exception e)
  21. {
  22. Log.Error(e.ToString());
  23. }
  24. }
  25. public async void TestAsync(NetOuterComponent networkComponent, string address, int j)
  26. {
  27. Session session = null;
  28. try
  29. {
  30. session = networkComponent.Create(address);
  31. int i = 0;
  32. while (i < 10000000)
  33. {
  34. ++i;
  35. await session.Call<C2R_Ping, R2C_Ping>(new C2R_Ping());
  36. ++this.k;
  37. if (this.k % 100000 != 0)
  38. {
  39. continue;
  40. }
  41. long time2 = TimeHelper.ClientNow();
  42. long time = time2 - this.time1;
  43. this.time1 = time2;
  44. Log.Info($"{j} Benchmark k: {this.k} 每10W次耗时: {time} ms");
  45. }
  46. }
  47. catch (RpcException e)
  48. {
  49. Log.Error(e.ToString());
  50. }
  51. catch (Exception e)
  52. {
  53. Log.Error(e.ToString());
  54. }
  55. finally
  56. {
  57. session.Dispose();
  58. }
  59. }
  60. public override void Dispose()
  61. {
  62. if (this.Id == 0)
  63. {
  64. return;
  65. }
  66. base.Dispose();
  67. }
  68. }
  69. }