2
0

SshClientBenchmark.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using BenchmarkDotNet.Attributes;
  2. using Renci.SshNet.IntegrationTests.TestsFixtures;
  3. namespace Renci.SshNet.IntegrationBenchmarks
  4. {
  5. [MemoryDiagnoser]
  6. [SimpleJob]
  7. public class SshClientBenchmark : IntegrationBenchmarkBase
  8. {
  9. private readonly InfrastructureFixture _infrastructureFixture;
  10. private SshClient? _sshClient;
  11. public SshClientBenchmark()
  12. {
  13. _infrastructureFixture = InfrastructureFixture.Instance;
  14. }
  15. [GlobalSetup]
  16. public async Task Setup()
  17. {
  18. await GlobalSetup().ConfigureAwait(false);
  19. _sshClient = new SshClient(_infrastructureFixture.SshServerHostName, _infrastructureFixture.SshServerPort, _infrastructureFixture.User.UserName, _infrastructureFixture.User.Password);
  20. await _sshClient.ConnectAsync(CancellationToken.None).ConfigureAwait(false);
  21. }
  22. [GlobalCleanup]
  23. public async Task Cleanup()
  24. {
  25. await GlobalCleanup().ConfigureAwait(false);
  26. }
  27. [Benchmark]
  28. public void Connect()
  29. {
  30. using var sshClient = new SshClient(_infrastructureFixture.SshServerHostName, _infrastructureFixture.SshServerPort, _infrastructureFixture.User.UserName, _infrastructureFixture.User.Password);
  31. sshClient.Connect();
  32. }
  33. [Benchmark]
  34. public async Task ConnectAsync()
  35. {
  36. using var sshClient = new SshClient(_infrastructureFixture.SshServerHostName, _infrastructureFixture.SshServerPort, _infrastructureFixture.User.UserName, _infrastructureFixture.User.Password);
  37. await sshClient.ConnectAsync(CancellationToken.None).ConfigureAwait(false);
  38. }
  39. [Benchmark]
  40. public string ConnectAndRunCommand()
  41. {
  42. using var sshClient = new SshClient(_infrastructureFixture.SshServerHostName, _infrastructureFixture.SshServerPort, _infrastructureFixture.User.UserName, _infrastructureFixture.User.Password);
  43. sshClient.Connect();
  44. return sshClient.RunCommand("echo $'test !@#$%^&*()_+{}:,./<>[];\\|'").Result;
  45. }
  46. [Benchmark]
  47. public async Task<string> ConnectAsyncAndRunCommand()
  48. {
  49. using var sshClient = new SshClient(_infrastructureFixture.SshServerHostName, _infrastructureFixture.SshServerPort, _infrastructureFixture.User.UserName, _infrastructureFixture.User.Password);
  50. await sshClient.ConnectAsync(CancellationToken.None).ConfigureAwait(false);
  51. return sshClient.RunCommand("echo $'test !@#$%^&*()_+{}:,./<>[];\\|'").Result;
  52. }
  53. [Benchmark]
  54. public string RunCommand()
  55. {
  56. return _sshClient!.RunCommand("echo $'test !@#$%^&*()_+{}:,./<>[];\\|'").Result;
  57. }
  58. }
  59. }