|
| 1 | +using BenchmarkDotNet.Attributes; |
| 2 | +using SkiaSharp; |
| 3 | +using System; |
| 4 | +using System.IO; |
| 5 | +using System.Reflection; |
| 6 | +using TyKonKet.BarcodeGenerator.Fonts; |
| 7 | + |
| 8 | +namespace TyKonKet.BarcodeGenerator.CB.Benchmarks |
| 9 | +{ |
| 10 | + [Config(typeof(BenchmarkConfig))] |
| 11 | + public class Isbn13Encoder |
| 12 | + { |
| 13 | + private const string BARCODE = "9781234567897"; |
| 14 | + |
| 15 | + private const int QUALITY = 75; |
| 16 | + |
| 17 | + private static readonly SKEncodedImageFormat sKEncodedImageFormat = SKEncodedImageFormat.Jpeg; |
| 18 | + |
| 19 | + private static readonly string outputFilePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "out", "barcode.jpg"); |
| 20 | + |
| 21 | + private static readonly BarcodeTypes barcodeType = BarcodeTypes.Isbn13; |
| 22 | + |
| 23 | + private const int HEIGHT = 20; |
| 24 | + |
| 25 | + private const int MARGINS = 1; |
| 26 | + |
| 27 | + private const int SCALING = 5; |
| 28 | + |
| 29 | + private static readonly SKColor backgroundColor = SKColors.White; |
| 30 | + |
| 31 | + private static readonly SKColor foregroundColor = SKColors.Black; |
| 32 | + |
| 33 | + private Barcode encodeWithoutTextEncoder; |
| 34 | + |
| 35 | + private Barcode encodeWithTextEncoder; |
| 36 | + |
| 37 | + private Barcode exportEncoder; |
| 38 | + |
| 39 | + [Benchmark] |
| 40 | + public Barcode SimpleInstance() |
| 41 | + { |
| 42 | + return new Barcode(options => |
| 43 | + { |
| 44 | + options.Type = barcodeType; |
| 45 | + options.Height = HEIGHT; |
| 46 | + options.Margins = MARGINS; |
| 47 | + options.Scaling = SCALING; |
| 48 | + options.BackgroundColor = backgroundColor; |
| 49 | + options.ForegroundColor = foregroundColor; |
| 50 | + options.RenderText = false; |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + [Benchmark] |
| 55 | + public Barcode AdvancedInstance() |
| 56 | + { |
| 57 | + return new Barcode(options => |
| 58 | + { |
| 59 | + options.Type = barcodeType; |
| 60 | + options.Height = HEIGHT; |
| 61 | + options.Margins = MARGINS; |
| 62 | + options.Scaling = SCALING; |
| 63 | + options.BackgroundColor = backgroundColor; |
| 64 | + options.ForegroundColor = foregroundColor; |
| 65 | + options.RenderText = true; |
| 66 | + options.UseTypeface(FontFamilies.DejaVuSerif, SKFontStyle.Normal); |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + #region EncodingWithoutText |
| 71 | + |
| 72 | + [GlobalSetup(Target = nameof(EncodingWithoutText))] |
| 73 | + public void EncodingWithoutTextSetup() |
| 74 | + { |
| 75 | + Console.WriteLine("Setting up the barcode encoder without text..."); |
| 76 | + Console.WriteLine(); |
| 77 | + |
| 78 | + encodeWithoutTextEncoder = new Barcode(options => |
| 79 | + { |
| 80 | + options.Type = barcodeType; |
| 81 | + options.Height = HEIGHT; |
| 82 | + options.Margins = MARGINS; |
| 83 | + options.Scaling = SCALING; |
| 84 | + options.BackgroundColor = backgroundColor; |
| 85 | + options.ForegroundColor = foregroundColor; |
| 86 | + options.RenderText = false; |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + [Benchmark] |
| 91 | + public void EncodingWithoutText() |
| 92 | + { |
| 93 | + encodeWithoutTextEncoder.Encode(BARCODE); |
| 94 | + } |
| 95 | + |
| 96 | + [GlobalCleanup(Target = nameof(EncodingWithoutText))] |
| 97 | + public void EncodingWithoutTextCleanup() |
| 98 | + { |
| 99 | + Console.WriteLine("Cleaning up the barcode encoder without text..."); |
| 100 | + Console.WriteLine(); |
| 101 | + |
| 102 | + encodeWithoutTextEncoder?.Dispose(); |
| 103 | + } |
| 104 | + |
| 105 | + #endregion |
| 106 | + |
| 107 | + #region EncodingWithText |
| 108 | + |
| 109 | + [GlobalSetup(Target = nameof(EncodingWithText))] |
| 110 | + public void EncodingWithTextSetup() |
| 111 | + { |
| 112 | + Console.WriteLine("Setting up the barcode encoder with text..."); |
| 113 | + Console.WriteLine(); |
| 114 | + |
| 115 | + encodeWithTextEncoder = new Barcode(options => |
| 116 | + { |
| 117 | + options.Type = barcodeType; |
| 118 | + options.Height = HEIGHT; |
| 119 | + options.Margins = MARGINS; |
| 120 | + options.Scaling = SCALING; |
| 121 | + options.BackgroundColor = backgroundColor; |
| 122 | + options.ForegroundColor = foregroundColor; |
| 123 | + options.RenderText = true; |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + [Benchmark] |
| 128 | + public void EncodingWithText() |
| 129 | + { |
| 130 | + encodeWithTextEncoder.Encode(BARCODE); |
| 131 | + } |
| 132 | + |
| 133 | + [GlobalCleanup(Target = nameof(EncodingWithText))] |
| 134 | + public void EncodingWithTextCleanup() |
| 135 | + { |
| 136 | + Console.WriteLine("Cleaning up the barcode encoder with text..."); |
| 137 | + Console.WriteLine(); |
| 138 | + |
| 139 | + encodeWithTextEncoder?.Dispose(); |
| 140 | + } |
| 141 | + |
| 142 | + #endregion |
| 143 | + |
| 144 | + #region ExportToFile |
| 145 | + |
| 146 | + [GlobalSetup(Target = nameof(ExportToFile))] |
| 147 | + public void ExportToFileSetup() |
| 148 | + { |
| 149 | + Console.WriteLine("Setting up the barcode export to file..."); |
| 150 | + Console.WriteLine(); |
| 151 | + |
| 152 | + exportEncoder = new Barcode(options => |
| 153 | + { |
| 154 | + options.Type = barcodeType; |
| 155 | + options.Height = HEIGHT; |
| 156 | + options.Margins = MARGINS; |
| 157 | + options.Scaling = SCALING; |
| 158 | + options.BackgroundColor = backgroundColor; |
| 159 | + options.ForegroundColor = foregroundColor; |
| 160 | + options.RenderText = false; |
| 161 | + }); |
| 162 | + exportEncoder.Encode(BARCODE); |
| 163 | + } |
| 164 | + |
| 165 | + [Benchmark] |
| 166 | + public void ExportToFile() |
| 167 | + { |
| 168 | + exportEncoder.Export(outputFilePath, sKEncodedImageFormat, QUALITY); |
| 169 | + } |
| 170 | + |
| 171 | + [GlobalCleanup(Target = nameof(ExportToFile))] |
| 172 | + public void ExportToFileCleanup() |
| 173 | + { |
| 174 | + Console.WriteLine("Cleaning up the barcode export to file..."); |
| 175 | + Console.WriteLine(); |
| 176 | + |
| 177 | + exportEncoder?.Dispose(); |
| 178 | + } |
| 179 | + |
| 180 | + #endregion |
| 181 | + } |
| 182 | +} |
0 commit comments