3434
3535namespace txt {
3636
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) {
3857 const char * text = " Hello World" ;
3958 auto icu_text = icu::UnicodeString::fromUTF8 (text);
4059 std::u16string u16_text (icu_text.getBuffer (),
@@ -45,7 +64,7 @@ static void BM_ParagraphShortLayout(benchmark::State& state) {
4564 txt::TextStyle text_style;
4665 text_style.font_families = std::vector<std::string>(1 , " Roboto" );
4766 text_style.color = SK_ColorBLACK;
48- txt::ParagraphBuilderTxt builder (paragraph_style, GetTestFontCollection () );
67+ txt::ParagraphBuilderTxt builder (paragraph_style, font_collection_ );
4968
5069 builder.PushStyle (text_style);
5170 builder.AddText (u16_text);
@@ -56,9 +75,8 @@ static void BM_ParagraphShortLayout(benchmark::State& state) {
5675 paragraph->Layout (300 );
5776 }
5877}
59- BENCHMARK (BM_ParagraphShortLayout);
6078
61- static void BM_ParagraphLongLayout (benchmark::State& state) {
79+ BENCHMARK_F (ParagraphFixture, LongLayout) (benchmark::State& state) {
6280 const char * text =
6381 " This is a very long sentence to test if the text will properly wrap "
6482 " around and go to the next line. Sometimes, short sentence. Longer "
@@ -87,7 +105,7 @@ static void BM_ParagraphLongLayout(benchmark::State& state) {
87105 text_style.font_families = std::vector<std::string>(1 , " Roboto" );
88106 text_style.color = SK_ColorBLACK;
89107
90- txt::ParagraphBuilderTxt builder (paragraph_style, GetTestFontCollection () );
108+ txt::ParagraphBuilderTxt builder (paragraph_style, font_collection_ );
91109
92110 builder.PushStyle (text_style);
93111 builder.AddText (u16_text);
@@ -98,9 +116,8 @@ static void BM_ParagraphLongLayout(benchmark::State& state) {
98116 paragraph->Layout (300 );
99117 }
100118}
101- BENCHMARK (BM_ParagraphLongLayout);
102119
103- static void BM_ParagraphJustifyLayout (benchmark::State& state) {
120+ BENCHMARK_F (ParagraphFixture, JustifyLayout) (benchmark::State& state) {
104121 const char * text =
105122 " This is a very long sentence to test if the text will properly wrap "
106123 " around and go to the next line. Sometimes, short sentence. Longer "
@@ -130,7 +147,7 @@ static void BM_ParagraphJustifyLayout(benchmark::State& state) {
130147 text_style.font_families = std::vector<std::string>(1 , " Roboto" );
131148 text_style.color = SK_ColorBLACK;
132149
133- txt::ParagraphBuilderTxt builder (paragraph_style, GetTestFontCollection () );
150+ txt::ParagraphBuilderTxt builder (paragraph_style, font_collection_ );
134151
135152 builder.PushStyle (text_style);
136153 builder.AddText (u16_text);
@@ -141,9 +158,8 @@ static void BM_ParagraphJustifyLayout(benchmark::State& state) {
141158 paragraph->Layout (300 );
142159 }
143160}
144- BENCHMARK (BM_ParagraphJustifyLayout);
145161
146- static void BM_ParagraphManyStylesLayout (benchmark::State& state) {
162+ BENCHMARK_F (ParagraphFixture, ManyStylesLayout) (benchmark::State& state) {
147163 const char * text = " -" ;
148164 auto icu_text = icu::UnicodeString::fromUTF8 (text);
149165 std::u16string u16_text (icu_text.getBuffer (),
@@ -154,7 +170,7 @@ static void BM_ParagraphManyStylesLayout(benchmark::State& state) {
154170 txt::TextStyle text_style;
155171 text_style.font_families = std::vector<std::string>(1 , " Roboto" );
156172 text_style.color = SK_ColorBLACK;
157- txt::ParagraphBuilderTxt builder (paragraph_style, GetTestFontCollection () );
173+ txt::ParagraphBuilderTxt builder (paragraph_style, font_collection_ );
158174 for (int i = 0 ; i < 1000 ; ++i) {
159175 builder.PushStyle (text_style);
160176 builder.AddText (u16_text);
@@ -165,9 +181,8 @@ static void BM_ParagraphManyStylesLayout(benchmark::State& state) {
165181 paragraph->Layout (300 );
166182 }
167183}
168- BENCHMARK (BM_ParagraphManyStylesLayout);
169184
170- static void BM_ParagraphTextBigO (benchmark::State& state) {
185+ BENCHMARK_DEFINE_F (ParagraphFixture, TextBigO) (benchmark::State& state) {
171186 std::vector<uint16_t > text;
172187 for (uint16_t i = 0 ; i < state.range (0 ); ++i) {
173188 text.push_back (i % 5 == 0 ? ' ' : i);
@@ -181,7 +196,7 @@ static void BM_ParagraphTextBigO(benchmark::State& state) {
181196 text_style.font_families = std::vector<std::string>(1 , " Roboto" );
182197 text_style.color = SK_ColorBLACK;
183198
184- txt::ParagraphBuilderTxt builder (paragraph_style, GetTestFontCollection () );
199+ txt::ParagraphBuilderTxt builder (paragraph_style, font_collection_ );
185200
186201 builder.PushStyle (text_style);
187202 builder.AddText (u16_text);
@@ -193,12 +208,12 @@ static void BM_ParagraphTextBigO(benchmark::State& state) {
193208 }
194209 state.SetComplexityN (state.range (0 ));
195210}
196- BENCHMARK (BM_ParagraphTextBigO )
211+ BENCHMARK_REGISTER_F (ParagraphFixture, TextBigO )
197212 ->RangeMultiplier (4 )
198213 ->Range(1 << 6 , 1 << 14 )
199214 ->Complexity(benchmark::oN);
200215
201- static void BM_ParagraphStylesBigO (benchmark::State& state) {
216+ BENCHMARK_DEFINE_F (ParagraphFixture, StylesBigO) (benchmark::State& state) {
202217 const char * text = " vry shrt " ;
203218 auto icu_text = icu::UnicodeString::fromUTF8 (text);
204219 std::u16string u16_text (icu_text.getBuffer (),
@@ -210,7 +225,7 @@ static void BM_ParagraphStylesBigO(benchmark::State& state) {
210225 text_style.font_families = std::vector<std::string>(1 , " Roboto" );
211226 text_style.color = SK_ColorBLACK;
212227
213- txt::ParagraphBuilderTxt builder (paragraph_style, GetTestFontCollection () );
228+ txt::ParagraphBuilderTxt builder (paragraph_style, font_collection_ );
214229
215230 for (int i = 0 ; i < state.range (0 ); ++i) {
216231 builder.PushStyle (text_style);
@@ -223,12 +238,12 @@ static void BM_ParagraphStylesBigO(benchmark::State& state) {
223238 }
224239 state.SetComplexityN (state.range (0 ));
225240}
226- BENCHMARK (BM_ParagraphStylesBigO )
241+ BENCHMARK_REGISTER_F (ParagraphFixture, StylesBigO )
227242 ->RangeMultiplier (4 )
228243 ->Range(1 << 3 , 1 << 12 )
229244 ->Complexity(benchmark::oN);
230245
231- static void BM_ParagraphPaintSimple (benchmark::State& state) {
246+ BENCHMARK_F (ParagraphFixture, PaintSimple) (benchmark::State& state) {
232247 const char * text = " Hello world! This is a simple sentence to test drawing." ;
233248 auto icu_text = icu::UnicodeString::fromUTF8 (text);
234249 std::u16string u16_text (icu_text.getBuffer (),
@@ -239,25 +254,20 @@ static void BM_ParagraphPaintSimple(benchmark::State& state) {
239254 txt::TextStyle text_style;
240255 text_style.font_families = std::vector<std::string>(1 , " Roboto" );
241256 text_style.color = SK_ColorBLACK;
242- txt::ParagraphBuilderTxt builder (paragraph_style, GetTestFontCollection () );
257+ txt::ParagraphBuilderTxt builder (paragraph_style, font_collection_ );
243258 builder.PushStyle (text_style);
244259 builder.AddText (u16_text);
245260 auto paragraph = BuildParagraph (builder);
246261 paragraph->Layout (300 );
247262
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);
252263 int offset = 0 ;
253264 while (state.KeepRunning ()) {
254- paragraph->Paint (canvas .get (), offset % 700 , 10 );
265+ paragraph->Paint (canvas_ .get (), offset % 700 , 10 );
255266 offset++;
256267 }
257268}
258- BENCHMARK (BM_ParagraphPaintSimple);
259269
260- static void BM_ParagraphPaintLarge (benchmark::State& state) {
270+ BENCHMARK_F (ParagraphFixture, PaintLarge) (benchmark::State& state) {
261271 const char * text =
262272 " Hello world! This is a simple sentence to test drawing. Hello world! "
263273 " This is a simple sentence to test drawing. Hello world! This is a "
@@ -286,25 +296,20 @@ static void BM_ParagraphPaintLarge(benchmark::State& state) {
286296 txt::TextStyle text_style;
287297 text_style.font_families = std::vector<std::string>(1 , " Roboto" );
288298 text_style.color = SK_ColorBLACK;
289- txt::ParagraphBuilderTxt builder (paragraph_style, GetTestFontCollection () );
299+ txt::ParagraphBuilderTxt builder (paragraph_style, font_collection_ );
290300 builder.PushStyle (text_style);
291301 builder.AddText (u16_text);
292302 auto paragraph = BuildParagraph (builder);
293303 paragraph->Layout (300 );
294304
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);
299305 int offset = 0 ;
300306 while (state.KeepRunning ()) {
301- paragraph->Paint (canvas .get (), offset % 700 , 10 );
307+ paragraph->Paint (canvas_ .get (), offset % 700 , 10 );
302308 offset++;
303309 }
304310}
305- BENCHMARK (BM_ParagraphPaintLarge);
306311
307- static void BM_ParagraphPaintDecoration (benchmark::State& state) {
312+ BENCHMARK_F (ParagraphFixture, PaintDecoration) (benchmark::State& state) {
308313 const char * text =
309314 " Hello world! This is a simple sentence to test drawing. Hello world! "
310315 " This is a simple sentence to test drawing." ;
@@ -322,7 +327,7 @@ static void BM_ParagraphPaintDecoration(benchmark::State& state) {
322327 text_style.decoration_style = TextDecorationStyle (kSolid );
323328 text_style.color = SK_ColorBLACK;
324329
325- txt::ParagraphBuilderTxt builder (paragraph_style, GetTestFontCollection () );
330+ txt::ParagraphBuilderTxt builder (paragraph_style, font_collection_ );
326331
327332 builder.PushStyle (text_style);
328333 builder.AddText (u16_text);
@@ -338,17 +343,12 @@ static void BM_ParagraphPaintDecoration(benchmark::State& state) {
338343 auto paragraph = BuildParagraph (builder);
339344 paragraph->Layout (300 );
340345
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);
345346 int offset = 0 ;
346347 while (state.KeepRunning ()) {
347- paragraph->Paint (canvas .get (), offset % 700 , 10 );
348+ paragraph->Paint (canvas_ .get (), offset % 700 , 10 );
348349 offset++;
349350 }
350351}
351- BENCHMARK (BM_ParagraphPaintDecoration);
352352
353353// -----------------------------------------------------------------------------
354354//
@@ -357,7 +357,7 @@ BENCHMARK(BM_ParagraphPaintDecoration);
357357//
358358// -----------------------------------------------------------------------------
359359
360- static void BM_ParagraphMinikinDoLayout (benchmark::State& state) {
360+ BENCHMARK_DEFINE_F (ParagraphFixture, MinikinDoLayout) (benchmark::State& state) {
361361 std::vector<uint16_t > text;
362362 for (uint16_t i = 0 ; i < 16000 * 2 ; ++i) {
363363 text.push_back (i % 5 == 0 ? ' ' : i);
@@ -372,9 +372,8 @@ static void BM_ParagraphMinikinDoLayout(benchmark::State& state) {
372372 paint.letterSpacing = text_style.letter_spacing ;
373373 paint.wordSpacing = text_style.word_spacing ;
374374
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" );
378377
379378 while (state.KeepRunning ()) {
380379 minikin::Layout layout;
@@ -383,12 +382,12 @@ static void BM_ParagraphMinikinDoLayout(benchmark::State& state) {
383382 }
384383 state.SetComplexityN (state.range (0 ));
385384}
386- BENCHMARK (BM_ParagraphMinikinDoLayout )
385+ BENCHMARK_REGISTER_F (ParagraphFixture, MinikinDoLayout )
387386 ->RangeMultiplier (4 )
388387 ->Range(1 << 7 , 1 << 14 )
389388 ->Complexity(benchmark::oN);
390389
391- static void BM_ParagraphMinikinAddStyleRun (benchmark::State& state) {
390+ BENCHMARK_DEFINE_F (ParagraphFixture, AddStyleRun) (benchmark::State& state) {
392391 std::vector<uint16_t > text;
393392 for (uint16_t i = 0 ; i < 16000 * 2 ; ++i) {
394393 text.push_back (i % 5 == 0 ? ' ' : i);
@@ -403,8 +402,6 @@ static void BM_ParagraphMinikinAddStyleRun(benchmark::State& state) {
403402 paint.letterSpacing = text_style.letter_spacing ;
404403 paint.wordSpacing = text_style.word_spacing ;
405404
406- auto font_collection = GetTestFontCollection ();
407-
408405 minikin::LineBreaker breaker;
409406 breaker.setLocale (icu::Locale (), nullptr );
410407 breaker.resize (text.size ());
@@ -414,20 +411,20 @@ static void BM_ParagraphMinikinAddStyleRun(benchmark::State& state) {
414411 while (state.KeepRunning ()) {
415412 for (int i = 0 ; i < 20 ; ++i) {
416413 breaker.addStyleRun (&paint,
417- font_collection ->GetMinikinFontCollectionForFamilies (
414+ font_collection_ ->GetMinikinFontCollectionForFamilies (
418415 std::vector<std::string>(1 , " Roboto" ), " en-US" ),
419416 font, state.range (0 ) / 20 * i,
420417 state.range (0 ) / 20 * (i + 1 ), false );
421418 }
422419 }
423420 state.SetComplexityN (state.range (0 ));
424421}
425- BENCHMARK (BM_ParagraphMinikinAddStyleRun )
422+ BENCHMARK_REGISTER_F (ParagraphFixture, AddStyleRun )
426423 ->RangeMultiplier (4 )
427424 ->Range(1 << 7 , 1 << 14 )
428425 ->Complexity(benchmark::oN);
429426
430- static void BM_ParagraphSkTextBlobAlloc (benchmark::State& state) {
427+ BENCHMARK_DEFINE_F (ParagraphFixture, SkTextBlobAlloc) (benchmark::State& state) {
431428 SkFont font;
432429 font.setEdging (SkFont::Edging::kAntiAlias );
433430 font.setSize (14 );
@@ -439,7 +436,7 @@ static void BM_ParagraphSkTextBlobAlloc(benchmark::State& state) {
439436 }
440437 state.SetComplexityN (state.range (0 ));
441438}
442- BENCHMARK (BM_ParagraphSkTextBlobAlloc )
439+ BENCHMARK_REGISTER_F (ParagraphFixture, SkTextBlobAlloc )
443440 ->RangeMultiplier (4 )
444441 ->Range(1 << 7 , 1 << 14 )
445442 ->Complexity(benchmark::oN);
0 commit comments