BenchmarkComponent.cs 1.3 KB

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