Browse Source

code cleanup in MultiImageBenchmarkBase

af/merge-core
Anton Firszov 10 years ago
parent
commit
9022836a48
  1. 4
      tests/ImageSharp.Benchmarks/Image/DecodeJpegMultiple.cs
  2. 6
      tests/ImageSharp.Benchmarks/Image/EncodeJpegMultiple.cs
  3. 55
      tests/ImageSharp.Benchmarks/Image/MultiImageBenchmarkBase.cs

4
tests/ImageSharp.Benchmarks/Image/DecodeJpegMultiple.cs

@ -11,9 +11,9 @@ namespace ImageSharp.Benchmarks.Image
public class DecodeJpegMultiple : MultiImageBenchmarkBase public class DecodeJpegMultiple : MultiImageBenchmarkBase
{ {
protected override IEnumerable<string> InputImageSubfolders => new[] protected override IEnumerable<string> InputImageSubfoldersOrFiles => new[]
{ {
"Formats/Jpg/" "Jpg/"
}; };
protected override IEnumerable<string> FileFilters => new[] { "*.jpg" }; protected override IEnumerable<string> FileFilters => new[] { "*.jpg" };

6
tests/ImageSharp.Benchmarks/Image/EncodeJpegMultiple.cs

@ -14,10 +14,10 @@
public class EncodeJpegMultiple : MultiImageBenchmarkBase.WithImagesPreloaded public class EncodeJpegMultiple : MultiImageBenchmarkBase.WithImagesPreloaded
{ {
protected override IEnumerable<string> InputImageSubfolders => new[] protected override IEnumerable<string> InputImageSubfoldersOrFiles => new[]
{ {
"Formats/Bmp/", "Bmp/",
"Formats/Jpg/baseline" "Jpg/baseline"
}; };
protected override IEnumerable<string> FileFilters => new[] { "*.bmp", "*.jpg" }; protected override IEnumerable<string> FileFilters => new[] { "*.bmp", "*.jpg" };

55
tests/ImageSharp.Benchmarks/Image/MultiImageBenchmarkBase.cs

@ -18,39 +18,51 @@
protected Dictionary<string, Image> FileNamesToImageSharpImages = new Dictionary<string, Image>(); protected Dictionary<string, Image> FileNamesToImageSharpImages = new Dictionary<string, Image>();
protected Dictionary<string, System.Drawing.Bitmap> FileNamesToSystemDrawingImages = new Dictionary<string, System.Drawing.Bitmap>(); protected Dictionary<string, System.Drawing.Bitmap> FileNamesToSystemDrawingImages = new Dictionary<string, System.Drawing.Bitmap>();
public enum TestingMode /// <summary>
/// The values of this enum separate input files into categories
/// </summary>
public enum InputImageCategory
{ {
All, AllImages,
SmallImagesOnly, SmallImagesOnly,
LargeImagesOnly LargeImagesOnly
} }
[Params(TestingMode.All, TestingMode.SmallImagesOnly, TestingMode.LargeImagesOnly)] [Params(InputImageCategory.AllImages, InputImageCategory.SmallImagesOnly, InputImageCategory.LargeImagesOnly)]
public TestingMode Mode { get; set; } public InputImageCategory InputCategory { get; set; }
protected virtual string BaseFolder => "../ImageSharp.Tests/TestImages/"; protected virtual string BaseFolder => "../ImageSharp.Tests/TestImages/Formats/";
protected abstract IEnumerable<string> FileFilters { get; } protected virtual IEnumerable<string> FileFilters => new[] { "*.*" };
protected IEnumerable<string> FilterWords => new string[] { }; /// <summary>
/// Gets the file names containing these strings are substrings are not processed by the benchmark.
protected virtual IEnumerable<string> Folders => this.InputImageSubfolders.Select(f => Path.Combine(this.BaseFolder, f)); /// </summary>
protected IEnumerable<string> ExcludeSubstringsInFileNames => new string[] { };
/// <summary>
/// Enumerates folders containing files OR files to be processed by the benchmark.
/// </summary>
protected IEnumerable<string> AllFoldersOrFiles => this.InputImageSubfoldersOrFiles.Select(f => Path.Combine(this.BaseFolder, f));
/// <summary>
/// The images sized above this threshold will be included in
/// </summary>
protected virtual int LargeImageThresholdInBytes => 100000; protected virtual int LargeImageThresholdInBytes => 100000;
protected IEnumerable<KeyValuePair<string, T>> EnumeratePairsByBenchmarkSettings<T>( protected IEnumerable<KeyValuePair<string, T>> EnumeratePairsByBenchmarkSettings<T>(
Dictionary<string, T> input, Dictionary<string, T> input,
Predicate<T> checkIfSmall) Predicate<T> checkIfSmall)
{ {
switch (this.Mode) switch (this.InputCategory)
{ {
case TestingMode.All: case InputImageCategory.AllImages:
return input; return input;
case TestingMode.SmallImagesOnly: case InputImageCategory.SmallImagesOnly:
return input.Where(kv => checkIfSmall(kv.Value)); return input.Where(kv => checkIfSmall(kv.Value));
case TestingMode.LargeImagesOnly: case InputImageCategory.LargeImagesOnly:
return input.Where(kv => !checkIfSmall(kv.Value)); return input.Where(kv => !checkIfSmall(kv.Value));
default: default:
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
@ -63,24 +75,25 @@
this.FileNamesToBytes, this.FileNamesToBytes,
arr => arr.Length < this.LargeImageThresholdInBytes); arr => arr.Length < this.LargeImageThresholdInBytes);
protected abstract IEnumerable<string> InputImageSubfolders { get; } protected abstract IEnumerable<string> InputImageSubfoldersOrFiles { get; }
[Setup] [Setup]
public void ReadImages() public void ReadImages()
{ {
//Console.WriteLine("Vector.IsHardwareAccelerated: " + Vector.IsHardwareAccelerated); // Console.WriteLine("Vector.IsHardwareAccelerated: " + Vector.IsHardwareAccelerated);
this.ReadImagesImpl(); this.ReadImagesImpl();
} }
protected virtual void ReadImagesImpl() protected virtual void ReadImagesImpl()
{ {
foreach (string folder in this.Folders) foreach (string folder in this.AllFoldersOrFiles)
{ {
var allFiles = var allFiles =
this.FileFilters.SelectMany( this.FileFilters.SelectMany(
f => f =>
Directory.EnumerateFiles(folder, f, SearchOption.AllDirectories) Directory.EnumerateFiles(folder, f, SearchOption.AllDirectories)
.Where(fn => !this.FilterWords.Any(w => fn.ToLower().Contains(w)))).ToArray(); .Where(fn => !this.ExcludeSubstringsInFileNames.Any(w => fn.ToLower().Contains(w)))).ToArray();
foreach (var fn in allFiles) foreach (var fn in allFiles)
{ {
this.FileNamesToBytes[fn] = File.ReadAllBytes(fn); this.FileNamesToBytes[fn] = File.ReadAllBytes(fn);

Loading…
Cancel
Save