libtxt: use a fixture in the benchmarks (flutter/engine#16531)

This commit is contained in:
Jason Simmons 2020-02-11 10:24:47 -08:00 committed by GitHub
parent 0f8ec47a01
commit 2af9eee72e

View File

@ -34,7 +34,26 @@
namespace txt {
static void BM_ParagraphShortLayout(benchmark::State& state) {
class ParagraphFixture : public benchmark::Fixture {
public:
void SetUp(const benchmark::State& state) {
font_collection_ = GetTestFontCollection();
bitmap_ = std::make_unique<SkBitmap>();
bitmap_->allocN32Pixels(1000, 1000);
canvas_ = std::make_unique<SkCanvas>(*bitmap_);
canvas_->clear(SK_ColorWHITE);
}
void TearDown(const benchmark::State& state) { font_collection_.reset(); }
protected:
std::shared_ptr<FontCollection> font_collection_;
std::unique_ptr<SkCanvas> canvas_;
std::unique_ptr<SkBitmap> bitmap_;
};
BENCHMARK_F(ParagraphFixture, ShortLayout)(benchmark::State& state) {
const char* text = "Hello World";
auto icu_text = icu::UnicodeString::fromUTF8(text);
std::u16string u16_text(icu_text.getBuffer(),
@ -45,7 +64,7 @@ static void BM_ParagraphShortLayout(benchmark::State& state) {
txt::TextStyle text_style;
text_style.font_families = std::vector<std::string>(1, "Roboto");
text_style.color = SK_ColorBLACK;
txt::ParagraphBuilderTxt builder(paragraph_style, GetTestFontCollection());
txt::ParagraphBuilderTxt builder(paragraph_style, font_collection_);
builder.PushStyle(text_style);
builder.AddText(u16_text);
@ -56,9 +75,8 @@ static void BM_ParagraphShortLayout(benchmark::State& state) {
paragraph->Layout(300);
}
}
BENCHMARK(BM_ParagraphShortLayout);
static void BM_ParagraphLongLayout(benchmark::State& state) {
BENCHMARK_F(ParagraphFixture, LongLayout)(benchmark::State& state) {
const char* text =
"This is a very long sentence to test if the text will properly wrap "
"around and go to the next line. Sometimes, short sentence. Longer "
@ -87,7 +105,7 @@ static void BM_ParagraphLongLayout(benchmark::State& state) {
text_style.font_families = std::vector<std::string>(1, "Roboto");
text_style.color = SK_ColorBLACK;
txt::ParagraphBuilderTxt builder(paragraph_style, GetTestFontCollection());
txt::ParagraphBuilderTxt builder(paragraph_style, font_collection_);
builder.PushStyle(text_style);
builder.AddText(u16_text);
@ -98,9 +116,8 @@ static void BM_ParagraphLongLayout(benchmark::State& state) {
paragraph->Layout(300);
}
}
BENCHMARK(BM_ParagraphLongLayout);
static void BM_ParagraphJustifyLayout(benchmark::State& state) {
BENCHMARK_F(ParagraphFixture, JustifyLayout)(benchmark::State& state) {
const char* text =
"This is a very long sentence to test if the text will properly wrap "
"around and go to the next line. Sometimes, short sentence. Longer "
@ -130,7 +147,7 @@ static void BM_ParagraphJustifyLayout(benchmark::State& state) {
text_style.font_families = std::vector<std::string>(1, "Roboto");
text_style.color = SK_ColorBLACK;
txt::ParagraphBuilderTxt builder(paragraph_style, GetTestFontCollection());
txt::ParagraphBuilderTxt builder(paragraph_style, font_collection_);
builder.PushStyle(text_style);
builder.AddText(u16_text);
@ -141,9 +158,8 @@ static void BM_ParagraphJustifyLayout(benchmark::State& state) {
paragraph->Layout(300);
}
}
BENCHMARK(BM_ParagraphJustifyLayout);
static void BM_ParagraphManyStylesLayout(benchmark::State& state) {
BENCHMARK_F(ParagraphFixture, ManyStylesLayout)(benchmark::State& state) {
const char* text = "-";
auto icu_text = icu::UnicodeString::fromUTF8(text);
std::u16string u16_text(icu_text.getBuffer(),
@ -154,7 +170,7 @@ static void BM_ParagraphManyStylesLayout(benchmark::State& state) {
txt::TextStyle text_style;
text_style.font_families = std::vector<std::string>(1, "Roboto");
text_style.color = SK_ColorBLACK;
txt::ParagraphBuilderTxt builder(paragraph_style, GetTestFontCollection());
txt::ParagraphBuilderTxt builder(paragraph_style, font_collection_);
for (int i = 0; i < 1000; ++i) {
builder.PushStyle(text_style);
builder.AddText(u16_text);
@ -165,9 +181,8 @@ static void BM_ParagraphManyStylesLayout(benchmark::State& state) {
paragraph->Layout(300);
}
}
BENCHMARK(BM_ParagraphManyStylesLayout);
static void BM_ParagraphTextBigO(benchmark::State& state) {
BENCHMARK_DEFINE_F(ParagraphFixture, TextBigO)(benchmark::State& state) {
std::vector<uint16_t> text;
for (uint16_t i = 0; i < state.range(0); ++i) {
text.push_back(i % 5 == 0 ? ' ' : i);
@ -181,7 +196,7 @@ static void BM_ParagraphTextBigO(benchmark::State& state) {
text_style.font_families = std::vector<std::string>(1, "Roboto");
text_style.color = SK_ColorBLACK;
txt::ParagraphBuilderTxt builder(paragraph_style, GetTestFontCollection());
txt::ParagraphBuilderTxt builder(paragraph_style, font_collection_);
builder.PushStyle(text_style);
builder.AddText(u16_text);
@ -193,12 +208,12 @@ static void BM_ParagraphTextBigO(benchmark::State& state) {
}
state.SetComplexityN(state.range(0));
}
BENCHMARK(BM_ParagraphTextBigO)
BENCHMARK_REGISTER_F(ParagraphFixture, TextBigO)
->RangeMultiplier(4)
->Range(1 << 6, 1 << 14)
->Complexity(benchmark::oN);
static void BM_ParagraphStylesBigO(benchmark::State& state) {
BENCHMARK_DEFINE_F(ParagraphFixture, StylesBigO)(benchmark::State& state) {
const char* text = "vry shrt ";
auto icu_text = icu::UnicodeString::fromUTF8(text);
std::u16string u16_text(icu_text.getBuffer(),
@ -210,7 +225,7 @@ static void BM_ParagraphStylesBigO(benchmark::State& state) {
text_style.font_families = std::vector<std::string>(1, "Roboto");
text_style.color = SK_ColorBLACK;
txt::ParagraphBuilderTxt builder(paragraph_style, GetTestFontCollection());
txt::ParagraphBuilderTxt builder(paragraph_style, font_collection_);
for (int i = 0; i < state.range(0); ++i) {
builder.PushStyle(text_style);
@ -223,12 +238,12 @@ static void BM_ParagraphStylesBigO(benchmark::State& state) {
}
state.SetComplexityN(state.range(0));
}
BENCHMARK(BM_ParagraphStylesBigO)
BENCHMARK_REGISTER_F(ParagraphFixture, StylesBigO)
->RangeMultiplier(4)
->Range(1 << 3, 1 << 12)
->Complexity(benchmark::oN);
static void BM_ParagraphPaintSimple(benchmark::State& state) {
BENCHMARK_F(ParagraphFixture, PaintSimple)(benchmark::State& state) {
const char* text = "Hello world! This is a simple sentence to test drawing.";
auto icu_text = icu::UnicodeString::fromUTF8(text);
std::u16string u16_text(icu_text.getBuffer(),
@ -239,25 +254,20 @@ static void BM_ParagraphPaintSimple(benchmark::State& state) {
txt::TextStyle text_style;
text_style.font_families = std::vector<std::string>(1, "Roboto");
text_style.color = SK_ColorBLACK;
txt::ParagraphBuilderTxt builder(paragraph_style, GetTestFontCollection());
txt::ParagraphBuilderTxt builder(paragraph_style, font_collection_);
builder.PushStyle(text_style);
builder.AddText(u16_text);
auto paragraph = BuildParagraph(builder);
paragraph->Layout(300);
std::unique_ptr<SkBitmap> bitmap = std::make_unique<SkBitmap>();
bitmap->allocN32Pixels(1000, 1000);
std::unique_ptr<SkCanvas> canvas = std::make_unique<SkCanvas>(*bitmap);
canvas->clear(SK_ColorWHITE);
int offset = 0;
while (state.KeepRunning()) {
paragraph->Paint(canvas.get(), offset % 700, 10);
paragraph->Paint(canvas_.get(), offset % 700, 10);
offset++;
}
}
BENCHMARK(BM_ParagraphPaintSimple);
static void BM_ParagraphPaintLarge(benchmark::State& state) {
BENCHMARK_F(ParagraphFixture, PaintLarge)(benchmark::State& state) {
const char* text =
"Hello world! This is a simple sentence to test drawing. Hello world! "
"This is a simple sentence to test drawing. Hello world! This is a "
@ -286,25 +296,20 @@ static void BM_ParagraphPaintLarge(benchmark::State& state) {
txt::TextStyle text_style;
text_style.font_families = std::vector<std::string>(1, "Roboto");
text_style.color = SK_ColorBLACK;
txt::ParagraphBuilderTxt builder(paragraph_style, GetTestFontCollection());
txt::ParagraphBuilderTxt builder(paragraph_style, font_collection_);
builder.PushStyle(text_style);
builder.AddText(u16_text);
auto paragraph = BuildParagraph(builder);
paragraph->Layout(300);
std::unique_ptr<SkBitmap> bitmap = std::make_unique<SkBitmap>();
bitmap->allocN32Pixels(1000, 1000);
std::unique_ptr<SkCanvas> canvas = std::make_unique<SkCanvas>(*bitmap);
canvas->clear(SK_ColorWHITE);
int offset = 0;
while (state.KeepRunning()) {
paragraph->Paint(canvas.get(), offset % 700, 10);
paragraph->Paint(canvas_.get(), offset % 700, 10);
offset++;
}
}
BENCHMARK(BM_ParagraphPaintLarge);
static void BM_ParagraphPaintDecoration(benchmark::State& state) {
BENCHMARK_F(ParagraphFixture, PaintDecoration)(benchmark::State& state) {
const char* text =
"Hello world! This is a simple sentence to test drawing. Hello world! "
"This is a simple sentence to test drawing.";
@ -322,7 +327,7 @@ static void BM_ParagraphPaintDecoration(benchmark::State& state) {
text_style.decoration_style = TextDecorationStyle(kSolid);
text_style.color = SK_ColorBLACK;
txt::ParagraphBuilderTxt builder(paragraph_style, GetTestFontCollection());
txt::ParagraphBuilderTxt builder(paragraph_style, font_collection_);
builder.PushStyle(text_style);
builder.AddText(u16_text);
@ -338,17 +343,12 @@ static void BM_ParagraphPaintDecoration(benchmark::State& state) {
auto paragraph = BuildParagraph(builder);
paragraph->Layout(300);
std::unique_ptr<SkBitmap> bitmap = std::make_unique<SkBitmap>();
bitmap->allocN32Pixels(1000, 1000);
std::unique_ptr<SkCanvas> canvas = std::make_unique<SkCanvas>(*bitmap);
canvas->clear(SK_ColorWHITE);
int offset = 0;
while (state.KeepRunning()) {
paragraph->Paint(canvas.get(), offset % 700, 10);
paragraph->Paint(canvas_.get(), offset % 700, 10);
offset++;
}
}
BENCHMARK(BM_ParagraphPaintDecoration);
// -----------------------------------------------------------------------------
//
@ -357,7 +357,7 @@ BENCHMARK(BM_ParagraphPaintDecoration);
//
// -----------------------------------------------------------------------------
static void BM_ParagraphMinikinDoLayout(benchmark::State& state) {
BENCHMARK_DEFINE_F(ParagraphFixture, MinikinDoLayout)(benchmark::State& state) {
std::vector<uint16_t> text;
for (uint16_t i = 0; i < 16000 * 2; ++i) {
text.push_back(i % 5 == 0 ? ' ' : i);
@ -372,9 +372,8 @@ static void BM_ParagraphMinikinDoLayout(benchmark::State& state) {
paint.letterSpacing = text_style.letter_spacing;
paint.wordSpacing = text_style.word_spacing;
auto collection =
GetTestFontCollection()->GetMinikinFontCollectionForFamilies(
text_style.font_families, "en-US");
auto collection = font_collection_->GetMinikinFontCollectionForFamilies(
text_style.font_families, "en-US");
while (state.KeepRunning()) {
minikin::Layout layout;
@ -383,12 +382,12 @@ static void BM_ParagraphMinikinDoLayout(benchmark::State& state) {
}
state.SetComplexityN(state.range(0));
}
BENCHMARK(BM_ParagraphMinikinDoLayout)
BENCHMARK_REGISTER_F(ParagraphFixture, MinikinDoLayout)
->RangeMultiplier(4)
->Range(1 << 7, 1 << 14)
->Complexity(benchmark::oN);
static void BM_ParagraphMinikinAddStyleRun(benchmark::State& state) {
BENCHMARK_DEFINE_F(ParagraphFixture, AddStyleRun)(benchmark::State& state) {
std::vector<uint16_t> text;
for (uint16_t i = 0; i < 16000 * 2; ++i) {
text.push_back(i % 5 == 0 ? ' ' : i);
@ -403,8 +402,6 @@ static void BM_ParagraphMinikinAddStyleRun(benchmark::State& state) {
paint.letterSpacing = text_style.letter_spacing;
paint.wordSpacing = text_style.word_spacing;
auto font_collection = GetTestFontCollection();
minikin::LineBreaker breaker;
breaker.setLocale(icu::Locale(), nullptr);
breaker.resize(text.size());
@ -414,7 +411,7 @@ static void BM_ParagraphMinikinAddStyleRun(benchmark::State& state) {
while (state.KeepRunning()) {
for (int i = 0; i < 20; ++i) {
breaker.addStyleRun(&paint,
font_collection->GetMinikinFontCollectionForFamilies(
font_collection_->GetMinikinFontCollectionForFamilies(
std::vector<std::string>(1, "Roboto"), "en-US"),
font, state.range(0) / 20 * i,
state.range(0) / 20 * (i + 1), false);
@ -422,12 +419,12 @@ static void BM_ParagraphMinikinAddStyleRun(benchmark::State& state) {
}
state.SetComplexityN(state.range(0));
}
BENCHMARK(BM_ParagraphMinikinAddStyleRun)
BENCHMARK_REGISTER_F(ParagraphFixture, AddStyleRun)
->RangeMultiplier(4)
->Range(1 << 7, 1 << 14)
->Complexity(benchmark::oN);
static void BM_ParagraphSkTextBlobAlloc(benchmark::State& state) {
BENCHMARK_DEFINE_F(ParagraphFixture, SkTextBlobAlloc)(benchmark::State& state) {
SkFont font;
font.setEdging(SkFont::Edging::kAntiAlias);
font.setSize(14);
@ -439,7 +436,7 @@ static void BM_ParagraphSkTextBlobAlloc(benchmark::State& state) {
}
state.SetComplexityN(state.range(0));
}
BENCHMARK(BM_ParagraphSkTextBlobAlloc)
BENCHMARK_REGISTER_F(ParagraphFixture, SkTextBlobAlloc)
->RangeMultiplier(4)
->Range(1 << 7, 1 << 14)
->Complexity(benchmark::oN);