34
34
35
35
namespace txt {
36
36
37
- static void BM_ParagraphShortLayout (benchmark::State& state) {
37
+ class ParagraphFixture : public benchmark ::Fixture {
38
+ public:
39
+ void SetUp (const benchmark::State& state) {
40
+ font_collection_ = GetTestFontCollection ();
41
+
42
+ bitmap_ = std::make_unique<SkBitmap>();
43
+ bitmap_->allocN32Pixels (1000 , 1000 );
44
+ canvas_ = std::make_unique<SkCanvas>(*bitmap_);
45
+ canvas_->clear (SK_ColorWHITE);
46
+ }
47
+
48
+ void TearDown (const benchmark::State& state) { font_collection_.reset (); }
49
+
50
+ protected:
51
+ std::shared_ptr<FontCollection> font_collection_;
52
+ std::unique_ptr<SkCanvas> canvas_;
53
+ std::unique_ptr<SkBitmap> bitmap_;
54
+ };
55
+
56
+ BENCHMARK_F (ParagraphFixture, ShortLayout)(benchmark::State& state) {
38
57
const char * text = " Hello World" ;
39
58
auto icu_text = icu::UnicodeString::fromUTF8 (text);
40
59
std::u16string u16_text (icu_text.getBuffer (),
@@ -45,7 +64,7 @@ static void BM_ParagraphShortLayout(benchmark::State& state) {
45
64
txt::TextStyle text_style;
46
65
text_style.font_families = std::vector<std::string>(1 , " Roboto" );
47
66
text_style.color = SK_ColorBLACK;
48
- txt::ParagraphBuilderTxt builder (paragraph_style, GetTestFontCollection () );
67
+ txt::ParagraphBuilderTxt builder (paragraph_style, font_collection_ );
49
68
50
69
builder.PushStyle (text_style);
51
70
builder.AddText (u16_text);
@@ -56,9 +75,8 @@ static void BM_ParagraphShortLayout(benchmark::State& state) {
56
75
paragraph->Layout (300 );
57
76
}
58
77
}
59
- BENCHMARK (BM_ParagraphShortLayout);
60
78
61
- static void BM_ParagraphLongLayout (benchmark::State& state) {
79
+ BENCHMARK_F (ParagraphFixture, LongLayout) (benchmark::State& state) {
62
80
const char * text =
63
81
" This is a very long sentence to test if the text will properly wrap "
64
82
" around and go to the next line. Sometimes, short sentence. Longer "
@@ -87,7 +105,7 @@ static void BM_ParagraphLongLayout(benchmark::State& state) {
87
105
text_style.font_families = std::vector<std::string>(1 , " Roboto" );
88
106
text_style.color = SK_ColorBLACK;
89
107
90
- txt::ParagraphBuilderTxt builder (paragraph_style, GetTestFontCollection () );
108
+ txt::ParagraphBuilderTxt builder (paragraph_style, font_collection_ );
91
109
92
110
builder.PushStyle (text_style);
93
111
builder.AddText (u16_text);
@@ -98,9 +116,8 @@ static void BM_ParagraphLongLayout(benchmark::State& state) {
98
116
paragraph->Layout (300 );
99
117
}
100
118
}
101
- BENCHMARK (BM_ParagraphLongLayout);
102
119
103
- static void BM_ParagraphJustifyLayout (benchmark::State& state) {
120
+ BENCHMARK_F (ParagraphFixture, JustifyLayout) (benchmark::State& state) {
104
121
const char * text =
105
122
" This is a very long sentence to test if the text will properly wrap "
106
123
" around and go to the next line. Sometimes, short sentence. Longer "
@@ -130,7 +147,7 @@ static void BM_ParagraphJustifyLayout(benchmark::State& state) {
130
147
text_style.font_families = std::vector<std::string>(1 , " Roboto" );
131
148
text_style.color = SK_ColorBLACK;
132
149
133
- txt::ParagraphBuilderTxt builder (paragraph_style, GetTestFontCollection () );
150
+ txt::ParagraphBuilderTxt builder (paragraph_style, font_collection_ );
134
151
135
152
builder.PushStyle (text_style);
136
153
builder.AddText (u16_text);
@@ -141,9 +158,8 @@ static void BM_ParagraphJustifyLayout(benchmark::State& state) {
141
158
paragraph->Layout (300 );
142
159
}
143
160
}
144
- BENCHMARK (BM_ParagraphJustifyLayout);
145
161
146
- static void BM_ParagraphManyStylesLayout (benchmark::State& state) {
162
+ BENCHMARK_F (ParagraphFixture, ManyStylesLayout) (benchmark::State& state) {
147
163
const char * text = " -" ;
148
164
auto icu_text = icu::UnicodeString::fromUTF8 (text);
149
165
std::u16string u16_text (icu_text.getBuffer (),
@@ -154,7 +170,7 @@ static void BM_ParagraphManyStylesLayout(benchmark::State& state) {
154
170
txt::TextStyle text_style;
155
171
text_style.font_families = std::vector<std::string>(1 , " Roboto" );
156
172
text_style.color = SK_ColorBLACK;
157
- txt::ParagraphBuilderTxt builder (paragraph_style, GetTestFontCollection () );
173
+ txt::ParagraphBuilderTxt builder (paragraph_style, font_collection_ );
158
174
for (int i = 0 ; i < 1000 ; ++i) {
159
175
builder.PushStyle (text_style);
160
176
builder.AddText (u16_text);
@@ -165,9 +181,8 @@ static void BM_ParagraphManyStylesLayout(benchmark::State& state) {
165
181
paragraph->Layout (300 );
166
182
}
167
183
}
168
- BENCHMARK (BM_ParagraphManyStylesLayout);
169
184
170
- static void BM_ParagraphTextBigO (benchmark::State& state) {
185
+ BENCHMARK_DEFINE_F (ParagraphFixture, TextBigO) (benchmark::State& state) {
171
186
std::vector<uint16_t > text;
172
187
for (uint16_t i = 0 ; i < state.range (0 ); ++i) {
173
188
text.push_back (i % 5 == 0 ? ' ' : i);
@@ -181,7 +196,7 @@ static void BM_ParagraphTextBigO(benchmark::State& state) {
181
196
text_style.font_families = std::vector<std::string>(1 , " Roboto" );
182
197
text_style.color = SK_ColorBLACK;
183
198
184
- txt::ParagraphBuilderTxt builder (paragraph_style, GetTestFontCollection () );
199
+ txt::ParagraphBuilderTxt builder (paragraph_style, font_collection_ );
185
200
186
201
builder.PushStyle (text_style);
187
202
builder.AddText (u16_text);
@@ -193,12 +208,12 @@ static void BM_ParagraphTextBigO(benchmark::State& state) {
193
208
}
194
209
state.SetComplexityN (state.range (0 ));
195
210
}
196
- BENCHMARK (BM_ParagraphTextBigO )
211
+ BENCHMARK_REGISTER_F (ParagraphFixture, TextBigO )
197
212
->RangeMultiplier (4 )
198
213
->Range(1 << 6 , 1 << 14 )
199
214
->Complexity(benchmark::oN);
200
215
201
- static void BM_ParagraphStylesBigO (benchmark::State& state) {
216
+ BENCHMARK_DEFINE_F (ParagraphFixture, StylesBigO) (benchmark::State& state) {
202
217
const char * text = " vry shrt " ;
203
218
auto icu_text = icu::UnicodeString::fromUTF8 (text);
204
219
std::u16string u16_text (icu_text.getBuffer (),
@@ -210,7 +225,7 @@ static void BM_ParagraphStylesBigO(benchmark::State& state) {
210
225
text_style.font_families = std::vector<std::string>(1 , " Roboto" );
211
226
text_style.color = SK_ColorBLACK;
212
227
213
- txt::ParagraphBuilderTxt builder (paragraph_style, GetTestFontCollection () );
228
+ txt::ParagraphBuilderTxt builder (paragraph_style, font_collection_ );
214
229
215
230
for (int i = 0 ; i < state.range (0 ); ++i) {
216
231
builder.PushStyle (text_style);
@@ -223,12 +238,12 @@ static void BM_ParagraphStylesBigO(benchmark::State& state) {
223
238
}
224
239
state.SetComplexityN (state.range (0 ));
225
240
}
226
- BENCHMARK (BM_ParagraphStylesBigO )
241
+ BENCHMARK_REGISTER_F (ParagraphFixture, StylesBigO )
227
242
->RangeMultiplier (4 )
228
243
->Range(1 << 3 , 1 << 12 )
229
244
->Complexity(benchmark::oN);
230
245
231
- static void BM_ParagraphPaintSimple (benchmark::State& state) {
246
+ BENCHMARK_F (ParagraphFixture, PaintSimple) (benchmark::State& state) {
232
247
const char * text = " Hello world! This is a simple sentence to test drawing." ;
233
248
auto icu_text = icu::UnicodeString::fromUTF8 (text);
234
249
std::u16string u16_text (icu_text.getBuffer (),
@@ -239,25 +254,20 @@ static void BM_ParagraphPaintSimple(benchmark::State& state) {
239
254
txt::TextStyle text_style;
240
255
text_style.font_families = std::vector<std::string>(1 , " Roboto" );
241
256
text_style.color = SK_ColorBLACK;
242
- txt::ParagraphBuilderTxt builder (paragraph_style, GetTestFontCollection () );
257
+ txt::ParagraphBuilderTxt builder (paragraph_style, font_collection_ );
243
258
builder.PushStyle (text_style);
244
259
builder.AddText (u16_text);
245
260
auto paragraph = BuildParagraph (builder);
246
261
paragraph->Layout (300 );
247
262
248
- std::unique_ptr<SkBitmap> bitmap = std::make_unique<SkBitmap>();
249
- bitmap->allocN32Pixels (1000 , 1000 );
250
- std::unique_ptr<SkCanvas> canvas = std::make_unique<SkCanvas>(*bitmap);
251
- canvas->clear (SK_ColorWHITE);
252
263
int offset = 0 ;
253
264
while (state.KeepRunning ()) {
254
- paragraph->Paint (canvas .get (), offset % 700 , 10 );
265
+ paragraph->Paint (canvas_ .get (), offset % 700 , 10 );
255
266
offset++;
256
267
}
257
268
}
258
- BENCHMARK (BM_ParagraphPaintSimple);
259
269
260
- static void BM_ParagraphPaintLarge (benchmark::State& state) {
270
+ BENCHMARK_F (ParagraphFixture, PaintLarge) (benchmark::State& state) {
261
271
const char * text =
262
272
" Hello world! This is a simple sentence to test drawing. Hello world! "
263
273
" This is a simple sentence to test drawing. Hello world! This is a "
@@ -286,25 +296,20 @@ static void BM_ParagraphPaintLarge(benchmark::State& state) {
286
296
txt::TextStyle text_style;
287
297
text_style.font_families = std::vector<std::string>(1 , " Roboto" );
288
298
text_style.color = SK_ColorBLACK;
289
- txt::ParagraphBuilderTxt builder (paragraph_style, GetTestFontCollection () );
299
+ txt::ParagraphBuilderTxt builder (paragraph_style, font_collection_ );
290
300
builder.PushStyle (text_style);
291
301
builder.AddText (u16_text);
292
302
auto paragraph = BuildParagraph (builder);
293
303
paragraph->Layout (300 );
294
304
295
- std::unique_ptr<SkBitmap> bitmap = std::make_unique<SkBitmap>();
296
- bitmap->allocN32Pixels (1000 , 1000 );
297
- std::unique_ptr<SkCanvas> canvas = std::make_unique<SkCanvas>(*bitmap);
298
- canvas->clear (SK_ColorWHITE);
299
305
int offset = 0 ;
300
306
while (state.KeepRunning ()) {
301
- paragraph->Paint (canvas .get (), offset % 700 , 10 );
307
+ paragraph->Paint (canvas_ .get (), offset % 700 , 10 );
302
308
offset++;
303
309
}
304
310
}
305
- BENCHMARK (BM_ParagraphPaintLarge);
306
311
307
- static void BM_ParagraphPaintDecoration (benchmark::State& state) {
312
+ BENCHMARK_F (ParagraphFixture, PaintDecoration) (benchmark::State& state) {
308
313
const char * text =
309
314
" Hello world! This is a simple sentence to test drawing. Hello world! "
310
315
" This is a simple sentence to test drawing." ;
@@ -322,7 +327,7 @@ static void BM_ParagraphPaintDecoration(benchmark::State& state) {
322
327
text_style.decoration_style = TextDecorationStyle (kSolid );
323
328
text_style.color = SK_ColorBLACK;
324
329
325
- txt::ParagraphBuilderTxt builder (paragraph_style, GetTestFontCollection () );
330
+ txt::ParagraphBuilderTxt builder (paragraph_style, font_collection_ );
326
331
327
332
builder.PushStyle (text_style);
328
333
builder.AddText (u16_text);
@@ -338,17 +343,12 @@ static void BM_ParagraphPaintDecoration(benchmark::State& state) {
338
343
auto paragraph = BuildParagraph (builder);
339
344
paragraph->Layout (300 );
340
345
341
- std::unique_ptr<SkBitmap> bitmap = std::make_unique<SkBitmap>();
342
- bitmap->allocN32Pixels (1000 , 1000 );
343
- std::unique_ptr<SkCanvas> canvas = std::make_unique<SkCanvas>(*bitmap);
344
- canvas->clear (SK_ColorWHITE);
345
346
int offset = 0 ;
346
347
while (state.KeepRunning ()) {
347
- paragraph->Paint (canvas .get (), offset % 700 , 10 );
348
+ paragraph->Paint (canvas_ .get (), offset % 700 , 10 );
348
349
offset++;
349
350
}
350
351
}
351
- BENCHMARK (BM_ParagraphPaintDecoration);
352
352
353
353
// -----------------------------------------------------------------------------
354
354
//
@@ -357,7 +357,7 @@ BENCHMARK(BM_ParagraphPaintDecoration);
357
357
//
358
358
// -----------------------------------------------------------------------------
359
359
360
- static void BM_ParagraphMinikinDoLayout (benchmark::State& state) {
360
+ BENCHMARK_DEFINE_F (ParagraphFixture, MinikinDoLayout) (benchmark::State& state) {
361
361
std::vector<uint16_t > text;
362
362
for (uint16_t i = 0 ; i < 16000 * 2 ; ++i) {
363
363
text.push_back (i % 5 == 0 ? ' ' : i);
@@ -372,9 +372,8 @@ static void BM_ParagraphMinikinDoLayout(benchmark::State& state) {
372
372
paint.letterSpacing = text_style.letter_spacing ;
373
373
paint.wordSpacing = text_style.word_spacing ;
374
374
375
- auto collection =
376
- GetTestFontCollection ()->GetMinikinFontCollectionForFamilies (
377
- text_style.font_families , " en-US" );
375
+ auto collection = font_collection_->GetMinikinFontCollectionForFamilies (
376
+ text_style.font_families , " en-US" );
378
377
379
378
while (state.KeepRunning ()) {
380
379
minikin::Layout layout;
@@ -383,12 +382,12 @@ static void BM_ParagraphMinikinDoLayout(benchmark::State& state) {
383
382
}
384
383
state.SetComplexityN (state.range (0 ));
385
384
}
386
- BENCHMARK (BM_ParagraphMinikinDoLayout )
385
+ BENCHMARK_REGISTER_F (ParagraphFixture, MinikinDoLayout )
387
386
->RangeMultiplier (4 )
388
387
->Range(1 << 7 , 1 << 14 )
389
388
->Complexity(benchmark::oN);
390
389
391
- static void BM_ParagraphMinikinAddStyleRun (benchmark::State& state) {
390
+ BENCHMARK_DEFINE_F (ParagraphFixture, AddStyleRun) (benchmark::State& state) {
392
391
std::vector<uint16_t > text;
393
392
for (uint16_t i = 0 ; i < 16000 * 2 ; ++i) {
394
393
text.push_back (i % 5 == 0 ? ' ' : i);
@@ -403,8 +402,6 @@ static void BM_ParagraphMinikinAddStyleRun(benchmark::State& state) {
403
402
paint.letterSpacing = text_style.letter_spacing ;
404
403
paint.wordSpacing = text_style.word_spacing ;
405
404
406
- auto font_collection = GetTestFontCollection ();
407
-
408
405
minikin::LineBreaker breaker;
409
406
breaker.setLocale (icu::Locale (), nullptr );
410
407
breaker.resize (text.size ());
@@ -414,20 +411,20 @@ static void BM_ParagraphMinikinAddStyleRun(benchmark::State& state) {
414
411
while (state.KeepRunning ()) {
415
412
for (int i = 0 ; i < 20 ; ++i) {
416
413
breaker.addStyleRun (&paint,
417
- font_collection ->GetMinikinFontCollectionForFamilies (
414
+ font_collection_ ->GetMinikinFontCollectionForFamilies (
418
415
std::vector<std::string>(1 , " Roboto" ), " en-US" ),
419
416
font, state.range (0 ) / 20 * i,
420
417
state.range (0 ) / 20 * (i + 1 ), false );
421
418
}
422
419
}
423
420
state.SetComplexityN (state.range (0 ));
424
421
}
425
- BENCHMARK (BM_ParagraphMinikinAddStyleRun )
422
+ BENCHMARK_REGISTER_F (ParagraphFixture, AddStyleRun )
426
423
->RangeMultiplier (4 )
427
424
->Range(1 << 7 , 1 << 14 )
428
425
->Complexity(benchmark::oN);
429
426
430
- static void BM_ParagraphSkTextBlobAlloc (benchmark::State& state) {
427
+ BENCHMARK_DEFINE_F (ParagraphFixture, SkTextBlobAlloc) (benchmark::State& state) {
431
428
SkFont font;
432
429
font.setEdging (SkFont::Edging::kAntiAlias );
433
430
font.setSize (14 );
@@ -439,7 +436,7 @@ static void BM_ParagraphSkTextBlobAlloc(benchmark::State& state) {
439
436
}
440
437
state.SetComplexityN (state.range (0 ));
441
438
}
442
- BENCHMARK (BM_ParagraphSkTextBlobAlloc )
439
+ BENCHMARK_REGISTER_F (ParagraphFixture, SkTextBlobAlloc )
443
440
->RangeMultiplier (4 )
444
441
->Range(1 << 7 , 1 << 14 )
445
442
->Complexity(benchmark::oN);
0 commit comments