Skip to content

Commit

Permalink
Remove VerifyImage from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mjebrahimi committed Aug 15, 2024
1 parent 751b9fd commit 2f7b193
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 51 deletions.
28 changes: 14 additions & 14 deletions src/Utilities/Html/HtmlHelper.Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace BenchmarkDotNetVisualizer.Utilities;
/// </summary>
public static partial class HtmlHelper
{
private static readonly BrowserFetcher browserFetcher = new();
private static readonly SemaphoreSlim browserDownloadSync = new(1, 1);
private static readonly SemaphoreSlim consoleProgressSync = new(1, 1);
private static readonly BrowserFetcher _browserFetcher = new();
private static readonly SemaphoreSlim _browserDownloadSync = new(1, 1);
private static readonly SemaphoreSlim _consoleProgressSync = new(1, 1);

/// <summary>
/// Gets or sets the default browser
Expand All @@ -21,11 +21,11 @@ public static partial class HtmlHelper
#pragma warning disable S3963 // "static" fields should be initialized inline
static HtmlHelper()
{
var installedBrowsers = browserFetcher.GetInstalledBrowsers().ToList();
var defaultBrowser = installedBrowsers.Find(p => p.Browser == browserFetcher.Browser && p.Platform == browserFetcher.Platform);
var installedBrowsers = _browserFetcher.GetInstalledBrowsers().ToList();
var defaultBrowser = installedBrowsers.Find(p => p.Browser == _browserFetcher.Browser && p.Platform == _browserFetcher.Platform);
if (defaultBrowser is not null)
{
var executablePath = browserFetcher.GetExecutablePath(defaultBrowser.BuildId);
var executablePath = _browserFetcher.GetExecutablePath(defaultBrowser.BuildId);
DefaultBrowser = new Browser(defaultBrowser.Browser, executablePath);
}
}
Expand Down Expand Up @@ -106,33 +106,33 @@ public static async Task EnsureBrowserDownloadedAsync(bool silent = false)
{
try
{
await browserDownloadSync.WaitAsync();
await _browserDownloadSync.WaitAsync();

if (DefaultBrowser is not null)
return;

var installedBrowser = await browserFetcher.DownloadAsync();
var executablePath = browserFetcher.GetExecutablePath(installedBrowser.BuildId);
var installedBrowser = await _browserFetcher.DownloadAsync();
var executablePath = _browserFetcher.GetExecutablePath(installedBrowser.BuildId);
DefaultBrowser = new Browser(installedBrowser.Browser, executablePath);
}
finally
{
browserDownloadSync.Release();
_browserDownloadSync.Release();
}
}));

var isProgressing = await consoleProgressSync.IsLockAlreadyAcquiredAsync();
var isProgressing = await _consoleProgressSync.IsLockAlreadyAcquiredAsync();
if (silent is false && isProgressing is false)
{
tasks.Add(Task.Run(async () =>
{
try
{
await consoleProgressSync.WaitAsync();
await _consoleProgressSync.WaitAsync();

await Task.Delay(1000);
var index = 0;
var isDownloading = await browserDownloadSync.IsLockAlreadyAcquiredAsync();
var isDownloading = await _browserDownloadSync.IsLockAlreadyAcquiredAsync();
while (isDownloading)
{
Console.Write($"Browser is downloading, please wait{new string('.', index + 1),-5}");
Expand All @@ -145,7 +145,7 @@ public static async Task EnsureBrowserDownloadedAsync(bool silent = false)
}
finally
{
consoleProgressSync.Release();
_consoleProgressSync.Release();
}
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public async Task Benchmark_Initialize_Mean()
Theme = Theme.Dark
};

var (htmlPath, imgPath) = await JoinReportsAndSaveAsHtmlAndImageAsync(benchmarkInfo, options);
var (htmlPath, _) = await JoinReportsAndSaveAsHtmlAndImageAsync(benchmarkInfo, options);

await VerifyHtmlAndImage(htmlPath, imgPath);
await VerifyHtml(htmlPath);
}

[Fact]
Expand All @@ -49,9 +49,9 @@ public async Task Benchmark_Initialize_Allocated()
Theme = Theme.Dark
};

var (htmlPath, imgPath) = await JoinReportsAndSaveAsHtmlAndImageAsync(benchmarkInfo, options);
var (htmlPath, _) = await JoinReportsAndSaveAsHtmlAndImageAsync(benchmarkInfo, options);

await VerifyHtmlAndImage(htmlPath, imgPath);
await VerifyHtml(htmlPath);
}

[Fact]
Expand All @@ -77,9 +77,9 @@ public async Task Benchmark_SearchContains_Mean()
Theme = Theme.Dark
};

