Skip to content

Commit b79897c

Browse files
committed
完成对接实现逻辑
1 parent 0af8120 commit b79897c

2 files changed

Lines changed: 132 additions & 3 deletions

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Serialization;
3+
using dotnetCampus.Ipc.IpcRouteds.DirectRouteds;
4+
using DotNetCampus.MediaConverters.CommandLineHandlers;
5+
using DotNetCampus.MediaConverters.Contexts;
6+
using DotNetCampus.MediaConverters.Contexts.IpcContexts;
7+
8+
namespace DotNetCampus.MediaConverters.Tests.Tool;
9+
10+
[TestClass]
11+
public class MediaConverterIpcTests
12+
{
13+
[TestMethod]
14+
public async Task TestBatchImage()
15+
{
16+
var testFolder = Path.Join(TestHelper.WorkingDirectory.FullName, Path.GetRandomFileName());
17+
Directory.CreateDirectory(testFolder);
18+
19+
var ipcHandler = new IpcHandler()
20+
{
21+
IpcName = Guid.NewGuid().ToString(),
22+
WorkingFolder = testFolder,
23+
ShouldLogToConsole = true,
24+
ShouldLogToFile = true,
25+
};
26+
27+
var task = Task.Run(async () =>
28+
{
29+
var provider = new JsonIpcDirectRoutedProvider();
30+
var clientProxy = await provider.GetAndConnectClientAsync(ipcHandler.IpcName);
31+
32+
var response = await clientProxy.GetResponseAsync<IpcConvertImageResponse>(IpcPaths.RequestConvertImage, new IpcConvertImageRequest()
33+
{
34+
TraceId = "TraceId-1",
35+
InputFile = TestFileProvider.GetTestFile(TestFileProvider.DefaultTestImageName).FullName,
36+
OutputFile = Path.Join(testFolder, "Output1.png"),
37+
ConvertConfigurationFile = ToConfigurationFile(new ImageConvertContext()
38+
{
39+
ImageConvertTaskList =
40+
[
41+
new SetSoftEdgeEffectTask()
42+
{
43+
Radius = 20
44+
}
45+
]
46+
},testFolder)
47+
});
48+
49+
Assert.IsNotNull(response);
50+
Assert.AreEqual(MediaConverterErrorCode.Success.Code, response.Code);
51+
52+
response = await clientProxy.GetResponseAsync<IpcConvertImageResponse>(IpcPaths.RequestConvertImage, new IpcConvertImageRequest()
53+
{
54+
TraceId = "TraceId-2",
55+
InputFile = TestFileProvider.GetTestFile(TestFileProvider.DefaultTestImageName).FullName,
56+
OutputFile = Path.Join(testFolder, "Output2.png"),
57+
ConvertConfigurationFile = ToConfigurationFile(new ImageConvertContext()
58+
{
59+
ImageConvertTaskList =
60+
[
61+
new SetLuminanceEffectTask()
62+
],
63+
ShouldCopyNewFile = false,
64+
}, testFolder)
65+
});
66+
67+
Assert.IsNotNull(response);
68+
Assert.AreEqual(MediaConverterErrorCode.Success.Code, response.Code);
69+
70+
response = await clientProxy.GetResponseAsync<IpcConvertImageResponse>(IpcPaths.RequestConvertImage, new IpcConvertImageRequest()
71+
{
72+
TraceId = "TraceId-3",
73+
InputFile = TestFileProvider.GetTestFile(TestFileProvider.DefaultTestImageName).FullName,
74+
OutputFile = Path.Join(testFolder, "Output3.png"),
75+
ConvertConfigurationFile = ToConfigurationFile(new ImageConvertContext()
76+
{
77+
ImageConvertTaskList =
78+
[
79+
new SetContrastTask()
80+
{
81+
Percentage = 0.7f
82+
}
83+
]
84+
}, testFolder)
85+
});
86+
87+
Assert.IsNotNull(response);
88+
Assert.AreEqual(MediaConverterErrorCode.Success.Code, response.Code);
89+
90+
var ipcExitResponse = await clientProxy.GetResponseAsync<IpcExitResponse>(IpcPaths.Exit, new IpcExitRequest()
91+
{
92+
Reason = "正常退出"
93+
});
94+
Assert.IsNotNull(ipcExitResponse);
95+
Assert.AreEqual(MediaConverterErrorCode.Success.Code, response.Code);
96+
});
97+
98+
await ipcHandler.RunAsync();
99+
100+
await task;
101+
}
102+
103+
private static string ToConfigurationFile(ImageConvertContext imageConvertContext,string testFolder)
104+
{
105+
var jsonText = JsonSerializer.Serialize(imageConvertContext,
106+
new JsonSerializerOptions(MediaConverterJsonSerializerSourceGenerationContext.Default.Options)
107+
{
108+
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
109+
});
110+
var configFile = Path.Join(testFolder, "Config.json");
111+
File.WriteAllText(configFile, jsonText);
112+
return configFile;
113+
}
114+
}

