Skip to content

Commit 8f14581

Browse files
committed
记录耗时信息
1 parent 17c10ae commit 8f14581

3 files changed

Lines changed: 66 additions & 3 deletions

File tree

src/MediaConverters/MediaConverters.Lib/Imaging/Optimizations/EnhancedGraphicsMetafileOptimization.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ private static ImageFileOptimizationResult ConvertInLinux(ImageFileOptimizationC
4949

5050
// 在 Linux 上,先尝试使用 Inkscape 进行转换,如失败,再使用 libwmf 进行转换
5151
// 调用 Inkscape 进行转换
52+
var stopwatch = Stopwatch.StartNew();
5253
ImageFileOptimizationResult result = ConvertWithInkscape(context);
54+
stopwatch.Stop();
55+
context.LogMessage($"ConvertWithInkscape Cost {stopwatch.ElapsedMilliseconds}ms");
5356
if (result.OptimizedImageFile is { } svgFile)
5457
{
5558
return ConvertSvgToPngFile(svgFile);
@@ -73,7 +76,10 @@ private static ImageFileOptimizationResult ConvertInLinux(ImageFileOptimizationC
7376

7477
// 使用 libwmf 进行转换
7578

79+
stopwatch.Restart();
7680
result = ConvertWithInkscapeLibWmf(context);
81+
stopwatch.Stop();
82+
context.LogMessage($"ConvertWithInkscapeLibWmf Cost {stopwatch.ElapsedMilliseconds}ms");
7783
if (result.OptimizedImageFile is { } svgLibWmfFile)
7884
{
7985
return ConvertSvgToPngFile(svgLibWmfFile);
@@ -87,10 +93,13 @@ ImageFileOptimizationResult ConvertSvgToPngFile(FileInfo svgImageFile)
8793
{
8894
try
8995
{
96+
stopwatch.Restart();
9097
var convertSvgToPngFile = ImageFileOptimization.ConvertSvgToPngFile(context with
9198
{
9299
ImageFile = svgImageFile
93100
});
101+
stopwatch.Stop();
102+
context.LogMessage($"Convert svg to png file Cost {stopwatch.ElapsedMilliseconds}ms. File='{svgImageFile.FullName}'");
94103
if (convertSvgToPngFile is not null)
95104
{
96105
return new ImageFileOptimizationResult()

src/MediaConverters/MediaConverters.Lib/Imaging/Optimizations/ImageFileOptimization.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics;
23
using System.IO;
34
using System.Threading;
45
using System.Threading.Tasks;
@@ -29,6 +30,8 @@ public static class ImageFileOptimization
2930
/// <returns></returns>
3031
public static async Task<ImageFileOptimizationResult> OptimizeImageFileAsync(ImageFileOptimizationContext context, bool useAreaSizeLimit = true, bool copyNewFile = true)
3132
{
33+
var stopwatch = Stopwatch.StartNew();
34+
3235
var imageFile = context.ImageFile;
3336
var workingFolder = context.WorkingFolder;
3437
var maxImageWidth = context.MaxImageWidth;
@@ -62,6 +65,10 @@ public static async Task<ImageFileOptimizationResult> OptimizeImageFileAsync(Ima
6265
};
6366
}
6467

68+
stopwatch.Stop();
69+
context.LogMessage($"Copy new file Cost {stopwatch.ElapsedMilliseconds}ms. ImageFile: '{context.ImageFile.FullName}'");
70+
stopwatch.Restart();
71+
6572
if (IsExtension(".svg"))
6673
{
6774
// 如果是 svg 那就直接转换了,因为后续叠加特效等逻辑都不能支持 SVG 格式
@@ -96,6 +103,9 @@ public static async Task<ImageFileOptimizationResult> OptimizeImageFileAsync(Ima
96103
IsExtension(".emf"))
97104
{
98105
var result = EnhancedGraphicsMetafileOptimization.ConvertWmfOrEmfToPngFile(context);
106+
stopwatch.Stop();
107+
context.LogMessage($"ConvertWmfOrEmfToPngFile Cost {stopwatch.ElapsedMilliseconds}ms. ImageFile: '{context.ImageFile.FullName}'");
108+
99109
if (result.OptimizedImageFile is not null)
100110
{
101111
context.LogMessage($"Success ConvertWmfOrEmfToPngFile. Update current image file to '{result.OptimizedImageFile}'");
@@ -110,6 +120,7 @@ public static async Task<ImageFileOptimizationResult> OptimizeImageFileAsync(Ima
110120
}
111121
}
112122

123+
stopwatch.Restart();
113124
context.LogMessage($"Start optimize image with ImageSharp. ImageFile: '{context.ImageFile.FullName}'");
114125

115126
Image<Rgba32> image;
@@ -119,6 +130,9 @@ public static async Task<ImageFileOptimizationResult> OptimizeImageFileAsync(Ima
119130
FileShare.Read);
120131

121132
image = await Image.LoadAsync<Rgba32>(fileStream);
133+
134+
stopwatch.Stop();
135+
context.LogMessage($"Load image with ImageSharp Cost {stopwatch.ElapsedMilliseconds}ms. ImageFile: '{context.ImageFile.FullName}'");
122136
}
123137
catch (ImageFormatException e)
124138
{
@@ -165,16 +179,25 @@ public static async Task<ImageFileOptimizationResult> OptimizeImageFileAsync(Ima
165179
};
166180
}
167181

182+
stopwatch.Restart();
168183
OptimizeImage(image, maxImageWidth, maxImageHeight, useAreaSizeLimit);
184+
stopwatch.Stop();
185+
context.LogMessage(
186+
$"Optimize image with ImageSharp Cost {stopwatch.ElapsedMilliseconds}ms. ImageFile: '{context.ImageFile.FullName}'");
169187

170188
// 重新保存即可
171189
var outputImageFilePath = Path.Join(workingFolder.FullName, $"{Path.GetRandomFileName()}.png");
190+
stopwatch.Restart();
172191
await image.SaveAsPngAsync(outputImageFilePath, new PngEncoder()
173192
{
174193
ColorType = PngColorType.RgbWithAlpha,
175194
BitDepth = PngBitDepth.Bit8,
176195
});
177196

197+
stopwatch.Stop();
198+
context.LogMessage(
199+
$"Save optimized image with ImageSharp Cost {stopwatch.ElapsedMilliseconds}ms. OutputImageFile: '{outputImageFilePath}'");
200+
178201
return new ImageFileOptimizationResult()
179202
{
180203
Image = image,
@@ -313,11 +336,27 @@ public static void LimitImageSize(Image<Rgba32> image, int? maxImageWidth, int?
313336
var imageFile = context.ImageFile;
314337
var workingFolder = context.WorkingFolder;
315338

339+
var stopwatch = Stopwatch.StartNew();
340+
341+
316342
using var skSvg = new SKSvg();
317343
using var skPicture = skSvg.Load(imageFile.FullName);
344+
345+
stopwatch.Stop();
346+
context.LogMessage($"Load sk svg Cost {stopwatch.ElapsedMilliseconds}ms");
347+
318348
var outputFile = Path.Join(workingFolder.FullName,
319349
$"SVG_{Path.GetRandomFileName()}.png");
350+
351+
stopwatch.Restart();
320352
var canSave = skSvg.Save(outputFile, SKColors.Transparent);
353+
stopwatch.Stop();
354+
context.LogMessage($"Save sk svg to png Cost {stopwatch.ElapsedMilliseconds}ms. OutputFile='{outputFile}'");
355+
356+
//var skBitmap = skPicture.ToBitmap(SKColor.Empty, 1,1,SKColorType.Bgra8888,SKAlphaType.Unpremul,null!);
357+
//var pixelSpan = skBitmap.GetPixelSpan();
358+
//Image<Rgba32>.LoadPixelData<Rgba32>(pixelSpan, skBitmap.Width, skBitmap.Height)
359+
321360
if (canSave && File.Exists(outputFile))
322361
{
323362
return new FileInfo(outputFile);

src/MediaConverters/MediaConverters.Tool/Program.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,19 @@ static async Task<int> Main(string[] args)
5555

5656
internal static async Task<ErrorCode> RunAsync(Options options)
5757
{
58-
var stopwatch = Stopwatch.StartNew();
58+
var total = Stopwatch.StartNew();
5959

60+
var stopwatch = Stopwatch.StartNew();
6061
var jsonText = await File.ReadAllTextAsync(options.ConvertConfigurationFile);
62+
stopwatch.Stop();
63+
Console.WriteLine($"ReadJsonFile Cost {stopwatch.ElapsedMilliseconds}ms");
6164

65+
stopwatch.Restart();
6266
var imageConvertContext = ImageConvertContext.FromJsonText(jsonText);
67+
stopwatch.Stop();
68+
Console.WriteLine($"ParseJsonFile Cost {stopwatch.ElapsedMilliseconds}ms");
69+
stopwatch.Restart();
70+
6371

6472
if (imageConvertContext is null)
6573
{
@@ -81,6 +89,9 @@ internal static async Task<ErrorCode> RunAsync(Options options)
8189
ShouldLogToFile = options.ShouldLogToFile ?? false,
8290
};
8391
using var imageFileOptimizationResult = await ImageFileOptimization.OptimizeImageFileAsync(context, useAreaSizeLimit, copyNewFile);
92+
stopwatch.Stop();
93+
Console.WriteLine($"OptimizeImageFileAsync Cost {stopwatch.ElapsedMilliseconds}ms. File='{inputFile.FullName}'");
94+
stopwatch.Restart();
8495

8596
if (!imageFileOptimizationResult.IsSuccess)
8697
{
@@ -131,14 +142,18 @@ internal static async Task<ErrorCode> RunAsync(Options options)
131142
ColorType = PngColorType.RgbWithAlpha,
132143
BitDepth = PngBitDepth.Bit8,
133144
});
145+
146+
stopwatch.Stop();
147+
Console.WriteLine($"Save WorkerProvider Png Cost {stopwatch.ElapsedMilliseconds}ms. File='{inputFile.FullName}'");
148+
stopwatch.Restart();
134149
}
135150
else
136151
{
137152
optimizedImageFile.CopyTo(options.OutputFile, overwrite: true);
138153
}
139154

140-
stopwatch.Stop();
141-
context.LogMessage($"Success converted image. Cost {stopwatch.ElapsedMilliseconds}ms. OutputFile='{options.OutputFile}'");
155+
total.Stop();
156+
context.LogMessage($"Success converted image. Cost {total.ElapsedMilliseconds}ms. OutputFile='{options.OutputFile}'");
142157

143158
return ErrorCode.Success;
144159
}

0 commit comments

Comments
 (0)