var (htmlPath, imgPath) = await JoinReportsAndSaveAsHtmlAndImageAsync(benchmarkInfo, options);
var (htmlPath, _) = await JoinReportsAndSaveAsHtmlAndImageAsync(benchmarkInfo, options);

await VerifyHtmlAndImage(htmlPath, imgPath);
await VerifyHtml(htmlPath);
}

[Fact]
Expand All @@ -105,9 +105,9 @@ public async Task Benchmark_SearchContains_Allocated()
Theme = Theme.Dark
};

var (htmlPath, imgPath) = await JoinReportsAndSaveAsHtmlAndImageAsync(benchmarkInfo, options);
var (htmlPath, _) = await JoinReportsAndSaveAsHtmlAndImageAsync(benchmarkInfo, options);

await VerifyHtmlAndImage(htmlPath, imgPath);
await VerifyHtml(htmlPath);
}

[Fact]
Expand All @@ -132,9 +132,9 @@ public async Task Benchmark_SearchTryGetValue_Mean()
HtmlWrapMode = HtmlDocumentWrapMode.RichDataTables,
};

var (htmlPath, imgPath) = await JoinReportsAndSaveAsHtmlAndImageAsync(benchmarkInfo, options);
var (htmlPath, _) = await JoinReportsAndSaveAsHtmlAndImageAsync(benchmarkInfo, options);

await VerifyHtmlAndImage(htmlPath, imgPath);
await VerifyHtml(htmlPath);
}

[Fact]
Expand All @@ -159,9 +159,9 @@ public async Task Benchmark_SearchTryGetValue_Allocated()
HtmlWrapMode = HtmlDocumentWrapMode.RichDataTables,
};

var (htmlPath, imgPath) = await JoinReportsAndSaveAsHtmlAndImageAsync(benchmarkInfo, options);
var (htmlPath, _) = await JoinReportsAndSaveAsHtmlAndImageAsync(benchmarkInfo, options);

await VerifyHtmlAndImage(htmlPath, imgPath);
await VerifyHtml(htmlPath);
}

#region Utils
Expand Down
19 changes: 4 additions & 15 deletions test/BenchmarkDotNetVisualizer.Tests/ImageComparer.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;

namespace BenchmarkDotNetVisualizer.Tests;

