|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Apache License, Version 2.0. |
| 3 | + |
| 4 | +using System.Drawing.Imaging; |
| 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 | +using SkiaSharp; |
| 11 | +using SDImage = System.Drawing.Image; |
| 12 | + |
| 13 | +namespace SixLabors.ImageSharp.Benchmarks.Codecs.Jpeg |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// Benchmark for performance comparison between other codecs. |
| 17 | + /// </summary> |
| 18 | + /// <remarks> |
| 19 | + /// This benchmarks tests baseline 4:2:0 chroma sampling path. |
| 20 | + /// </remarks> |
| 21 | + public class EncodeJpegComparison |
| 22 | + { |
| 23 | + // Big enough, 4:4:4 chroma sampling |
| 24 | + private const string TestImage = TestImages.Jpeg.Baseline.Calliphora; |
| 25 | + |
| 26 | + // Change/add parameters for extra benchmarks |
| 27 | + [Params(75, 90, 100)] |
| 28 | + public int Quality; |
| 29 | + |
| 30 | + private MemoryStream destinationStream; |
| 31 | + |
| 32 | + // ImageSharp |
| 33 | + private Image<Rgba32> imageImageSharp; |
| 34 | + private JpegEncoder encoderImageSharp; |
| 35 | + |
| 36 | + // SkiaSharp |
| 37 | + private SKBitmap imageSkiaSharp; |
| 38 | + |
| 39 | + [GlobalSetup(Target = nameof(BenchmarkImageSharp))] |
| 40 | + public void SetupImageSharp() |
| 41 | + { |
| 42 | + using FileStream imageBinaryStream = File.OpenRead(Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, TestImage)); |
| 43 | + |
| 44 | + this.imageImageSharp = Image.Load<Rgba32>(imageBinaryStream); |
| 45 | + this.encoderImageSharp = new JpegEncoder { Quality = this.Quality, ColorType = JpegColorType.YCbCrRatio420 }; |
| 46 | + |
| 47 | + this.destinationStream = new MemoryStream(); |
| 48 | + } |
| 49 | + |
| 50 | + [GlobalCleanup(Target = nameof(BenchmarkImageSharp))] |
| 51 | + public void CleanupImageSharp() |
| 52 | + { |
| 53 | + this.imageImageSharp.Dispose(); |
| 54 | + this.imageImageSharp = null; |
| 55 | + |
| 56 | + this.destinationStream.Dispose(); |
| 57 | + this.destinationStream = null; |
| 58 | + } |
| 59 | + |
| 60 | + [Benchmark(Description = "ImageSharp")] |
| 61 | + public void BenchmarkImageSharp() |
| 62 | + { |
| 63 | + this.imageImageSharp.SaveAsJpeg(this.destinationStream, this.encoderImageSharp); |
| 64 | + this.destinationStream.Seek(0, SeekOrigin.Begin); |
| 65 | + } |
| 66 | + |
| 67 | + [GlobalSetup(Target = nameof(BenchmarkSkiaSharp))] |
| 68 | + public void SetupSkiaSharp() |
| 69 | + { |
| 70 | + using FileStream imageBinaryStream = File.OpenRead(Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, TestImage)); |
| 71 | + |
| 72 | + this.imageSkiaSharp = SKBitmap.Decode(imageBinaryStream); |
| 73 | + |
| 74 | + this.destinationStream = new MemoryStream(); |
| 75 | + } |
| 76 | + |
| 77 | + [GlobalCleanup(Target = nameof(BenchmarkSkiaSharp))] |
| 78 | + public void CleanupSkiaSharp() |
| 79 | + { |
| 80 | + this.imageSkiaSharp.Dispose(); |
| 81 | + this.imageSkiaSharp = null; |
| 82 | + |
| 83 | + this.destinationStream.Dispose(); |
| 84 | + this.destinationStream = null; |
| 85 | + } |
| 86 | + |
| 87 | + [Benchmark(Description = "SkiaSharp")] |
| 88 | + public void BenchmarkSkiaSharp() |
| 89 | + { |
| 90 | + this.imageSkiaSharp.Encode(SKEncodedImageFormat.Jpeg, this.Quality).SaveTo(this.destinationStream); |
| 91 | + this.destinationStream.Seek(0, SeekOrigin.Begin); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/* |
| 97 | +BenchmarkDotNet=v0.13.0, OS=Windows 10.0.19044 |
| 98 | +Intel Core i7-6700K CPU 4.00GHz (Skylake), 1 CPU, 8 logical and 4 physical cores |
| 99 | +.NET SDK=6.0.100-preview.3.21202.5 |
| 100 | + [Host] : .NET Core 3.1.21 (CoreCLR 4.700.21.51404, CoreFX 4.700.21.51508), X64 RyuJIT |
| 101 | + DefaultJob : .NET Core 3.1.21 (CoreCLR 4.700.21.51404, CoreFX 4.700.21.51508), X64 RyuJIT |
| 102 | +
|
| 103 | +
|
| 104 | +| Method | Quality | Mean | Error | StdDev | |
| 105 | +|----------- |-------- |----------:|----------:|----------:| |
| 106 | +| ImageSharp | 75 | 6.820 ms | 0.0374 ms | 0.0312 ms | |
| 107 | +| SkiaSharp | 75 | 16.417 ms | 0.3238 ms | 0.4747 ms | |
| 108 | +| ImageSharp | 90 | 7.849 ms | 0.1565 ms | 0.3126 ms | |
| 109 | +| SkiaSharp | 90 | 16.893 ms | 0.2200 ms | 0.2058 ms | |
| 110 | +| ImageSharp | 100 | 11.016 ms | 0.2087 ms | 0.1850 ms | |
| 111 | +| SkiaSharp | 100 | 20.410 ms | 0.2583 ms | 0.2290 ms | |
| 112 | +*/ |
0 commit comments