2
0

SshClientBenchmark.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 RunBigCommand()
  65. {
  66. using var command = _sshClient!.CreateCommand("head -c 10000000 /dev/urandom | base64"); // 10MB of data please
  67. var asyncResult = command.BeginExecute();
  68. command.OutputStream.CopyTo(Stream.Null);
  69. return command.EndExecute(asyncResult);
  70. }
  71. [Benchmark]
  72. public string ShellStreamReadLine()
  73. {
  74. using (var shellStream = _sshClient!.CreateShellStream("xterm", 80, 24, 800, 600, 1024, ShellStreamTerminalModes))
  75. {
  76. shellStream.WriteLine("for i in $(seq 500); do echo \"Within cells. Interlinked. $i\"; sleep 0.001; done; echo \"Username:\";");
  77. while (true)
  78. {
  79. var line = shellStream.ReadLine()!;
  80. if (line.EndsWith("500", StringComparison.Ordinal))
  81. {
  82. return line;
  83. }
  84. }
  85. }
  86. }
  87. [Benchmark]
  88. public string? ShellStreamExpect()
  89. {
  90. using (var shellStream = _sshClient!.CreateShellStream("xterm", 80, 24, 800, 600, 1024, ShellStreamTerminalModes))
  91. {
  92. shellStream.WriteLine("for i in $(seq 500); do echo \"Within cells. Interlinked. $i\"; sleep 0.001; done; echo \"Username:\";");
  93. return shellStream.Expect("Username:");
  94. }
  95. }
  96. }
  97. }