internal static class ImageComparer
{
public static async Task<bool> IsEqualAsync(string imagePath1, string imagePath2, bool resizeToSameSize = false)
public static async Task<bool> IsEqualAsync(string imagePath1, string imagePath2)
{
using var image1 = await Image.LoadAsync<RgbaVector>(imagePath1);
using var image2 = await Image.LoadAsync<RgbaVector>(imagePath2);

if (resizeToSameSize)
{
image2.Mutate(x => x.Resize(image1.Width, image1.Height));
}

if (image1.Width != image2.Width || image1.Height != image2.Height)
{
Console.WriteLine("Images must be of the same dimensions.");
Expand All @@ -39,24 +33,19 @@ public static async Task<bool> IsEqualAsync(string imagePath1, string imagePath2
return true;
}

public static async Task<bool> IsSimilarAsync(string imagePath1, string imagePath2, bool resizeToSameSize = false, double ignoreDifferentColorPercentLessThan = 5.0, double thresholdDifferentPixlesPercent = 1.0)
public static async Task<bool> IsSimilarAsync(string imagePath1, string imagePath2, double ignoreDifferentColorPercentLessThan = 5.0, double thresholdDifferentPixlesPercent = 1.0)
{
var diff = await CalculateDiffAsync(imagePath1, imagePath2, resizeToSameSize, ignoreDifferentColorPercentLessThan);
var diff = await CalculateDiffAsync(imagePath1, imagePath2, ignoreDifferentColorPercentLessThan);
return diff < thresholdDifferentPixlesPercent;
}

public static async Task<double> CalculateDiffAsync(string imagePath1, string imagePath2, bool resizeToSameSize = false, double ignoreDifferentColorPercentLessThan = 10.0)
public static async Task<double> CalculateDiffAsync(string imagePath1, string imagePath2, double ignoreDifferentColorPercentLessThan = 10.0)
{
//TODO: Test with Rgba32 and PackedValue property

using var image1 = await Image.LoadAsync<RgbaVector>(imagePath1);
using var image2 = await Image.LoadAsync<RgbaVector>(imagePath2);

if (resizeToSameSize)
{
image2.Mutate(x => x.Resize(image1.Width, image1.Height));
}

if (image1.Width != image2.Width || image1.Height != image2.Height)
{
Console.WriteLine("Images must be of the same dimensions.");
Expand Down
16 changes: 8 additions & 8 deletions test/BenchmarkDotNetVisualizer.Tests/IterationBenchmarkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public async Task Joined_PivotBy_Runtime_Dark()
Theme = Theme.Dark
};

var (htmlPath, imgPath) = await JoinReportsAndSaveAsHtmlAndImageAsync(benchmarkInfo, options);
var (htmlPath, _) = await JoinReportsAndSaveAsHtmlAndImageAsync(benchmarkInfo, options);

await VerifyHtmlAndImage(htmlPath, imgPath);
await VerifyHtml(htmlPath);
}

[Fact]
Expand All @@ -43,9 +43,9 @@ public async Task Joined_PivotBy_Runtime_Light()
Theme = Theme.Light
};

var (htmlPath, imgPath) = await JoinReportsAndSaveAsHtmlAndImageAsync(benchmarkInfo, options);
var (htmlPath, _) = await JoinReportsAndSaveAsHtmlAndImageAsync(benchmarkInfo, options);

await VerifyHtmlAndImage(htmlPath, imgPath);
await VerifyHtml(htmlPath);
}

[Fact]
Expand All @@ -66,9 +66,9 @@ public async Task Joined_PivotBy_Method_Dark()
Theme = Theme.Dark
};

var (htmlPath, imgPath) = await JoinReportsAndSaveAsHtmlAndImageAsync(benchmarkInfo, options);
var (htmlPath, _) = await JoinReportsAndSaveAsHtmlAndImageAsync(benchmarkInfo, options);

await VerifyHtmlAndImage(htmlPath, imgPath);
await VerifyHtml(htmlPath);
}

[Fact]
Expand All @@ -89,8 +89,8 @@ public async Task Joined_PivotBy_Method_Light()
Theme = Theme.Light
};

var (htmlPath, imgPath) = await JoinReportsAndSaveAsHtmlAndImageAsync(benchmarkInfo, options);
var (htmlPath, _) = await JoinReportsAndSaveAsHtmlAndImageAsync(benchmarkInfo, options);

await VerifyHtmlAndImage(htmlPath, imgPath);
await VerifyHtml(htmlPath);
}
}
4 changes: 2 additions & 2 deletions test/BenchmarkDotNetVisualizer.Tests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ static TestBase()
public async Task VerifyHtmlAndImage(string htmlPath, string imgPath,
[CallerFilePath] string callerFilePath = null!, [CallerMemberName] string callerMethod = null!)
{
await VerifyImage(imgPath, callerFilePath, callerMethod);

await VerifyHtml(htmlPath);

await VerifyImage(imgPath, callerFilePath, callerMethod);
}

public async Task VerifyHtml(string htmlPath)
Expand Down

0 comments on commit 2f7b193

Please sign in to comment.