|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Six Labors Split License. |
| 3 | + |
| 4 | +using System.Diagnostics; |
| 5 | +using CommandLine; |
| 6 | +using CommandLine.Text; |
| 7 | +using SixLabors.ImageSharp; |
| 8 | +using SixLabors.ImageSharp.Drawing; |
| 9 | +using SixLabors.ImageSharp.Drawing.Processing; |
| 10 | +using SixLabors.ImageSharp.Drawing.Tests; |
| 11 | +using SixLabors.ImageSharp.PixelFormats; |
| 12 | +using SixLabors.ImageSharp.Processing; |
| 13 | + |
| 14 | +public sealed class DrawingThroughputBenchmark |
| 15 | +{ |
| 16 | + private readonly CommandLineOptions options; |
| 17 | + private readonly Configuration configuration; |
| 18 | + private readonly List<(IPath Path, SolidBrush Fill, SolidPen Stroke)> elements; |
| 19 | + private ulong totalProcessedPixels; |
| 20 | + |
| 21 | + private DrawingThroughputBenchmark(CommandLineOptions options) |
| 22 | + { |
| 23 | + this.options = options; |
| 24 | + this.configuration = Configuration.Default.Clone(); |
| 25 | + this.configuration.MaxDegreeOfParallelism = options.ProcessorParallelism > 0 |
| 26 | + ? options.ProcessorParallelism |
| 27 | + : Environment.ProcessorCount; |
| 28 | + List<SvgBenchmarkHelper.SvgElement> elements = SvgBenchmarkHelper.ParseSvg( |
| 29 | + TestFile.GetInputFileFullPath(TestImages.Svg.GhostscriptTiger)); |
| 30 | + float size = (options.Width + options.Height) * 0.5f; |
| 31 | + this.elements = SvgBenchmarkHelper.BuildImageSharpElements(elements, size / 200f); |
| 32 | + } |
| 33 | + |
| 34 | + public static Task RunAsync(string[] args) |
| 35 | + { |
| 36 | + CommandLineOptions? options = null; |
| 37 | + if (args.Length > 0) |
| 38 | + { |
| 39 | + options = CommandLineOptions.Parse(args); |
| 40 | + if (options == null) |
| 41 | + { |
| 42 | + return Task.CompletedTask; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + options ??= new CommandLineOptions(); |
| 47 | + return new DrawingThroughputBenchmark(options.Normalize()) |
| 48 | + .RunAsync(); |
| 49 | + } |
| 50 | + |
| 51 | + private async Task RunAsync() |
| 52 | + { |
| 53 | + SemaphoreSlim semaphore = new(this.options.ConcurrentRequests); |
| 54 | + Console.WriteLine(this.options.Method); |
| 55 | + Func<int> action = this.options.Method switch |
| 56 | + { |
| 57 | + Method.Tiger => this.Tiger, |
| 58 | + _ => throw new NotImplementedException(), |
| 59 | + }; |
| 60 | + |
| 61 | + Console.WriteLine(this.options); |
| 62 | + Console.WriteLine($"Running {this.options.Method} for {this.options.Seconds} seconds ..."); |
| 63 | + TimeSpan runFor = TimeSpan.FromSeconds(this.options.Seconds); |
| 64 | + |
| 65 | + // inFlight starts at 1 to represent the dispatch loop itself |
| 66 | + int inFlight = 1; |
| 67 | + TaskCompletionSource drainTcs = new(TaskCreationOptions.RunContinuationsAsynchronously); |
| 68 | + |
| 69 | + Stopwatch stopwatch = Stopwatch.StartNew(); |
| 70 | + while (stopwatch.Elapsed < runFor && !drainTcs.Task.IsCompleted) |
| 71 | + { |
| 72 | + await semaphore.WaitAsync(); |
| 73 | + |
| 74 | + if (stopwatch.Elapsed >= runFor) |
| 75 | + { |
| 76 | + semaphore.Release(); |
| 77 | + break; |
| 78 | + } |
| 79 | + |
| 80 | + Interlocked.Increment(ref inFlight); |
| 81 | + |
| 82 | + _ = ProcessImage(); |
| 83 | + |
| 84 | + async Task ProcessImage() |
| 85 | + { |
| 86 | + try |
| 87 | + { |
| 88 | + if (stopwatch.Elapsed >= runFor || drainTcs.Task.IsCompleted) |
| 89 | + { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + await Task.Yield(); // "emulate IO", i.e., make sure the processing code is async |
| 94 | + ulong pixels = (ulong)action(); |
| 95 | + Interlocked.Add(ref this.totalProcessedPixels, pixels); |
| 96 | + } |
| 97 | + catch (Exception ex) |
| 98 | + { |
| 99 | + Console.WriteLine(ex); |
| 100 | + drainTcs.TrySetException(ex); |
| 101 | + } |
| 102 | + finally |
| 103 | + { |
| 104 | + semaphore.Release(); |
| 105 | + if (Interlocked.Decrement(ref inFlight) == 0) |
| 106 | + { |
| 107 | + drainTcs.TrySetResult(); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Release the dispatch loop's own count; if no work is in flight, this completes immediately |
| 114 | + if (Interlocked.Decrement(ref inFlight) == 0) |
| 115 | + { |
| 116 | + drainTcs.TrySetResult(); |
| 117 | + } |
| 118 | + |
| 119 | + await drainTcs.Task; |
| 120 | + stopwatch.Stop(); |
| 121 | + |
| 122 | + double totalMegaPixels = this.totalProcessedPixels / 1_000_000.0; |
| 123 | + double totalSeconds = stopwatch.ElapsedMilliseconds / 1000.0; |
| 124 | + double megapixelsPerSec = totalMegaPixels / totalSeconds; |
| 125 | + Console.WriteLine($"TotalSeconds: {totalSeconds:F2}"); |
| 126 | + Console.WriteLine($"MegaPixelsPerSec: {megapixelsPerSec:F2}"); |
| 127 | + } |
| 128 | + |
| 129 | + private int Tiger() |
| 130 | + { |
| 131 | + using Image<Rgba32> image = new(this.options.Width, this.options.Height); |
| 132 | + image.Mutate(this.configuration, c => c.ProcessWithCanvas(canvas => |
| 133 | + { |
| 134 | + foreach ((IPath path, SolidBrush fill, SolidPen stroke) in this.elements) |
| 135 | + { |
| 136 | + if (fill is not null) |
| 137 | + { |
| 138 | + canvas.Fill(fill, path); |
| 139 | + } |
| 140 | + |
| 141 | + if (stroke is not null) |
| 142 | + { |
| 143 | + canvas.Draw(stroke, path); |
| 144 | + } |
| 145 | + } |
| 146 | + })); |
| 147 | + return image.Width * image.Height; |
| 148 | + } |
| 149 | + |
| 150 | + private enum Method |
| 151 | + { |
| 152 | + Tiger, |
| 153 | + } |
| 154 | + |
| 155 | + private sealed class CommandLineOptions |
| 156 | + { |
| 157 | + private const int DefaultSize = 2000; |
| 158 | + |
| 159 | + [Option('m', "method", Required = false, Default = Method.Tiger, HelpText = "The stress test method to run (Edges, Crop)")] |
| 160 | + public Method Method { get; set; } = Method.Tiger; |
| 161 | + |
| 162 | + [Option('p', "drawing-parallelism", Required = false, Default = -1, HelpText = "Level of parallelism for the image processor")] |
| 163 | + public int ProcessorParallelism { get; set; } = -1; |
| 164 | + |
| 165 | + [Option('c', "concurrent-requests", Required = false, Default = -1, HelpText = "Number of concurrent in-flight requests")] |
| 166 | + public int ConcurrentRequests { get; set; } = -1; |
| 167 | + |
| 168 | + [Option('w', "width", Required = false, Default = DefaultSize, HelpText = "Width of the test image")] |
| 169 | + public int Width { get; set; } = DefaultSize; |
| 170 | + |
| 171 | + [Option('h', "height", Required = false, Default = DefaultSize, HelpText = "Height of the test image")] |
| 172 | + public int Height { get; set; } = DefaultSize; |
| 173 | + |
| 174 | + [Option('s', "seconds", Required = false, Default = 5, HelpText = "Duration of the stress test in seconds")] |
| 175 | + public int Seconds { get; set; } = 5; |
| 176 | + |
| 177 | + public override string ToString() => string.Join( |
| 178 | + "|", |
| 179 | + $"method: {this.Method}", |
| 180 | + $"processor-parallelism: {this.ProcessorParallelism}", |
| 181 | + $"concurrent-requests: {this.ConcurrentRequests}", |
| 182 | + $"width: {this.Width}", |
| 183 | + $"height: {this.Height}", |
| 184 | + $"seconds: {this.Seconds}"); |
| 185 | + |
| 186 | + public CommandLineOptions Normalize() |
| 187 | + { |
| 188 | + if (this.ProcessorParallelism < 0) |
| 189 | + { |
| 190 | + this.ProcessorParallelism = Environment.ProcessorCount; |
| 191 | + } |
| 192 | + |
| 193 | + if (this.ConcurrentRequests < 0) |
| 194 | + { |
| 195 | + this.ConcurrentRequests = Environment.ProcessorCount; |
| 196 | + } |
| 197 | + |
| 198 | + return this; |
| 199 | + } |
| 200 | + |
| 201 | + public static CommandLineOptions? Parse(string[] args) |
| 202 | + { |
| 203 | + CommandLineOptions? result = null; |
| 204 | + using Parser parser = new(settings => settings.CaseInsensitiveEnumValues = true); |
| 205 | + ParserResult<CommandLineOptions> parserResult = parser.ParseArguments<CommandLineOptions>(args).WithParsed(o => |
| 206 | + { |
| 207 | + result = o; |
| 208 | + }); |
| 209 | + |
| 210 | + if (result == null) |
| 211 | + { |
| 212 | + Console.WriteLine(HelpText.RenderUsageText(parserResult)); |
| 213 | + } |
| 214 | + |
| 215 | + return result; |
| 216 | + } |
| 217 | + } |
| 218 | +} |
0 commit comments