SshClientBenchmark.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using BenchmarkDotNet.Attributes;
  2. using Renci.SshNet.Common;
  3. using Renci.SshNet.IntegrationTests.TestsFixtures;
  4. namespace Renci.SshNet.IntegrationBenchmarks
  5. {
  6. [MemoryDiagnoser]
  7. [SimpleJob]
  8. public class SshClientBenchmark : IntegrationBenchmarkBase
  9. {
  10. private static readonly Dictionary<TerminalModes, uint> ShellStreamTerminalModes = new Dictionary<TerminalModes, uint>
  11. {
  12. { TerminalModes.ECHO, 0 }
  13. };
  14. private readonly InfrastructureFixture _infrastructureFixture;
  15. private SshClient? _sshClient;
  16. public SshClientBenchmark()
  17. {
  18. _infrastructureFixture = InfrastructureFixture.Instance;
  19. }
  20. [GlobalSetup]
  21. public async Task Setup()
  22. {
  23. await GlobalSetup().ConfigureAwait(false);
  24. _sshClient = new SshClient(_infrastructureFixture.SshServerHostName, _infrastructureFixture.SshServerPort, _infrastructureFixture.User.UserName, _infrastructureFixture.User.Password);
  25. await _sshClient.ConnectAsync(CancellationToken.None).ConfigureAwait(false);
  26. }
  27. [GlobalCleanup]
  28. public async Task Cleanup()
  29. {
  30. await GlobalCleanup().ConfigureAwait(false);
  31. }
  32. [Benchmark]
  33. public void Connect()
  34. {
  35. using var sshClient = new SshClient(_infrastructureFixture.SshServerHostName, _infrastructureFixture.SshServerPort, _infrastructureFixture.User.UserName, _infrastructureFixture.User.Password);
  36. sshClient.Connect();
  37. }
  38. [Benchmark]
  39. public async Task ConnectAsync()
  40. {
  41. using var sshClient = new SshClient(_infrastructureFixture.SshServerHostName, _infrastructureFixture.SshServerPort, _infrastructureFixture.User.UserName, _infrastructureFixture.User.Password);
  42. await sshClient.ConnectAsync(CancellationToken.None).ConfigureAwait(false);
  43. }
  44. [Benchmark]
  45. public string ConnectAndRunCommand()
  46. {
  47. using var sshClient = new SshClient(_infrastructureFixture.SshServerHostName, _infrastructureFixture.SshServerPort, _infrastructureFixture.User.UserName, _infrastructureFixture.User.Password);
  48. sshClient.Connect();
  49. return sshClient.RunCommand("echo $'test !@#$%^&*()_+{}:,./<>[];\\|'").Result;
  50. }
  51. [Benchmark]
  52. public async Task<string> ConnectAsyncAndRunCommand()
  53. {
  54. using var sshClient = new SshClient(_infrastructureFixture.SshServerHostName, _infrastructureFixture.SshServerPort, _infrastructureFixture.User.UserName, _infrastructureFixture.User.Password);
  55. await sshClient.ConnectAsync(CancellationToken.None).ConfigureAwait(false);
  56. return sshClient.RunCommand("echo $'test !@#$%^&*()_+{}:,./<>[];\\|'").Result;
  57. }
  58. [Benchmark]
  59. public string RunCommand()
  60. {
  61. return _sshClient!.RunCommand("echo $'test !@#$%^&*()_+{}:,./<>[];\\|'").Result;
  62. }
  63. [Benchmark]
  64. public string ShellStreamReadLine()
  65. {
  66. using (var shellStream = _sshClient!.CreateShellStream("xterm", 80, 24, 800, 600, 1024, ShellStreamTerminalModes))
  67. {
  68. shellStream.WriteLine("for i in $(seq 500); do echo \"Within cells. Interlinked. $i\"; sleep 0.001; done; echo \"Username:\";");
  69. while (true)
  70. {
  71. var line = shellStream.ReadLine()!;
  72. if (line.EndsWith("500", StringComparison.Ordinal))
  73. {
  74. return line;
  75. }
  76. }
  77. }
  78. }
  79. [Benchmark]
  80. public string? ShellStreamExpect()
  81. {
  82. using (var shellStream = _sshClient!.CreateShellStream("xterm", 80, 24, 800, 600, 1024, ShellStreamTerminalModes))
  83. {
  84. shellStream.WriteLine("for i in $(seq 500); do echo \"Within cells. Interlinked. $i\"; sleep 0.001; done; echo \"Username:\";");
  85. return shellStream.Expect("Username:");
  86. }
  87. }
  88. }
  89. }