|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Apache License, Version 2.0. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using BenchmarkDotNet.Attributes; |
| 7 | +using SixLabors.ImageSharp.Formats.Jpeg; |
| 8 | +using SixLabors.ImageSharp.PixelFormats; |
| 9 | +using SixLabors.ImageSharp.Tests; |
| 10 | + |
| 11 | +namespace SixLabors.ImageSharp.Benchmarks.Codecs.Jpeg |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Benchmark for all available encoding features of the Jpeg file type. |
| 15 | + /// </summary> |
| 16 | + /// <remarks> |
| 17 | + /// This benchmark does NOT compare ImageSharp to any other jpeg codecs. |
| 18 | + /// </remarks> |
| 19 | + public class EncodeJpegFeatures |
| 20 | + { |
| 21 | + // Big enough, 4:4:4 chroma sampling |
| 22 | + // No metadata |
| 23 | + private const string TestImage = TestImages.Jpeg.Baseline.Calliphora; |
| 24 | + |
| 25 | + public static IEnumerable<JpegColorType> ColorSpaceValues => |
| 26 | + new[] { JpegColorType.Luminance, JpegColorType.Rgb, JpegColorType.YCbCrRatio420, JpegColorType.YCbCrRatio444 }; |
| 27 | + |
| 28 | + [Params(75, 90, 100)] |
| 29 | + public int Quality; |
| 30 | + |
| 31 | + [ParamsSource(nameof(ColorSpaceValues), Priority = -100)] |
| 32 | + public JpegColorType TargetColorSpace; |
| 33 | + |
| 34 | + private Image<Rgb24> bmpCore; |
| 35 | + private JpegEncoder encoder; |
| 36 | + |
| 37 | + private MemoryStream destinationStream; |
| 38 | + |
| 39 | + [GlobalSetup] |
| 40 | + public void Setup() |
| 41 | + { |
| 42 | + using FileStream imageBinaryStream = File.OpenRead(Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, TestImage)); |
| 43 | + this.bmpCore = Image.Load<Rgb24>(imageBinaryStream); |
| 44 | + this.encoder = new JpegEncoder { Quality = this.Quality, ColorType = this.TargetColorSpace }; |
| 45 | + this.destinationStream = new MemoryStream(); |
| 46 | + } |
| 47 | + |
| 48 | + [GlobalCleanup] |
| 49 | + public void Cleanup() |
| 50 | + { |
| 51 | + this.bmpCore.Dispose(); |
| 52 | + this.bmpCore = null; |
| 53 | + |
| 54 | + this.destinationStream.Dispose(); |
| 55 | + this.destinationStream = null; |
| 56 | + } |
| 57 | + |
| 58 | + [Benchmark] |
| 59 | + public void Benchmark() |
| 60 | + { |
| 61 | + this.bmpCore.SaveAsJpeg(this.destinationStream, this.encoder); |
| 62 | + this.destinationStream.Seek(0, SeekOrigin.Begin); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/* |
| 68 | +BenchmarkDotNet=v0.13.0, OS=Windows 10.0.19044 |
| 69 | +Intel Core i7-6700K CPU 4.00GHz (Skylake), 1 CPU, 8 logical and 4 physical cores |
| 70 | +.NET SDK=6.0.100-preview.3.21202.5 |
| 71 | + [Host] : .NET Core 3.1.21 (CoreCLR 4.700.21.51404, CoreFX 4.700.21.51508), X64 RyuJIT |
| 72 | + DefaultJob : .NET Core 3.1.21 (CoreCLR 4.700.21.51404, CoreFX 4.700.21.51508), X64 RyuJIT |
| 73 | +
|
| 74 | +
|
| 75 | +| Method | TargetColorSpace | Quality | Mean | Error | StdDev | |
| 76 | +|---------- |----------------- |-------- |----------:|----------:|----------:| |
| 77 | +| Benchmark | Luminance | 75 | 7.055 ms | 0.1411 ms | 0.3297 ms | |
| 78 | +| Benchmark | Rgb | 75 | 12.139 ms | 0.0645 ms | 0.0538 ms | |
| 79 | +| Benchmark | YCbCrRatio420 | 75 | 6.463 ms | 0.0282 ms | 0.0235 ms | |
| 80 | +| Benchmark | YCbCrRatio444 | 75 | 8.616 ms | 0.0422 ms | 0.0374 ms | |
| 81 | +| Benchmark | Luminance | 90 | 7.011 ms | 0.0361 ms | 0.0301 ms | |
| 82 | +| Benchmark | Rgb | 90 | 13.119 ms | 0.0947 ms | 0.0886 ms | |
| 83 | +| Benchmark | YCbCrRatio420 | 90 | 6.786 ms | 0.0328 ms | 0.0274 ms | |
| 84 | +| Benchmark | YCbCrRatio444 | 90 | 8.672 ms | 0.0772 ms | 0.0722 ms | |
| 85 | +| Benchmark | Luminance | 100 | 9.554 ms | 0.1211 ms | 0.1012 ms | |
| 86 | +| Benchmark | Rgb | 100 | 19.475 ms | 0.1080 ms | 0.0958 ms | |
| 87 | +| Benchmark | YCbCrRatio420 | 100 | 10.146 ms | 0.0585 ms | 0.0519 ms | |
| 88 | +| Benchmark | YCbCrRatio444 | 100 | 15.317 ms | 0.0709 ms | 0.0592 ms | |
| 89 | +*/ |
0 commit comments