Enhanced testing function

Separate xray-core and sing-box-core testing
pull/6237/head
2dust 2024-12-05 15:21:16 +08:00
parent 736c450161
commit 8aceff7480
1 changed files with 56 additions and 38 deletions

View File

@ -8,8 +8,8 @@ namespace ServiceLib.Services
public class SpeedtestService public class SpeedtestService
{ {
private Config? _config; private Config? _config;
private List<ServerTestItem> _selecteds;
private Action<SpeedTestResult>? _updateFunc; private Action<SpeedTestResult>? _updateFunc;
private bool _exitLoop = false; private bool _exitLoop = false;
public SpeedtestService(Config config, List<ProfileItem> selecteds, ESpeedActionType actionType, Action<SpeedTestResult> updateFunc) public SpeedtestService(Config config, List<ProfileItem> selecteds, ESpeedActionType actionType, Action<SpeedTestResult> updateFunc)
@ -17,7 +17,7 @@ namespace ServiceLib.Services
_config = config; _config = config;
_updateFunc = updateFunc; _updateFunc = updateFunc;
_selecteds = new List<ServerTestItem>(); var lstSelected = new List<ServerTestItem>();
foreach (var it in selecteds) foreach (var it in selecteds)
{ {
if (it.ConfigType == EConfigType.Custom) if (it.ConfigType == EConfigType.Custom)
@ -28,7 +28,7 @@ namespace ServiceLib.Services
{ {
continue; continue;
} }
_selecteds.Add(new ServerTestItem() lstSelected.Add(new ServerTestItem()
{ {
IndexId = it.IndexId, IndexId = it.IndexId,
Address = it.Address, Address = it.Address,
@ -36,8 +36,9 @@ namespace ServiceLib.Services
ConfigType = it.ConfigType ConfigType = it.ConfigType
}); });
} }
//clear test result //clear test result
foreach (var it in _selecteds) foreach (var it in lstSelected)
{ {
switch (actionType) switch (actionType)
{ {
@ -60,25 +61,44 @@ namespace ServiceLib.Services
} }
} }
MessageBus.Current.Listen<string>(EMsgCommand.StopSpeedtest.ToString()).Subscribe(ExitLoop);
Task.Run(async () => { await RunAsync(actionType, lstSelected); });
}
private async Task RunAsync(ESpeedActionType actionType, List<ServerTestItem> lstSelected)
{
if (actionType == ESpeedActionType.Tcping)
{
await RunTcpingAsync(lstSelected);
return;
}
List<List<ServerTestItem>> lstTest = new();
lstTest.Add(lstSelected.Where(t => t.ConfigType is not (EConfigType.Hysteria2 or EConfigType.TUIC or EConfigType.WireGuard)).ToList());
lstTest.Add(lstSelected.Where(t => t.ConfigType is EConfigType.Hysteria2 or EConfigType.TUIC or EConfigType.WireGuard).ToList());
foreach (var lst in lstTest)
{
switch (actionType) switch (actionType)
{ {
case ESpeedActionType.Tcping:
Task.Run(RunTcpingAsync);
break;
case ESpeedActionType.Realping: case ESpeedActionType.Realping:
Task.Run(RunRealPingAsync); await RunRealPingAsync(lst);
break; break;
case ESpeedActionType.Speedtest: case ESpeedActionType.Speedtest:
Task.Run(RunSpeedTestAsync); await RunSpeedTestAsync(lst);
break; break;
case ESpeedActionType.Mixedtest: case ESpeedActionType.Mixedtest:
Task.Run(RunMixedTestAsync); await RunMixedTestAsync(lst);
break; break;
} }
MessageBus.Current.Listen<string>(EMsgCommand.StopSpeedtest.ToString()).Subscribe(ExitLoop);
await Task.Delay(1000);
}
UpdateFunc("", ResUI.SpeedtestingCompleted);
} }
private void ExitLoop(string x) private void ExitLoop(string x)
@ -88,12 +108,12 @@ namespace ServiceLib.Services
UpdateFunc("", ResUI.SpeedtestingStop); UpdateFunc("", ResUI.SpeedtestingStop);
} }
private async Task RunTcpingAsync() private async Task RunTcpingAsync(List<ServerTestItem> selecteds)
{ {
try try
{ {
List<Task> tasks = []; List<Task> tasks = [];
foreach (var it in _selecteds) foreach (var it in selecteds)
{ {
if (it.ConfigType == EConfigType.Custom) if (it.ConfigType == EConfigType.Custom)
{ {
@ -127,12 +147,12 @@ namespace ServiceLib.Services
} }
} }
private async Task RunRealPingAsync() private async Task RunRealPingAsync(List<ServerTestItem> selecteds)
{ {
var pid = -1; var pid = -1;
try try
{ {
pid = await CoreHandler.Instance.LoadCoreConfigSpeedtest(_selecteds); pid = await CoreHandler.Instance.LoadCoreConfigSpeedtest(selecteds);
if (pid < 0) if (pid < 0)
{ {
UpdateFunc("", ResUI.FailedToRunCore); UpdateFunc("", ResUI.FailedToRunCore);
@ -142,7 +162,7 @@ namespace ServiceLib.Services
var downloadHandle = new DownloadService(); var downloadHandle = new DownloadService();
List<Task> tasks = new(); List<Task> tasks = new();
foreach (var it in _selecteds) foreach (var it in selecteds)
{ {
if (!it.AllowTest) if (!it.AllowTest)
{ {
@ -186,10 +206,10 @@ namespace ServiceLib.Services
} }
} }
private async Task RunSpeedTestAsync() private async Task RunSpeedTestAsync(List<ServerTestItem> selecteds)
{ {
var pid = -1; var pid = -1;
pid = await CoreHandler.Instance.LoadCoreConfigSpeedtest(_selecteds); pid = await CoreHandler.Instance.LoadCoreConfigSpeedtest(selecteds);
if (pid < 0) if (pid < 0)
{ {
UpdateFunc("", ResUI.FailedToRunCore); UpdateFunc("", ResUI.FailedToRunCore);
@ -201,7 +221,7 @@ namespace ServiceLib.Services
DownloadService downloadHandle = new(); DownloadService downloadHandle = new();
foreach (var it in _selecteds) foreach (var it in selecteds)
{ {
if (_exitLoop) if (_exitLoop)
{ {
@ -244,14 +264,13 @@ namespace ServiceLib.Services
{ {
await CoreHandler.Instance.CoreStopPid(pid); await CoreHandler.Instance.CoreStopPid(pid);
} }
UpdateFunc("", ResUI.SpeedtestingCompleted);
await ProfileExHandler.Instance.SaveTo(); await ProfileExHandler.Instance.SaveTo();
} }
private async Task RunSpeedTestMulti() private async Task RunSpeedTestMulti(List<ServerTestItem> selecteds)
{ {
var pid = -1; var pid = -1;
pid = await CoreHandler.Instance.LoadCoreConfigSpeedtest(_selecteds); pid = await CoreHandler.Instance.LoadCoreConfigSpeedtest(selecteds);
if (pid < 0) if (pid < 0)
{ {
UpdateFunc("", ResUI.FailedToRunCore); UpdateFunc("", ResUI.FailedToRunCore);
@ -263,7 +282,7 @@ namespace ServiceLib.Services
DownloadService downloadHandle = new(); DownloadService downloadHandle = new();
foreach (var it in _selecteds) foreach (var it in selecteds)
{ {
if (_exitLoop) if (_exitLoop)
{ {
@ -309,17 +328,16 @@ namespace ServiceLib.Services
{ {
await CoreHandler.Instance.CoreStopPid(pid); await CoreHandler.Instance.CoreStopPid(pid);
} }
UpdateFunc("", ResUI.SpeedtestingCompleted);
await ProfileExHandler.Instance.SaveTo(); await ProfileExHandler.Instance.SaveTo();
} }
private async Task RunMixedTestAsync() private async Task RunMixedTestAsync(List<ServerTestItem> selecteds)
{ {
await RunRealPingAsync(); await RunRealPingAsync(selecteds);
await Task.Delay(1000); await Task.Delay(1000);
await RunSpeedTestMulti(); await RunSpeedTestMulti(selecteds);
} }
private async Task<string> GetRealPingTime(DownloadService downloadHandle, IWebProxy webProxy) private async Task<string> GetRealPingTime(DownloadService downloadHandle, IWebProxy webProxy)