src/MediaConverters/MediaConverters.Tool/Program.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ static async Task<int> Main(string[] args)
6464

6565
internal static async Task<ErrorCode> RunAsync(ConvertHandler convertHandler)
6666
{
67-
var stopwatch = Stopwatch.StartNew();
67+
var stepStopwatch = Stopwatch.StartNew();
68+
var totalStopwatch = Stopwatch.StartNew();
6869

6970
var jsonText = await File.ReadAllTextAsync(convertHandler.ConvertConfigurationFile);
7071

@@ -89,8 +90,15 @@ internal static async Task<ErrorCode> RunAsync(ConvertHandler convertHandler)
8990
ShouldLogToConsole = convertHandler.ShouldLogToConsole ?? false,
9091
ShouldLogToFile = convertHandler.ShouldLogToFile ?? false,
9192
};
93+
94+
context.LogMessage($"[Performance] FromJsonText cost {stepStopwatch.ElapsedMilliseconds}ms Total {totalStopwatch.ElapsedMilliseconds}ms");
95+
stepStopwatch.Restart();
96+
9297
using var imageFileOptimizationResult = await ImageFileOptimization.OptimizeImageFileAsync(context, useAreaSizeLimit, copyNewFile);
9398

99+
context.LogMessage($"[Performance] OptimizeImageFileAsync cost {stepStopwatch.ElapsedMilliseconds}ms Total {totalStopwatch.ElapsedMilliseconds}ms");
100+
stepStopwatch.Restart();
101+
94102
if (!imageFileOptimizationResult.IsSuccess)
95103
{
96104
var errorMessage = $"Failed to convert image file '{inputFile.FullName}'. Reason: {imageFileOptimizationResult.FailureReason}";
@@ -137,19 +145,26 @@ internal static async Task<ErrorCode> RunAsync(ConvertHandler convertHandler)
137145
workerProvider.Run(image, imageConvertTask);
138146
}
139147

148+
context.LogMessage($"[Performance] RunWorkerProvider cost {stepStopwatch.ElapsedMilliseconds}ms Total {totalStopwatch.ElapsedMilliseconds}ms");
149+
stepStopwatch.Restart();
150+
140151
await image.SaveAsPngAsync(convertHandler.OutputFile, new PngEncoder()
141152
{
142153
ColorType = PngColorType.RgbWithAlpha,
143154
BitDepth = PngBitDepth.Bit8,
144155
});
156+
157+
context.LogMessage($"[Performance] SaveAsPngAsync cost {stepStopwatch.ElapsedMilliseconds}ms Total {totalStopwatch.ElapsedMilliseconds}ms");
145158
}
146159
else
147160
{
148161
optimizedImageFile.CopyTo(convertHandler.OutputFile, overwrite: true);
162+
163+
context.LogMessage($"[Performance] CopyTo cost {stepStopwatch.ElapsedMilliseconds}ms Total {totalStopwatch.ElapsedMilliseconds}ms");
149164
}
150165

151-
stopwatch.Stop();
152-
context.LogMessage($"Success converted image. Cost {stopwatch.ElapsedMilliseconds}ms. OutputFile:'{convertHandler.OutputFile}'");
166+
totalStopwatch.Stop();
167+
context.LogMessage($"Success converted image. Cost {totalStopwatch.ElapsedMilliseconds}ms. OutputFile:'{convertHandler.OutputFile}'");
153168

154169
return ErrorCode.Success;
155170
}

0 commit comments

Comments
 (0)