-
Notifications
You must be signed in to change notification settings - Fork 305
Expand file tree
/
Copy pathRichTextBenchmarks.cs
More file actions
281 lines (251 loc) · 11.9 KB
/
RichTextBenchmarks.cs
File metadata and controls
281 lines (251 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*************************************************************************************************
Required Notice: Copyright (C) EPPlus Software AB.
This software is licensed under PolyForm Noncommercial License 1.0.0
and may only be used for noncommercial purposes
https://polyformproject.org/licenses/noncommercial/1.0.0/
A commercial license to use this software can be purchased at https://epplussoftware.com
*************************************************************************************************
Date Author Change
*************************************************************************************************
01/23/2026 EPPlus Software AB Debug NA benchmarks
*************************************************************************************************/
using BenchmarkDotNet.Attributes;
using EPPlus.Fonts.OpenType;
using EPPlus.Fonts.OpenType.Integration;
using EPPlus.Fonts.OpenType.TextShaping;
using OfficeOpenXml.Interfaces.Drawing.Text;
using OfficeOpenXml.Interfaces.Fonts;
using System;
using System.Collections.Generic;
using System.IO;
namespace EPPlus.Fonts.Benchmarks
{
[MemoryDiagnoser]
[SimpleJob(warmupCount: 1, iterationCount: 2)]
public class DebugNABenchmarks
{
private const string LoremIpsum20Para = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla pulvinar interdum imperdiet. Praesent ut auctor urna. Phasellus sollicitudin quam vitae est convallis, eu mattis lorem efficitur. Mauris nulla libero, tincidunt id ipsum non, lobortis tristique mauris. Donec ut enim sed enim fermentum molestie vel quis odio. Morbi a fermentum massa, sit amet ultrices est. Aenean ante mi, fermentum nec rhoncus et, vulputate vel sapien. Donec tempus, leo quis luctus rhoncus, augue odio pharetra libero, ac blandit urna turpis sed diam. Vivamus augue purus, eleifend et justo facilisis, imperdiet rhoncus sem. Quisque accumsan pellentesque elit, eget finibus massa accumsan in.\r\n\r\nFusce eu accumsan enim. Cras pulvinar enim vel tellus lacinia, consectetur euismod tortor consectetur. Praesent tincidunt pretium eros, ac auctor magna luctus sed. Ut porta lectus quam, non ornare mauris lacinia sit amet. Nullam egestas dolor quis magna porttitor, ac iaculis nisi hendrerit. Proin at mollis lacus, in porttitor nunc. Aliquam erat volutpat. Sed vel egestas risus, at aliquam arcu. Vestibulum quis lobortis nulla. Etiam pellentesque auctor nulla, eget tincidunt felis rhoncus id.";
private TextLayoutEngine _layoutEngine;
private List<string> _fontFolders;
private const double MaxPointWidth = 39d;
private const float FontSize = 11f;
private const string FontFamily = "Roboto";
private List<TextFragment> _fragments10;
[GlobalSetup]
public void Setup()
{
Console.WriteLine("========================================");
Console.WriteLine("DEBUG NA BENCHMARKS - GlobalSetup START");
Console.WriteLine("========================================");
var fontsPath = Path.Combine(AppContext.BaseDirectory, "Fonts");
Console.WriteLine(string.Format("Fonts directory: {0}", fontsPath));
if (!Directory.Exists(fontsPath))
{
throw new DirectoryNotFoundException(
string.Format("Fonts directory not found: {0}", fontsPath));
}
_fontFolders = new List<string> { fontsPath };
Console.WriteLine("\nAvailable Roboto fonts:");
foreach (var file in Directory.GetFiles(fontsPath, "Roboto*.ttf"))
{
Console.WriteLine(string.Format(" {0}", Path.GetFileName(file)));
}
Console.WriteLine("\nLoading Roboto Regular...");
var font = OpenTypeFonts.LoadFont(FontFamily, FontSubFamily.Regular);
Console.WriteLine(string.Format("Loaded: {0} {1} ({2} glyphs)",
font.FullName, font.SubFamily, font.GlyfTable.Glyphs.Count));
var shaper = new TextShaper(font);
_layoutEngine = new TextLayoutEngine(shaper, _fontFolders, searchSystemDirectories: false);
Console.WriteLine("\nPre-warming font cache (Regular, Bold, Italic)...");
PrewarmFontCache();
Console.WriteLine("\nPreparing 10 test fragments...");
_fragments10 = new List<TextFragment>();
var measurementFont = new MeasurementFont
{
FontFamily = FontFamily,
Size = FontSize,
Style = MeasurementFontStyles.Regular
};
for (int i = 0; i < 10; i++)
{
_fragments10.Add(new TextFragment
{
Text = LoremIpsum20Para,
Font = measurementFont
});
}
Console.WriteLine(string.Format("Prepared {0} fragments, each with {1} chars",
_fragments10.Count, LoremIpsum20Para.Length));
Console.WriteLine("\n=== TESTING BENCHMARK 1 ===");
try
{
var sw = System.Diagnostics.Stopwatch.StartNew();
var result = TestBenchmark1();
sw.Stop();
Console.WriteLine(string.Format("SUCCESS: {0} lines in {1}ms",
result.Count, sw.ElapsedMilliseconds));
}
catch (Exception ex)
{
Console.WriteLine(string.Format("FAILED: {0}", ex.Message));
Console.WriteLine(ex.StackTrace);
}
Console.WriteLine("\n=== TESTING BENCHMARK 2 ===");
try
{
var sw = System.Diagnostics.Stopwatch.StartNew();
var result = TestBenchmark2();
sw.Stop();
Console.WriteLine(string.Format("SUCCESS: {0} lines in {1}ms",
result.Count, sw.ElapsedMilliseconds));
}
catch (Exception ex)
{
Console.WriteLine(string.Format("FAILED: {0}", ex.Message));
Console.WriteLine(ex.StackTrace);
}
Console.WriteLine("\n========================================");
Console.WriteLine("GlobalSetup COMPLETE");
Console.WriteLine("========================================\n");
}
private void PrewarmFontCache()
{
var warmupFragments = new List<TextFragment>
{
new TextFragment
{
Text = "warmup",
Font = new MeasurementFont
{
FontFamily = FontFamily,
Size = FontSize,
Style = MeasurementFontStyles.Regular
}
},
new TextFragment
{
Text = "warmup",
Font = new MeasurementFont
{
FontFamily = FontFamily,
Size = 12f,
Style = MeasurementFontStyles.Bold
}
},
new TextFragment
{
Text = "warmup",
Font = new MeasurementFont
{
FontFamily = FontFamily,
Size = FontSize,
Style = MeasurementFontStyles.Italic
}
}
};
var result = _layoutEngine.WrapRichText(warmupFragments, MaxPointWidth);
Console.WriteLine(string.Format(" Cache warmed ({0} lines)", result.Count));
}
private List<string> TestBenchmark1()
{
Console.WriteLine(" Wrapping 10 paragraphs sequentially...");
List<string> allLines = new List<string>();
for (int i = 0; i < 10; i++)
{
Console.WriteLine(string.Format(" Paragraph {0}...", i + 1));
var lines = _layoutEngine.WrapRichText(
new List<TextFragment> { _fragments10[i] },
MaxPointWidth
);
allLines.AddRange(lines);
}
return allLines;
}
private List<string> TestBenchmark2()
{
Console.WriteLine(" Creating 5 fragments with mixed fonts...");
var text = LoremIpsum20Para;
int chunkSize = text.Length / 5;
var fragments = new List<TextFragment>
{
new TextFragment
{
Text = text.Substring(0, chunkSize),
Font = new MeasurementFont { FontFamily = FontFamily, Size = FontSize }
},
new TextFragment
{
Text = text.Substring(chunkSize, chunkSize),
Font = new MeasurementFont { FontFamily = FontFamily, Size = 12f, Style = MeasurementFontStyles.Bold }
},
new TextFragment
{
Text = text.Substring(chunkSize * 2, chunkSize),
Font = new MeasurementFont { FontFamily = FontFamily, Size = FontSize, Style = MeasurementFontStyles.Italic }
},
new TextFragment
{
Text = text.Substring(chunkSize * 3, chunkSize),
Font = new MeasurementFont { FontFamily = FontFamily, Size = FontSize }
},
new TextFragment
{
Text = text.Substring(chunkSize * 4),
Font = new MeasurementFont { FontFamily = FontFamily, Size = 10f }
}
};
Console.WriteLine(string.Format(" Created {0} fragments", fragments.Count));
Console.WriteLine(" Wrapping...");
return _layoutEngine.WrapRichText(fragments, MaxPointWidth);
}
[Benchmark]
public List<string> Wrap_10Paragraphs_RichText()
{
List<string> allLines = new List<string>();
for (int i = 0; i < 10; i++)
{
var lines = _layoutEngine.WrapRichText(
new List<TextFragment> { _fragments10[i] },
MaxPointWidth
);
allLines.AddRange(lines);
}
return allLines;
}
[Benchmark]
public List<string> WrapRichText_MixedFonts_LongText()
{
var text = LoremIpsum20Para;
int chunkSize = text.Length / 5;
var fragments = new List<TextFragment>
{
new TextFragment
{
Text = text.Substring(0, chunkSize),
Font = new MeasurementFont { FontFamily = FontFamily, Size = FontSize }
},
new TextFragment
{
Text = text.Substring(chunkSize, chunkSize),
Font = new MeasurementFont { FontFamily = FontFamily, Size = 12f, Style = MeasurementFontStyles.Bold }
},
new TextFragment
{
Text = text.Substring(chunkSize * 2, chunkSize),
Font = new MeasurementFont { FontFamily = FontFamily, Size = FontSize, Style = MeasurementFontStyles.Italic }
},
new TextFragment
{
Text = text.Substring(chunkSize * 3, chunkSize),
Font = new MeasurementFont { FontFamily = FontFamily, Size = FontSize }
},
new TextFragment
{
Text = text.Substring(chunkSize * 4),
Font = new MeasurementFont { FontFamily = FontFamily, Size = 10f }
}
};
return _layoutEngine.WrapRichText(fragments, MaxPointWidth);
}
}
}