|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, Zhang Ji Peng |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright notice, this |
| 9 | + * list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * * Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 19 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 21 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 22 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 23 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "test.h" |
| 28 | + |
| 29 | +PERF_TEST_DEFINE(Font); |
| 30 | + |
| 31 | +// Test 1: Basic font creation and destruction |
| 32 | +PERF_TEST(Font, FontCreateDestroy) |
| 33 | +{ |
| 34 | + for (int i = 0; i < 10000; i++) { |
| 35 | + ps_font* font = ps_font_create("Arial", CHARSET_ANSI, 12.0f, FONT_WEIGHT_REGULAR, False); |
| 36 | + ps_font_unref(font); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// Test 2: Font creation with all charsets |
| 41 | +PERF_TEST_RUN(Font, FontCreateAllCharsets) |
| 42 | +{ |
| 43 | + auto result = RunBenchmark(Font_FontCreateAllCharsets, [&]() { |
| 44 | + for (int i = 0; i < 5000; i++) { |
| 45 | + ps_font* font1 = ps_font_create("Arial", CHARSET_ANSI, 12.0f, FONT_WEIGHT_REGULAR, False); |
| 46 | + ps_font* font2 = ps_font_create("Arial", CHARSET_UNICODE, 12.0f, FONT_WEIGHT_REGULAR, False); |
| 47 | + ps_font_unref(font1); |
| 48 | + ps_font_unref(font2); |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + CompareToBenchmark(Font_FontCreateAllCharsets, result); |
| 53 | +} |
| 54 | + |
| 55 | +// Test 3: Font creation with all weights |
| 56 | +PERF_TEST_RUN(Font, FontCreateAllWeights) |
| 57 | +{ |
| 58 | + ps_font_weight weights[] = { |
| 59 | + FONT_WEIGHT_REGULAR, FONT_WEIGHT_MEDIUM, FONT_WEIGHT_BOLD, FONT_WEIGHT_HEAVY |
| 60 | + }; |
| 61 | + |
| 62 | + auto result = RunBenchmark(Font_FontCreateAllWeights, [&]() { |
| 63 | + for (int i = 0; i < 2500; i++) { |
| 64 | + for (int j = 0; j < 4; j++) { |
| 65 | + ps_font* font = ps_font_create("Arial", CHARSET_ANSI, 12.0f, weights[j], False); |
| 66 | + ps_font_unref(font); |
| 67 | + } |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + CompareToBenchmark(Font_FontCreateAllWeights, result); |
| 72 | +} |
| 73 | + |
| 74 | +// Test 4: Font creation with italic variants |
| 75 | +PERF_TEST_RUN(Font, FontCreateItalicVariants) |
| 76 | +{ |
| 77 | + auto result = RunBenchmark(Font_FontCreateItalicVariants, [&]() { |
| 78 | + for (int i = 0; i < 5000; i++) { |
| 79 | + ps_font* regular = ps_font_create("Arial", CHARSET_ANSI, 12.0f, FONT_WEIGHT_REGULAR, False); |
| 80 | + ps_font* italic = ps_font_create("Arial", CHARSET_ANSI, 12.0f, FONT_WEIGHT_REGULAR, True); |
| 81 | + ps_font_unref(regular); |
| 82 | + ps_font_unref(italic); |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + CompareToBenchmark(Font_FontCreateItalicVariants, result); |
| 87 | +} |
| 88 | + |
| 89 | +// Test 5: Font creation with different sizes |
| 90 | +PERF_TEST_RUN(Font, FontCreateDifferentSizes) |
| 91 | +{ |
| 92 | + float sizes[] = {8.0f, 12.0f, 16.0f, 24.0f, 36.0f, 48.0f, 72.0f, 96.0f}; |
| 93 | + |
| 94 | + auto result = RunBenchmark(Font_FontCreateDifferentSizes, [&]() { |
| 95 | + for (int i = 0; i < 1250; i++) { |
| 96 | + for (int j = 0; j < 8; j++) { |
| 97 | + ps_font* font = ps_font_create("Arial", CHARSET_ANSI, sizes[j], FONT_WEIGHT_REGULAR, False); |
| 98 | + ps_font_unref(font); |
| 99 | + } |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + CompareToBenchmark(Font_FontCreateDifferentSizes, result); |
| 104 | +} |
| 105 | + |
| 106 | +// Test 6: Font creation copy |
| 107 | +PERF_TEST_RUN(Font, FontCreateCopy) |
| 108 | +{ |
| 109 | + ps_font* source = ps_font_create("Arial", CHARSET_ANSI, 18.0f, FONT_WEIGHT_BOLD, True); |
| 110 | + |
| 111 | + auto result = RunBenchmark(Font_FontCreateCopy, [&]() { |
| 112 | + for (int i = 0; i < 5000; i++) { |
| 113 | + ps_font* copy = ps_font_create_copy(source); |
| 114 | + ps_font_unref(copy); |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + CompareToBenchmark(Font_FontCreateCopy, result); |
| 119 | + ps_font_unref(source); |
| 120 | +} |
| 121 | + |
| 122 | +// Test 7: Font reference counting |
| 123 | +PERF_TEST_RUN(Font, FontReferenceOperations) |
| 124 | +{ |
| 125 | + ps_font* font = ps_font_create("Arial", CHARSET_ANSI, 12.0f, FONT_WEIGHT_REGULAR, False); |
| 126 | + |
| 127 | + auto result = RunBenchmark(Font_FontReferenceOperations, [&]() { |
| 128 | + for (int i = 0; i < 100000; i++) { |
| 129 | + ps_font_ref(font); |
| 130 | + ps_font_unref(font); |
| 131 | + } |
| 132 | + }); |
| 133 | + |
| 134 | + CompareToBenchmark(Font_FontReferenceOperations, result); |
| 135 | + ps_font_unref(font); |
| 136 | +} |
| 137 | + |
| 138 | +// Test 8: Multiple font references |
| 139 | +PERF_TEST_RUN(Font, FontMultipleReferences) |
| 140 | +{ |
| 141 | + auto result = RunBenchmark(Font_FontMultipleReferences, [&]() { |
| 142 | + for (int i = 0; i < 1000; i++) { |
| 143 | + ps_font* font = ps_font_create("Arial", CHARSET_ANSI, 12.0f, FONT_WEIGHT_REGULAR, False); |
| 144 | + ps_font_ref(font); |
| 145 | + ps_font_ref(font); |
| 146 | + ps_font_unref(font); |
| 147 | + ps_font_unref(font); |
| 148 | + ps_font_unref(font); |
| 149 | + } |
| 150 | + }); |
| 151 | + |
| 152 | + CompareToBenchmark(Font_FontMultipleReferences, result); |
| 153 | +} |
| 154 | + |
| 155 | +// Test 9: Font property operations - size |
| 156 | +PERF_TEST_RUN(Font, FontSizeOperations) |
| 157 | +{ |
| 158 | + ps_font* font = ps_font_create("Arial", CHARSET_ANSI, 12.0f, FONT_WEIGHT_REGULAR, False); |
| 159 | + |
| 160 | + auto result = RunBenchmark(Font_FontSizeOperations, [&]() { |
| 161 | + for (int i = 0; i < 50000; i++) { |
| 162 | + ps_font_set_size(font, 8.0f + (i % 20) * 2.0f); |
| 163 | + } |
| 164 | + }); |
| 165 | + |
| 166 | + CompareToBenchmark(Font_FontSizeOperations, result); |
| 167 | + ps_font_unref(font); |
| 168 | +} |
| 169 | + |
| 170 | +// Test 10: Font property operations - weight |
| 171 | +PERF_TEST_RUN(Font, FontWeightOperations) |
| 172 | +{ |
| 173 | + ps_font* font = ps_font_create("Arial", CHARSET_ANSI, 12.0f, FONT_WEIGHT_REGULAR, False); |
| 174 | + ps_font_weight weights[] = { |
| 175 | + FONT_WEIGHT_REGULAR, FONT_WEIGHT_MEDIUM, FONT_WEIGHT_BOLD, FONT_WEIGHT_HEAVY |
| 176 | + }; |
| 177 | + |
| 178 | + auto result = RunBenchmark(Font_FontWeightOperations, [&]() { |
| 179 | + for (int i = 0; i < 25000; i++) { |
| 180 | + ps_font_set_weight(font, weights[i % 4]); |
| 181 | + } |
| 182 | + }); |
| 183 | + |
| 184 | + CompareToBenchmark(Font_FontWeightOperations, result); |
| 185 | + ps_font_unref(font); |
| 186 | +} |
| 187 | + |
| 188 | +// Test 11: Font property operations - italic |
| 189 | +PERF_TEST_RUN(Font, FontItalicOperations) |
| 190 | +{ |
| 191 | + ps_font* font = ps_font_create("Arial", CHARSET_ANSI, 12.0f, FONT_WEIGHT_REGULAR, False); |
| 192 | + |
| 193 | + auto result = RunBenchmark(Font_FontItalicOperations, [&]() { |
| 194 | + for (int i = 0; i < 50000; i++) { |
| 195 | + ps_font_set_italic(font, (i % 2) ? True : False); |
| 196 | + } |
| 197 | + }); |
| 198 | + |
| 199 | + CompareToBenchmark(Font_FontItalicOperations, result); |
| 200 | + ps_font_unref(font); |
| 201 | +} |
| 202 | + |
| 203 | +// Test 12: Font property operations - charset |
| 204 | +PERF_TEST_RUN(Font, FontCharsetOperations) |
| 205 | +{ |
| 206 | + ps_font* font = ps_font_create("Arial", CHARSET_ANSI, 12.0f, FONT_WEIGHT_REGULAR, False); |
| 207 | + |
| 208 | + auto result = RunBenchmark(Font_FontCharsetOperations, [&]() { |
| 209 | + for (int i = 0; i < 50000; i++) { |
| 210 | + ps_font_set_charset(font, (i % 2) ? CHARSET_ANSI : CHARSET_UNICODE); |
| 211 | + } |
| 212 | + }); |
| 213 | + |
| 214 | + CompareToBenchmark(Font_FontCharsetOperations, result); |
| 215 | + ps_font_unref(font); |
| 216 | +} |
| 217 | + |
| 218 | +// Test 13: Font property operations - hint |
| 219 | +PERF_TEST_RUN(Font, FontHintOperations) |
| 220 | +{ |
| 221 | + ps_font* font = ps_font_create("Arial", CHARSET_ANSI, 12.0f, FONT_WEIGHT_REGULAR, False); |
| 222 | + |
| 223 | + auto result = RunBenchmark(Font_FontHintOperations, [&]() { |
| 224 | + for (int i = 0; i < 50000; i++) { |
| 225 | + ps_font_set_hint(font, (i % 2) ? True : False); |
| 226 | + } |
| 227 | + }); |
| 228 | + |
| 229 | + CompareToBenchmark(Font_FontHintOperations, result); |
| 230 | + ps_font_unref(font); |
| 231 | +} |
| 232 | + |
| 233 | +// Test 14: Font property operations - flip |
| 234 | +PERF_TEST_RUN(Font, FontFlipOperations) |
| 235 | +{ |
| 236 | + ps_font* font = ps_font_create("Arial", CHARSET_ANSI, 12.0f, FONT_WEIGHT_REGULAR, False); |
| 237 | + |
| 238 | + auto result = RunBenchmark(Font_FontFlipOperations, [&]() { |
| 239 | + for (int i = 0; i < 50000; i++) { |
| 240 | + ps_font_set_flip(font, (i % 2) ? True : False); |
| 241 | + } |
| 242 | + }); |
| 243 | + |
| 244 | + CompareToBenchmark(Font_FontFlipOperations, result); |
| 245 | + ps_font_unref(font); |
| 246 | +} |
| 247 | + |
| 248 | +// Test 15: Complex font property operations |
| 249 | +PERF_TEST_RUN(Font, FontComplexPropertyOperations) |
| 250 | +{ |
| 251 | + ps_font* font = ps_font_create("Arial", CHARSET_ANSI, 12.0f, FONT_WEIGHT_REGULAR, False); |
| 252 | + ps_font_weight weights[] = { |
| 253 | + FONT_WEIGHT_REGULAR, FONT_WEIGHT_MEDIUM, FONT_WEIGHT_BOLD, FONT_WEIGHT_HEAVY |
| 254 | + }; |
| 255 | + |
| 256 | + auto result = RunBenchmark(Font_FontComplexPropertyOperations, [&]() { |
| 257 | + for (int i = 0; i < 10000; i++) { |
| 258 | + ps_font_set_size(font, 8.0f + (i % 10) * 4.0f); |
| 259 | + ps_font_set_weight(font, weights[i % 4]); |
| 260 | + ps_font_set_italic(font, (i % 2) ? True : False); |
| 261 | + ps_font_set_charset(font, (i % 2) ? CHARSET_ANSI : CHARSET_UNICODE); |
| 262 | + ps_font_set_hint(font, (i % 2) ? True : False); |
| 263 | + ps_font_set_flip(font, (i % 2) ? True : False); |
| 264 | + } |
| 265 | + }); |
| 266 | + |
| 267 | + CompareToBenchmark(Font_FontComplexPropertyOperations, result); |
| 268 | + ps_font_unref(font); |
| 269 | +} |
| 270 | + |
| 271 | +// Test 16: Context font operations |
| 272 | +PERF_TEST_RUN(Font, FontContextOperations) |
| 273 | +{ |
| 274 | + ps_context* ctx = ps_context_create(get_test_canvas(), NULL); |
| 275 | + ps_font* fonts[4]; |
| 276 | + |
| 277 | + fonts[0] = ps_font_create("Arial", CHARSET_ANSI, 12.0f, FONT_WEIGHT_REGULAR, False); |
| 278 | + fonts[1] = ps_font_create("Arial", CHARSET_ANSI, 16.0f, FONT_WEIGHT_BOLD, False); |
| 279 | + fonts[2] = ps_font_create("Arial", CHARSET_ANSI, 20.0f, FONT_WEIGHT_MEDIUM, True); |
| 280 | + fonts[3] = ps_font_create("Arial", CHARSET_UNICODE, 24.0f, FONT_WEIGHT_HEAVY, False); |
| 281 | + |
| 282 | + auto result = RunBenchmark(Font_FontContextOperations, [&]() { |
| 283 | + for (int i = 0; i < 10000; i++) { |
| 284 | + for (int j = 0; j < 4; j++) { |
| 285 | + ps_font* oldFont = ps_set_font(ctx, fonts[j]); |
| 286 | + ps_font_info info; |
| 287 | + ps_get_font_info(ctx, &info); |
| 288 | + ps_set_font(ctx, oldFont); |
| 289 | + } |
| 290 | + } |
| 291 | + }); |
| 292 | + |
| 293 | + CompareToBenchmark(Font_FontContextOperations, result); |
| 294 | + |
| 295 | + for (int i = 0; i < 4; i++) { |
| 296 | + ps_font_unref(fonts[i]); |
| 297 | + } |
| 298 | + ps_context_unref(ctx); |
| 299 | +} |
| 300 | + |
| 301 | +// Test 17: Stress test with many fonts |
| 302 | +PERF_TEST_RUN(Font, FontStressTest) |
| 303 | +{ |
| 304 | + const int num_fonts = 1000; |
| 305 | + ps_font* fonts[num_fonts]; |
| 306 | + |
| 307 | + auto result = RunBenchmark(Font_FontStressTest, [&]() { |
| 308 | + // Create many fonts |
| 309 | + for (int i = 0; i < num_fonts; i++) { |
| 310 | + fonts[i] = ps_font_create("Arial", CHARSET_ANSI, 12.0f + i * 0.01f, FONT_WEIGHT_REGULAR, False); |
| 311 | + } |
| 312 | + |
| 313 | + // Reference operations |
| 314 | + for (int i = 0; i < 100; i++) { |
| 315 | + for (int j = 0; j < num_fonts; j++) { |
| 316 | + ps_font_ref(fonts[j]); |
| 317 | + } |
| 318 | + for (int j = 0; j < num_fonts; j++) { |
| 319 | + ps_font_unref(fonts[j]); |
| 320 | + } |
| 321 | + } |
| 322 | + |
| 323 | + // Clean up |
| 324 | + for (int i = 0; i < num_fonts; i++) { |
| 325 | + ps_font_unref(fonts[i]); |
| 326 | + } |
| 327 | + }); |
| 328 | + |
| 329 | + CompareToBenchmark(Font_FontStressTest, result); |
| 330 | +} |
| 331 | + |
| 332 | +// Test 18: Font family variations |
| 333 | +PERF_TEST_RUN(Font, FontFamilyVariations) |
| 334 | +{ |
| 335 | + const char* families[] = {"Arial", "Times New Roman", "Courier New", "Verdana", "Helvetica"}; |
| 336 | + |
| 337 | + auto result = RunBenchmark(Font_FontFamilyVariations, [&]() { |
| 338 | + for (int i = 0; i < 2000; i++) { |
| 339 | + for (int j = 0; j < 5; j++) { |
| 340 | + ps_font* font = ps_font_create(families[j], CHARSET_ANSI, 12.0f, FONT_WEIGHT_REGULAR, False); |
| 341 | + ps_font_unref(font); |
| 342 | + } |
| 343 | + } |
| 344 | + }); |
| 345 | + |
| 346 | + CompareToBenchmark(Font_FontFamilyVariations, result); |
| 347 | +} |
| 348 | + |
| 349 | +// Test 19: Error handling with NULL parameters |
| 350 | +PERF_TEST_RUN(Font, FontErrorHandling) |
| 351 | +{ |
| 352 | + auto result = RunBenchmark(Font_FontErrorHandling, [&]() { |
| 353 | + for (int i = 0; i < 10000; i++) { |
| 354 | + ps_font_create(NULL, CHARSET_ANSI, 12.0f, FONT_WEIGHT_REGULAR, False); |
| 355 | + ps_font_create_copy(NULL); |
| 356 | + ps_font_ref(NULL); |
| 357 | + ps_font_unref(NULL); |
| 358 | + ps_font_set_size(NULL, 12.0f); |
| 359 | + ps_font_set_weight(NULL, FONT_WEIGHT_REGULAR); |
| 360 | + ps_font_set_italic(NULL, False); |
| 361 | + ps_font_set_charset(NULL, CHARSET_ANSI); |
| 362 | + ps_font_set_hint(NULL, True); |
| 363 | + ps_font_set_flip(NULL, False); |
| 364 | + } |
| 365 | + }); |
| 366 | + |
| 367 | + CompareToBenchmark(Font_FontErrorHandling, result); |
| 368 | +} |
| 369 | + |
| 370 | +// Test 20: Font reuse performance |
| 371 | +PERF_TEST_RUN(Font, FontReusePerformance) |
| 372 | +{ |
| 373 | + ps_font* font = ps_font_create("Arial", CHARSET_ANSI, 12.0f, FONT_WEIGHT_REGULAR, False); |
| 374 | + |
| 375 | + auto result = RunBenchmark(Font_FontReusePerformance, [&]() { |
| 376 | + for (int i = 0; i < 10000; i++) { |
| 377 | + ps_font_set_size(font, 8.0f + (i % 20) * 2.0f); |
| 378 | + ps_font_set_weight(font, FONT_WEIGHT_REGULAR + (i % 4) * 100); |
| 379 | + ps_font_set_italic(font, (i % 2) ? True : False); |
| 380 | + ps_font_set_charset(font, (i % 2) ? CHARSET_ANSI : CHARSET_UNICODE); |
| 381 | + ps_font_set_hint(font, (i % 2) ? True : False); |
| 382 | + ps_font_set_flip(font, (i % 2) ? True : False); |
| 383 | + } |
| 384 | + }); |
| 385 | + |
| 386 | + CompareToBenchmark(Font_FontReusePerformance, result); |
| 387 | + ps_font_unref(font); |
| 388 | +} |
0 commit comments