-
-
Notifications
You must be signed in to change notification settings - Fork 887
Improved jpeg encoding benchmarks #2051
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
aaac6d4
Removed obsolete benchmark
053c131
Added ImageSharp-only jpeg encoding benchmark
869c025
Added cross-codec jpeg encoding benchmark
8481a3a
Merge branch 'main' into dp/jpeg-benchmark
JimBobSquarePants 1ae157d
Merge branch 'main' into dp/jpeg-benchmark
JimBobSquarePants File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
112 changes: 112 additions & 0 deletions
112
tests/ImageSharp.Benchmarks/Codecs/Jpeg/EncodeJpegComparison.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
89
tests/ImageSharp.Benchmarks/Codecs/Jpeg/EncodeJpegFeatures.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | | ||
| */ | ||
34 changes: 0 additions & 34 deletions
34
tests/ImageSharp.Benchmarks/Codecs/Jpeg/EncodeJpegMultiple.cs
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.