Remotely/Tests/LoadTester/Program.cs
Jared Goodwin 3ef4cdf81a
Extract remote control functionality into separate library. (#539)
* Convert server to new single-file startup model.

* Add remote control implementations.

* Implement IViewerAuthorizer.

* Update hub endpoints.

* Implement HubEventHandler.

* Implement ViewerHubDataProvider.

* Implement page data provider.

* Implement RCL and refactor.

* Update submodule.

* Replace submodule with NuGet.

* Update copy URL.

* Update NuGet.

* Remove deprecated WebRTC.

* Remove deprecated WebRTC.

* Update Immense.RemoteControl

* Building out desktop projects.

* Bring more services into submodule.

* Update submodule.

* Update submodule.

* Refactoring for module.

* Update submodule.

* Update submodule

* Got Windows desktop app running.

* Refactor for submodule changes.

* FIx unattended session start.

* Switch desktop app out of console mode.

* Fix tests.

* Update publishing.

* Remove ClickOnce middleware.

* Remove ClickOnce remnants.

* Update submodule

* Add some logging.

* Update Linux path.

* Update submodule.

* Add cleanup service for unattended sessions that failed to start.

* Update submodule.

* Fix chat.

* Add ValidateExecutableReferencesMatchSelfContained property.

* Add other submodule projects.  Align checkbox.

* Update submodule.  Reduce deserialization in the browser, resulting in faster renders.

* Update submodule.

* Update submodule.

* Update submodule.

* Update submodule.

* Add orgId back for branding.

* Get branding loading in desktop apps.

* Update submodule.

* Create log dir.

* Refactor version check on config page.

* Update submodule.

* Update submodule.

* Change submodule URL.

* Correct namespace.

* Update submodule.

* Checkout submodules recursively.
2022-12-23 06:39:12 -08:00

116 lines
4.3 KiB
C#

using Microsoft.AspNetCore.SignalR.Client;
using Remotely.Agent.Services;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Remotely.Tests.LoadTester
{
internal class Program
{
private static readonly SemaphoreSlim _lock = new(10, 10);
private static readonly double _heartbeatMs = TimeSpan.FromMinutes(1).TotalMilliseconds;
private static int _agentCount;
private static string _organizationId;
private static string _serverurl;
private static DeviceInformationServiceWin _deviceInfo;
private static void Main(string[] args)
{
_deviceInfo = new DeviceInformationServiceWin();
ConnectAgents();
Console.Write("Press Enter to exit...");
Console.ReadLine();
}
private static void ConnectAgents()
{
try
{
if (!CommandLineParser.CommandLineArgs.ContainsKey("serverurl") ||
!CommandLineParser.CommandLineArgs.ContainsKey("organizationid") ||
!CommandLineParser.CommandLineArgs.ContainsKey("agentcount"))
{
Console.WriteLine("Command line arguments must include all of the following: ");
Console.WriteLine();
Console.WriteLine("-serverurl [full URL of the Remotely server]");
Console.WriteLine();
Console.WriteLine("-organizationid [organization ID that the device will belong to]");
Console.WriteLine();
Console.WriteLine("-agentcount [the number of agent connections to simulate]");
Console.WriteLine();
Console.WriteLine("Press Enter to exit...");
Environment.Exit(0);
}
_agentCount = int.Parse(CommandLineParser.CommandLineArgs["agentcount"]);
_organizationId = CommandLineParser.CommandLineArgs["organizationid"];
_serverurl = CommandLineParser.CommandLineArgs["serverurl"];
for (var i = 0; i < _agentCount; i++)
{
_ = StartAgent(i);
}
}
catch (Exception ex)
{
Console.WriteLine("An error occurred. Check your syntex. Error: ");
Console.WriteLine();
Console.WriteLine(ex.Message);
}
}
private static async Task StartAgent(int i)
{
try
{
await _lock.WaitAsync();
var deviceId = Guid.NewGuid().ToString();
var hubConnection = new HubConnectionBuilder()
.WithUrl(_serverurl + "/hubs/service")
.Build();
Console.WriteLine("Connecting device number " + i.ToString());
await hubConnection.StartAsync();
var device = await _deviceInfo.CreateDevice(deviceId, _organizationId);
device.DeviceName = "TestDevice-" + Guid.NewGuid();
var result = await hubConnection.InvokeAsync<bool>("DeviceCameOnline", device);
if (!result)
{
Console.WriteLine($"Device {i} failed to come online.");
return;
}
_ = Task.Run(async () =>
{
await Task.Delay(new Random().Next(1, (int)_heartbeatMs));
var heartbeatTimer = new System.Timers.Timer(_heartbeatMs);
heartbeatTimer.Elapsed += async (sender, args) =>
{
var currentInfo = await _deviceInfo.CreateDevice(device.ID, _organizationId);
currentInfo.DeviceName = device.DeviceName;
await hubConnection.SendAsync("DeviceHeartbeat", currentInfo);
};
heartbeatTimer.Start();
});
}
catch (Exception ex)
{
Console.WriteLine($"Device {i} failed to connect.");
Console.WriteLine(ex.Message);
}
finally
{
_lock.Release();
}
}
}
}