Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 0 additions & 161 deletions tests/ImageSharp.Benchmarks/Codecs/Jpeg/EncodeJpeg.cs

This file was deleted.

112 changes: 112 additions & 0 deletions tests/ImageSharp.Benchmarks/Codecs/Jpeg/EncodeJpegComparison.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

using System.Drawing.Imaging;
using System.IO;
using BenchmarkDotNet.Attributes;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Tests;
using SkiaSharp;
using SDImage = System.Drawing.Image;

namespace SixLabors.ImageSharp.Benchmarks.Codecs.Jpeg
{
/// <summary>
/// Benchmark for performance comparison between other codecs.
/// </summary>
/// <remarks>
/// This benchmarks tests baseline 4:2:0 chroma sampling path.
/// </remarks>
public class EncodeJpegComparison
{
// Big enough, 4:4:4 chroma sampling
private const string TestImage = TestImages.Jpeg.Baseline.Calliphora;

// Change/add parameters for extra benchmarks
[Params(75, 90, 100)]
public int Quality;

private MemoryStream destinationStream;

// ImageSharp
private Image<Rgba32> imageImageSharp;
private JpegEncoder encoderImageSharp;

// SkiaSharp
private SKBitmap imageSkiaSharp;

[GlobalSetup(Target = nameof(BenchmarkImageSharp))]
public void SetupImageSharp()
{
using FileStream imageBinaryStream = File.OpenRead(Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, TestImage));

this.imageImageSharp = Image.Load<Rgba32>(imageBinaryStream);
this.encoderImageSharp = new JpegEncoder { Quality = this.Quality, ColorType = JpegColorType.YCbCrRatio420 };

this.destinationStream = new MemoryStream();
}

[GlobalCleanup(Target = nameof(BenchmarkImageSharp))]
public void CleanupImageSharp()
{
this.imageImageSharp.Dispose();
this.imageImageSharp = null;

this.destinationStream.Dispose();
this.destinationStream = null;
}

[Benchmark(Description = "ImageSharp")]
public void BenchmarkImageSharp()
{
this.imageImageSharp.SaveAsJpeg(this.destinationStream, this.encoderImageSharp);
this.destinationStream.Seek(0, SeekOrigin.Begin);
}

[GlobalSetup(Target = nameof(BenchmarkSkiaSharp))]
public void SetupSkiaSharp()
{
using FileStream imageBinaryStream = File.OpenRead(Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, TestImage));

this.imageSkiaSharp = SKBitmap.Decode(imageBinaryStream);

this.destinationStream = new MemoryStream();
}

[GlobalCleanup(Target = nameof(BenchmarkSkiaSharp))]
public void CleanupSkiaSharp()
{
this.imageSkiaSharp.Dispose();
this.imageSkiaSharp = null;

this.destinationStream.Dispose();
this.destinationStream = null;
}

[Benchmark(Description = "SkiaSharp")]
public void BenchmarkSkiaSharp()
{
this.imageSkiaSharp.Encode(SKEncodedImageFormat.Jpeg, this.Quality).SaveTo(this.destinationStream);
this.destinationStream.Seek(0, SeekOrigin.Begin);
}
}
}

/*
BenchmarkDotNet=v0.13.0, OS=Windows 10.0.19044
Intel Core i7-6700K CPU 4.00GHz (Skylake), 1 CPU, 8 logical and 4 physical cores
.NET SDK=6.0.100-preview.3.21202.5
[Host] : .NET Core 3.1.21 (CoreCLR 4.700.21.51404, CoreFX 4.700.21.51508), X64 RyuJIT
DefaultJob : .NET Core 3.1.21 (CoreCLR 4.700.21.51404, CoreFX 4.700.21.51508), X64 RyuJIT


| Method | Quality | Mean | Error | StdDev |
|----------- |-------- |----------:|----------:|----------:|
| ImageSharp | 75 | 6.820 ms | 0.0374 ms | 0.0312 ms |
| SkiaSharp | 75 | 16.417 ms | 0.3238 ms | 0.4747 ms |
| ImageSharp | 90 | 7.849 ms | 0.1565 ms | 0.3126 ms |
| SkiaSharp | 90 | 16.893 ms | 0.2200 ms | 0.2058 ms |
| ImageSharp | 100 | 11.016 ms | 0.2087 ms | 0.1850 ms |
| SkiaSharp | 100 | 20.410 ms | 0.2583 ms | 0.2290 ms |
*/
89 changes: 89 additions & 0 deletions tests/ImageSharp.Benchmarks/Codecs/Jpeg/EncodeJpegFeatures.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

