mirror of
https://github.com/sshnet/SSH.NET.git
synced 2026-10-28 14:08:37 +08:00
* Fix a few issues with ShellStream The main change is to replace the Queue<byte> with a byte[] and a couple of variables which index the start and end of the data. The remainder is mainly slightly more careful locking semantics. It also implements Expect(string) separately so that it can work on the bytes and skip a lot of encoding work (this is where I wish ShellStream derived from StreamReader). One possibly contentious point: in fixing the Write behaviour I chose to remove the "outgoing" buffer and immediately send the data across the channel. Write(string) and WriteLine(string) were already doing this, and I felt it was better to change Write(byte[]) to match rather than changing the string methods. * Integrate expectSize (as "windowSize" parameter) * Rename "windowSize" to "lookback" --------- Co-authored-by: Wojciech Nagórski <wojtpl2@gmail.com>
105 lines
4.0 KiB
C#
105 lines
4.0 KiB
C#
using BenchmarkDotNet.Attributes;
|
|
|
|
using Renci.SshNet.Common;
|
|
using Renci.SshNet.IntegrationTests.TestsFixtures;
|
|
|
|
namespace Renci.SshNet.IntegrationBenchmarks
|
|
{
|
|
[MemoryDiagnoser]
|
|
[SimpleJob]
|
|
public class SshClientBenchmark : IntegrationBenchmarkBase
|
|
{
|
|
private static readonly Dictionary<TerminalModes, uint> ShellStreamTerminalModes = new Dictionary<TerminalModes, uint>
|
|
{
|
|
{ TerminalModes.ECHO, 0 }
|
|
};
|
|
|
|
private readonly InfrastructureFixture _infrastructureFixture;
|
|
private SshClient? _sshClient;
|
|
|
|
public SshClientBenchmark()
|
|
{
|
|
_infrastructureFixture = InfrastructureFixture.Instance;
|
|
}
|
|
|
|
[GlobalSetup]
|
|
public async Task Setup()
|
|
{
|
|
await GlobalSetup().ConfigureAwait(false);
|
|
_sshClient = new SshClient(_infrastructureFixture.SshServerHostName, _infrastructureFixture.SshServerPort, _infrastructureFixture.User.UserName, _infrastructureFixture.User.Password);
|
|
await _sshClient.ConnectAsync(CancellationToken.None).ConfigureAwait(false);
|
|
}
|
|
|
|
[GlobalCleanup]
|
|
public async Task Cleanup()
|
|
{
|
|
await GlobalCleanup().ConfigureAwait(false);
|
|
}
|
|
|
|
[Benchmark]
|
|
public void Connect()
|
|
{
|
|
using var sshClient = new SshClient(_infrastructureFixture.SshServerHostName, _infrastructureFixture.SshServerPort, _infrastructureFixture.User.UserName, _infrastructureFixture.User.Password);
|
|
sshClient.Connect();
|
|
}
|
|
|
|
[Benchmark]
|
|
public async Task ConnectAsync()
|
|
{
|
|
using var sshClient = new SshClient(_infrastructureFixture.SshServerHostName, _infrastructureFixture.SshServerPort, _infrastructureFixture.User.UserName, _infrastructureFixture.User.Password);
|
|
await sshClient.ConnectAsync(CancellationToken.None).ConfigureAwait(false);
|
|
}
|
|
|
|
[Benchmark]
|
|
public string ConnectAndRunCommand()
|
|
{
|
|
using var sshClient = new SshClient(_infrastructureFixture.SshServerHostName, _infrastructureFixture.SshServerPort, _infrastructureFixture.User.UserName, _infrastructureFixture.User.Password);
|
|
sshClient.Connect();
|
|
return sshClient.RunCommand("echo $'test !@#$%^&*()_+{}:,./<>[];\\|'").Result;
|
|
}
|
|
|
|
[Benchmark]
|
|
public async Task<string> ConnectAsyncAndRunCommand()
|
|
{
|
|
using var sshClient = new SshClient(_infrastructureFixture.SshServerHostName, _infrastructureFixture.SshServerPort, _infrastructureFixture.User.UserName, _infrastructureFixture.User.Password);
|
|
await sshClient.ConnectAsync(CancellationToken.None).ConfigureAwait(false);
|
|
return sshClient.RunCommand("echo $'test !@#$%^&*()_+{}:,./<>[];\\|'").Result;
|
|
}
|
|
|
|
[Benchmark]
|
|
public string RunCommand()
|
|
{
|
|
return _sshClient!.RunCommand("echo $'test !@#$%^&*()_+{}:,./<>[];\\|'").Result;
|
|
}
|
|
|
|
[Benchmark]
|
|
public string ShellStreamReadLine()
|
|
{
|
|
using (var shellStream = _sshClient!.CreateShellStream("xterm", 80, 24, 800, 600, 1024, ShellStreamTerminalModes))
|
|
{
|
|
shellStream.WriteLine("for i in $(seq 500); do echo \"Within cells. Interlinked. $i\"; sleep 0.001; done; echo \"Username:\";");
|
|
|
|
while (true)
|
|
{
|
|
var line = shellStream.ReadLine()!;
|
|
|
|
if (line.EndsWith("500", StringComparison.Ordinal))
|
|
{
|
|
return line;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
[Benchmark]
|
|
public string? ShellStreamExpect()
|
|
{
|
|
using (var shellStream = _sshClient!.CreateShellStream("xterm", 80, 24, 800, 600, 1024, ShellStreamTerminalModes))
|
|
{
|
|
shellStream.WriteLine("for i in $(seq 500); do echo \"Within cells. Interlinked. $i\"; sleep 0.001; done; echo \"Username:\";");
|
|
return shellStream.Expect("Username:");
|
|
}
|
|
}
|
|
}
|
|
}
|