BenchmarkComponent.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. try
  27. {
  28. using (Session session = networkComponent.Create(address))
  29. {
  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. }
  47. catch (RpcException e)
  48. {
  49. Log.Error(e.ToString());
  50. }
  51. catch (Exception e)
  52. {
  53. Log.Error(e.ToString());
  54. }
  55. }
  56. public override void Dispose()
  57. {
  58. if (this.Id == 0)
  59. {
  60. return;
  61. }
  62. base.Dispose();
  63. }
  64. }
  65. }