using System.Collections.Generic;
using System.IO;
using BenchmarkDotNet.Attributes;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Tests;

namespace SixLabors.ImageSharp.Benchmarks.Codecs.Jpeg
{
/// <summary>
/// Benchmark for all available encoding features of the Jpeg file type.
/// </summary>
/// <remarks>
/// This benchmark does NOT compare ImageSharp to any other jpeg codecs.
/// </remarks>
public class EncodeJpegFeatures
{
// Big enough, 4:4:4 chroma sampling
// No metadata
private const string TestImage = TestImages.Jpeg.Baseline.Calliphora;

public static IEnumerable<JpegColorType> ColorSpaceValues =>
new[] { JpegColorType.Luminance, JpegColorType.Rgb, JpegColorType.YCbCrRatio420, JpegColorType.YCbCrRatio444 };

[Params(75, 90, 100)]
public int Quality;

[ParamsSource(nameof(ColorSpaceValues), Priority = -100)]
public JpegColorType TargetColorSpace;

private Image<Rgb24> bmpCore;
private JpegEncoder encoder;

private MemoryStream destinationStream;

[GlobalSetup]
public void Setup()
{
using FileStream imageBinaryStream = File.OpenRead(Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, TestImage));
this.bmpCore = Image.Load<Rgb24>(imageBinaryStream);
this.encoder = new JpegEncoder { Quality = this.Quality, ColorType = this.TargetColorSpace };
this.destinationStream = new MemoryStream();
}

[GlobalCleanup]
public void Cleanup()
{
this.bmpCore.Dispose();
this.bmpCore = null;

this.destinationStream.Dispose();
this.destinationStream = null;
}

[Benchmark]
public void Benchmark()
{
this.bmpCore.SaveAsJpeg(this.destinationStream, this.encoder);
this.destinationStream.Seek(0, SeekOrigin.Begin);
}
}
}

/*
BenchmarkDotNet=v0.13.0, OS=Windows 10.0.19044
Intel Core i7-6700K CPU 4.00GHz (Skylake), 1 CPU, 8 logical and 4 physical cores
.NET SDK=6.0.100-preview.3.21202.5
[Host] : .NET Core 3.1.21 (CoreCLR 4.700.21.51404, CoreFX 4.700.21.51508), X64 RyuJIT
DefaultJob : .NET Core 3.1.21 (CoreCLR 4.700.21.51404, CoreFX 4.700.21.51508), X64 RyuJIT


| Method | TargetColorSpace | Quality | Mean | Error | StdDev |
|---------- |----------------- |-------- |----------:|----------:|----------:|
| Benchmark | Luminance | 75 | 7.055 ms | 0.1411 ms | 0.3297 ms |
| Benchmark | Rgb | 75 | 12.139 ms | 0.0645 ms | 0.0538 ms |
| Benchmark | YCbCrRatio420 | 75 | 6.463 ms | 0.0282 ms | 0.0235 ms |
| Benchmark | YCbCrRatio444 | 75 | 8.616 ms | 0.0422 ms | 0.0374 ms |
| Benchmark | Luminance | 90 | 7.011 ms | 0.0361 ms | 0.0301 ms |
| Benchmark | Rgb | 90 | 13.119 ms | 0.0947 ms | 0.0886 ms |
| Benchmark | YCbCrRatio420 | 90 | 6.786 ms | 0.0328 ms | 0.0274 ms |
| Benchmark | YCbCrRatio444 | 90 | 8.672 ms | 0.0772 ms | 0.0722 ms |
| Benchmark | Luminance | 100 | 9.554 ms | 0.1211 ms | 0.1012 ms |
| Benchmark | Rgb | 100 | 19.475 ms | 0.1080 ms | 0.0958 ms |
| Benchmark | YCbCrRatio420 | 100 | 10.146 ms | 0.0585 ms | 0.0519 ms |
| Benchmark | YCbCrRatio444 | 100 | 15.317 ms | 0.0709 ms | 0.0592 ms |
Comment on lines +75 to +88
Copy link
Contributor Author

@br3aker br3aker Mar 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Luminance encoding has same (or even worse) performance as YCbCr 4:2:0 because Rgb24 -> L8 conversion uses scalar code. It would be a lot faster if user decodes image into L8 pixel type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah theres definitely optimisation opportunity there.

*/
34 changes: 0 additions & 34 deletions tests/ImageSharp.Benchmarks/Codecs/Jpeg/EncodeJpegMultiple.cs

This file was deleted.