From 7e02287cf9dc6f88513249f6928036a43dedd407 Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Wed, 28 Jun 2017 18:16:37 -0700 Subject: [PATCH] Add additional benchmarks and initial bigO checks. Change-Id: I3d2d9186fb187d55319c10260bf7e0d579c65a6f --- engine/src/flutter/benchmarks/BUILD.gn | 2 +- .../benchmarks/paragraph_benchmarks.cc | 139 ++++++++++++++++-- .../paragraph_builder_benchmarks.cc | 14 +- .../benchmarks/styled_runs_benchmarks.cc | 39 +++++ 4 files changed, 170 insertions(+), 24 deletions(-) create mode 100644 engine/src/flutter/benchmarks/styled_runs_benchmarks.cc diff --git a/engine/src/flutter/benchmarks/BUILD.gn b/engine/src/flutter/benchmarks/BUILD.gn index 7a3f14d7d73..3b637cc4c34 100644 --- a/engine/src/flutter/benchmarks/BUILD.gn +++ b/engine/src/flutter/benchmarks/BUILD.gn @@ -25,6 +25,7 @@ executable("benchmarks") { "font_collection_benchmarks.cc", "paragraph_benchmarks.cc", "paragraph_builder_benchmarks.cc", + "styled_runs_benchmarks.cc", "txt_run_all_benchmarks.cc", "utils.cc", "utils.h", @@ -41,5 +42,4 @@ executable("benchmarks") { "//lib/txt/libs/minikin", ] - } diff --git a/engine/src/flutter/benchmarks/paragraph_benchmarks.cc b/engine/src/flutter/benchmarks/paragraph_benchmarks.cc index 34d44ad5576..7a694f6eb51 100644 --- a/engine/src/flutter/benchmarks/paragraph_benchmarks.cc +++ b/engine/src/flutter/benchmarks/paragraph_benchmarks.cc @@ -16,8 +16,12 @@ #include "third_party/benchmark/include/benchmark/benchmark_api.h" +#include #include "lib/ftl/command_line.h" #include "lib/ftl/logging.h" +#include "lib/txt/libs/minikin/LayoutUtils.h" +#include "lib/txt/src/font_collection.h" +#include "lib/txt/src/font_skia.h" #include "lib/txt/src/font_style.h" #include "lib/txt/src/font_weight.h" #include "lib/txt/src/paragraph.h" @@ -35,17 +39,18 @@ static void BM_ParagraphShortLayout(benchmark::State& state) { std::u16string u16_text(icu_text.getBuffer(), icu_text.getBuffer() + icu_text.length()); - while (state.KeepRunning()) { - txt::ParagraphStyle paragraph_style; - txt::ParagraphBuilder builder(paragraph_style); + txt::ParagraphStyle paragraph_style; - txt::TextStyle text_style; - text_style.color = SK_ColorBLACK; + txt::TextStyle text_style; + text_style.color = SK_ColorBLACK; + while (state.KeepRunning()) { + txt::ParagraphBuilder builder(paragraph_style); builder.PushStyle(text_style); builder.AddText(u16_text); builder.Pop(); auto paragraph = builder.Build(); + paragraph->Layout(300, txt::GetFontDir(), true); } } @@ -75,16 +80,17 @@ static void BM_ParagraphLongLayout(benchmark::State& state) { icu_text.getBuffer() + icu_text.length()); txt::ParagraphStyle paragraph_style; - txt::ParagraphBuilder builder(paragraph_style); txt::TextStyle text_style; text_style.color = SK_ColorBLACK; - - builder.PushStyle(text_style); - builder.AddText(u16_text); - builder.Pop(); - auto paragraph = builder.Build(); while (state.KeepRunning()) { + txt::ParagraphBuilder builder(paragraph_style); + + builder.PushStyle(text_style); + builder.AddText(u16_text); + builder.Pop(); + auto paragraph = builder.Build(); + paragraph->Layout(300, txt::GetFontDir(), true); } } @@ -96,13 +102,12 @@ static void BM_ParagraphManyStylesLayout(benchmark::State& state) { std::u16string u16_text(icu_text.getBuffer(), icu_text.getBuffer() + icu_text.length()); + txt::ParagraphStyle paragraph_style; + + txt::TextStyle text_style; + text_style.color = SK_ColorBLACK; while (state.KeepRunning()) { - txt::ParagraphStyle paragraph_style; txt::ParagraphBuilder builder(paragraph_style); - - txt::TextStyle text_style; - text_style.color = SK_ColorBLACK; - for (int i = 0; i < 100; ++i) { builder.PushStyle(text_style); builder.AddText(u16_text); @@ -113,4 +118,106 @@ static void BM_ParagraphManyStylesLayout(benchmark::State& state) { } BENCHMARK(BM_ParagraphManyStylesLayout); +static void BM_ParagraphTextBigO(benchmark::State& state) { + std::string text(state.range(0), '-'); + auto icu_text = icu::UnicodeString::fromUTF8(text); + std::u16string u16_text(icu_text.getBuffer(), + icu_text.getBuffer() + icu_text.length()); + + txt::ParagraphStyle paragraph_style; + + txt::TextStyle text_style; + text_style.color = SK_ColorBLACK; + while (state.KeepRunning()) { + txt::ParagraphBuilder builder(paragraph_style); + + builder.PushStyle(text_style); + builder.AddText(u16_text); + builder.Pop(); + auto paragraph = builder.Build(); + + paragraph->Layout(300, txt::GetFontDir(), true); + } + state.SetComplexityN(state.range(0)); +} +BENCHMARK(BM_ParagraphTextBigO) + ->RangeMultiplier(20) + ->Range(1 << 4, 1 << 12) + ->Complexity(benchmark::oN); + +static void BM_ParagraphStylesBigO(benchmark::State& state) { + const char* text = "A short sentence. "; + auto icu_text = icu::UnicodeString::fromUTF8(text); + std::u16string u16_text(icu_text.getBuffer(), + icu_text.getBuffer() + icu_text.length()); + + txt::ParagraphStyle paragraph_style; + + txt::TextStyle text_style; + text_style.color = SK_ColorBLACK; + while (state.KeepRunning()) { + txt::ParagraphBuilder builder(paragraph_style); + + for (int i = 0; i < state.range(0); ++i) { + builder.PushStyle(text_style); + builder.AddText(u16_text); + } + auto paragraph = builder.Build(); + paragraph->Layout(300, txt::GetFontDir(), true); + } + state.SetComplexityN(state.range(0)); +} +BENCHMARK(BM_ParagraphStylesBigO) + ->RangeMultiplier(20) + ->Range(1 << 2, 1 << 8) + ->Complexity(benchmark::oN); + +// ----------------------------------------------------------------------------- +// +// The following benchmarks break down the layout function and attempts to time +// each of the components to more finely attribute latency. +// +// ----------------------------------------------------------------------------- + +static void BM_ParagraphMinikinDoLayout(benchmark::State& state) { + std::vector text; + for (uint16_t i = 0; i < 100; ++i) { + text.push_back(i); + } + minikin::FontStyle font; + txt::TextStyle text_style; + text_style.font_family = "Roboto"; + minikin::MinikinPaint paint; + + font = minikin::FontStyle(4, false); + paint.size = text_style.font_size; + paint.letterSpacing = text_style.letter_spacing; + paint.wordSpacing = text_style.word_spacing; + + auto collection = + FontCollection::GetFontCollection(txt::GetFontDir()) + .GetMinikinFontCollectionForFamily(text_style.font_family); + + while (state.KeepRunning()) { + minikin::Layout layout; + layout.doLayout(text.data(), 0, 50, text.size(), 0, font, paint, + collection); + } +} +BENCHMARK(BM_ParagraphMinikinDoLayout); + +static void BM_ParagraphSkTextBlobAlloc(benchmark::State& state) { + SkPaint paint; + paint.setAntiAlias(true); + paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); + paint.setTextSize(14); + paint.setFakeBoldText(false); + + while (state.KeepRunning()) { + SkTextBlobBuilder builder; + builder.allocRunPos(paint, 100); + } +} +BENCHMARK(BM_ParagraphSkTextBlobAlloc); + } // namespace txt diff --git a/engine/src/flutter/benchmarks/paragraph_builder_benchmarks.cc b/engine/src/flutter/benchmarks/paragraph_builder_benchmarks.cc index 88da483736c..e91ba4416d4 100644 --- a/engine/src/flutter/benchmarks/paragraph_builder_benchmarks.cc +++ b/engine/src/flutter/benchmarks/paragraph_builder_benchmarks.cc @@ -37,11 +37,11 @@ BENCHMARK(BM_ParagraphBuilderConstruction); static void BM_ParagraphBuilderPushStyle(benchmark::State& state) { txt::ParagraphStyle paragraph_style; - txt::ParagraphBuilder builder(paragraph_style); txt::TextStyle text_style; text_style.color = SK_ColorBLACK; while (state.KeepRunning()) { + txt::ParagraphBuilder builder(paragraph_style); builder.PushStyle(text_style); } } @@ -64,9 +64,9 @@ static void BM_ParagraphBuilderAddTextString(benchmark::State& state) { std::string text = "Hello World"; txt::ParagraphStyle paragraph_style; - txt::ParagraphBuilder builder(paragraph_style); while (state.KeepRunning()) { + txt::ParagraphBuilder builder(paragraph_style); builder.AddText(text); } } @@ -76,9 +76,9 @@ static void BM_ParagraphBuilderAddTextChar(benchmark::State& state) { const char* text = "Hello World"; txt::ParagraphStyle paragraph_style; - txt::ParagraphBuilder builder(paragraph_style); while (state.KeepRunning()) { + txt::ParagraphBuilder builder(paragraph_style); builder.AddText(text); } } @@ -91,9 +91,9 @@ static void BM_ParagraphBuilderAddTextU16stringShort(benchmark::State& state) { icu_text.getBuffer() + icu_text.length()); txt::ParagraphStyle paragraph_style; - txt::ParagraphBuilder builder(paragraph_style); while (state.KeepRunning()) { + txt::ParagraphBuilder builder(paragraph_style); builder.AddText(u16_text); } } @@ -123,9 +123,9 @@ static void BM_ParagraphBuilderAddTextU16stringLong(benchmark::State& state) { icu_text.getBuffer() + icu_text.length()); txt::ParagraphStyle paragraph_style; - txt::ParagraphBuilder builder(paragraph_style); while (state.KeepRunning()) { + txt::ParagraphBuilder builder(paragraph_style); builder.AddText(u16_text); } } @@ -139,12 +139,12 @@ static void BM_ParagraphBuilderShortParagraphConstruct( icu_text.getBuffer() + icu_text.length()); txt::ParagraphStyle paragraph_style; - txt::ParagraphBuilder builder(paragraph_style); txt::TextStyle text_style; text_style.color = SK_ColorBLACK; while (state.KeepRunning()) { + txt::ParagraphBuilder builder(paragraph_style); builder.PushStyle(text_style); builder.AddText(u16_text); builder.Pop(); @@ -177,12 +177,12 @@ static void BM_ParagraphBuilderLongParagraphConstruct(benchmark::State& state) { icu_text.getBuffer() + icu_text.length()); txt::ParagraphStyle paragraph_style; - txt::ParagraphBuilder builder(paragraph_style); txt::TextStyle text_style; text_style.color = SK_ColorBLACK; while (state.KeepRunning()) { + txt::ParagraphBuilder builder(paragraph_style); builder.PushStyle(text_style); builder.AddText(u16_text); builder.Pop(); diff --git a/engine/src/flutter/benchmarks/styled_runs_benchmarks.cc b/engine/src/flutter/benchmarks/styled_runs_benchmarks.cc new file mode 100644 index 00000000000..b0531c2da09 --- /dev/null +++ b/engine/src/flutter/benchmarks/styled_runs_benchmarks.cc @@ -0,0 +1,39 @@ +/* + * Copyright 2017 Google, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "third_party/benchmark/include/benchmark/benchmark_api.h" + +#include "lib/ftl/command_line.h" +#include "lib/ftl/logging.h" +#include "lib/txt/src/styled_runs.h" +#include "lib/txt/src/text_style.h" +#include "utils.h" + +namespace txt { + +static void BM_StyledRunsGetRun(benchmark::State& state) { + StyledRuns runs; + TextStyle style; + runs.AddStyle(style); + runs.StartRun(0, 0); + runs.EndRunIfNeeded(11); + while (state.KeepRunning()) { + runs.GetRun(0); + } +} +BENCHMARK(BM_StyledRunsGetRun); + +} // namespace txt