Skip to content

Commit

Permalink
优化安装器组件代码,添加了复合安装器
Browse files Browse the repository at this point in the history
  • Loading branch information
YangSpring114 committed Aug 28, 2024
1 parent 1af89fa commit 6e51056
Show file tree
Hide file tree
Showing 13 changed files with 362 additions and 139 deletions.
1 change: 0 additions & 1 deletion MinecraftLaunch.BanchTest/MinecraftLaunch.BanchTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<PropertyGroup>
<Nullable>enable</Nullable>
<OutputType>Exe</OutputType>
<PublishAot>true</PublishAot>
<ImplicitUsings>enable</ImplicitUsings>
<TargetFramework>net8.0</TargetFramework>
<InvariantGlobalization>true</InvariantGlobalization>
Expand Down
85 changes: 76 additions & 9 deletions MinecraftLaunch.BanchTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,92 @@
using BenchmarkDotNet.Attributes;
using MinecraftLaunch.Components.Checker;
using MinecraftLaunch.Components.Resolver;
using MinecraftLaunch.Extensions;
using MinecraftLaunch.Classes.Interfaces;
using System.Threading.Tasks.Dataflow;
using MinecraftLaunch.Utilities;
using System.Diagnostics;

var summary = BenchmarkRunner.Run<BenchmarkClass>();
//var summary = BenchmarkRunner.Run<BenchmarkClass>();

await new BenchmarkClass().RunTaskParallel();
Console.ReadKey();

[MemoryDiagnoser]
public class BenchmarkClass {
private readonly ResourceChecker _checker;
private readonly GameResolver _gameResolver = new("D:\\GamePackages\\.minecraft");
private readonly GameResolver _gameResolver = new("C:\\Users\\w\\Desktop\\temp\\.minecraft");

public BenchmarkClass() {
_checker = new(_gameResolver.GetGameEntity("Life in the village"));
_checker = new(_gameResolver.GetGameEntity("1.16.5"));
_ = _checker.CheckAsync();
}

//[Benchmark]
//public Task<bool> RunTask() {
// return _checker.CheckAsync1();
//}
[Benchmark]
public ValueTask<bool> RunTaskParallel() {
return _checker.MissingResources.DownloadResourceParallel();
}

[Benchmark]
public ValueTask<bool> RunValueTask() {
return _checker.CheckAsync();
public ValueTask<bool> RunParallel() {
return _checker.MissingResources.DownloadResourceEntrysAsync();
}
}

public static class Downloader {
public static async ValueTask<bool> DownloadResourceParallel(this IEnumerable<IDownloadEntry> entries) {
int completedCount = 0;
int totalCount = entries.Count();

TransformBlock<IDownloadEntry, IDownloadEntry> transformBlock = new(x => {
return x;
}, new ExecutionDataflowBlockOptions {
BoundedCapacity = DownloadUitl.DefaultDownloadRequest.MultiThreadsCount,
MaxDegreeOfParallelism = DownloadUitl.DefaultDownloadRequest.MultiThreadsCount,
});

ActionBlock<IDownloadEntry> actionBlock = new(async x => {
await Task.Run(async () => {
if (string.IsNullOrEmpty(x.Url)) {
return;
}

await DownloadUitl.DownloadAsync(x).AsTask().ContinueWith(task => {
if (task.IsFaulted) {
if (!x.Verify()) {
Debug.WriteLine(task.Exception.Message);
return;
}

return;
}

var downloadResult = task.Result;
});

Interlocked.Increment(ref completedCount);
});
}, new ExecutionDataflowBlockOptions {
BoundedCapacity = DownloadUitl.DefaultDownloadRequest.MultiThreadsCount,
MaxDegreeOfParallelism = DownloadUitl.DefaultDownloadRequest.MultiThreadsCount,
});

var transformManyBlock = new TransformManyBlock<IEnumerable<IDownloadEntry>, IDownloadEntry>(chunk => chunk,
new ExecutionDataflowBlockOptions());

var linkOptions = new DataflowLinkOptions {
PropagateCompletion = true
};

transformManyBlock.LinkTo(transformBlock, linkOptions);
transformBlock.LinkTo(actionBlock, linkOptions);

if (entries != null) {
transformManyBlock.Post(entries);
}

transformManyBlock.Complete();
await actionBlock.Completion.WaitAsync(new CancellationToken());
return true;
}
}
1 change: 0 additions & 1 deletion MinecraftLaunch.Test/MinecraftLaunch.Simple.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\MinecraftLaunch.Skin\MinecraftLaunch.Skin.csproj" />
<ProjectReference Include="..\MinecraftLaunch\MinecraftLaunch.csproj" />
</ItemGroup>

Expand Down
Loading

0 comments on commit 6e51056

Please sign in to comment.