Flutter Engine
paragraph_builder_benchmarks.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Google, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "flutter/fml/logging.h"
18 #include "flutter/third_party/txt/tests/txt_test_utils.h"
19 #include "third_party/benchmark/include/benchmark/benchmark_api.h"
20 #include "third_party/icu/source/common/unicode/unistr.h"
21 #include "third_party/skia/include/core/SkColor.h"
22 #include "txt/font_collection.h"
23 #include "txt/font_style.h"
24 #include "txt/font_weight.h"
25 #include "txt/paragraph.h"
27 
28 namespace txt {
29 
30 static void BM_ParagraphBuilderConstruction(benchmark::State& state) {
31  txt::ParagraphStyle paragraph_style;
32  auto font_collection = GetTestFontCollection();
33  while (state.KeepRunning()) {
34  txt::ParagraphBuilderTxt builder(paragraph_style, font_collection);
35  }
36 }
38 
39 static void BM_ParagraphBuilderPushStyle(benchmark::State& state) {
40  txt::ParagraphStyle paragraph_style;
41 
42  txt::TextStyle text_style;
43  text_style.color = SK_ColorBLACK;
44  auto font_collection = GetTestFontCollection();
45  while (state.KeepRunning()) {
46  txt::ParagraphBuilderTxt builder(paragraph_style, font_collection);
47  builder.PushStyle(text_style);
48  }
49 }
51 
52 static void BM_ParagraphBuilderPushPop(benchmark::State& state) {
53  txt::ParagraphStyle paragraph_style;
54  txt::ParagraphBuilderTxt builder(paragraph_style, GetTestFontCollection());
55 
56  txt::TextStyle text_style;
57  text_style.color = SK_ColorBLACK;
58  while (state.KeepRunning()) {
59  builder.PushStyle(text_style);
60  builder.Pop();
61  }
62 }
64 
65 static void BM_ParagraphBuilderAddTextString(benchmark::State& state) {
66  std::u16string text = u"Hello World";
67 
68  auto font_collection = GetTestFontCollection();
69 
70  txt::ParagraphStyle paragraph_style;
71 
72  while (state.KeepRunning()) {
73  txt::ParagraphBuilderTxt builder(paragraph_style, font_collection);
74  builder.AddText(text);
75  }
76 }
78 
79 static void BM_ParagraphBuilderAddTextChar(benchmark::State& state) {
80  std::u16string text = u"Hello World";
81 
82  txt::ParagraphStyle paragraph_style;
83  auto font_collection = GetTestFontCollection();
84  while (state.KeepRunning()) {
85  txt::ParagraphBuilderTxt builder(paragraph_style, font_collection);
86  builder.AddText(text);
87  }
88 }
90 
91 static void BM_ParagraphBuilderAddTextU16stringShort(benchmark::State& state) {
92  const char* text = "H";
93  auto icu_text = icu::UnicodeString::fromUTF8(text);
94  std::u16string u16_text(icu_text.getBuffer(),
95  icu_text.getBuffer() + icu_text.length());
96 
97  txt::ParagraphStyle paragraph_style;
98  auto font_collection = GetTestFontCollection();
99  while (state.KeepRunning()) {
100  txt::ParagraphBuilderTxt builder(paragraph_style, font_collection);
101  builder.AddText(u16_text);
102  }
103 }
105 
106 static void BM_ParagraphBuilderAddTextU16stringLong(benchmark::State& state) {
107  const char* text =
108  "This is a very long sentence to test if the text will properly wrap "
109  "around and go to the next line. Sometimes, short sentence. Longer "
110  "sentences are okay too because they are necessary. Very short. "
111  "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
112  "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
113  "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
114  "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
115  "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
116  "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
117  "mollit anim id est laborum. "
118  "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
119  "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
120  "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
121  "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
122  "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
123  "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
124  "mollit anim id est laborum.";
125  auto icu_text = icu::UnicodeString::fromUTF8(text);
126  std::u16string u16_text(icu_text.getBuffer(),
127  icu_text.getBuffer() + icu_text.length());
128 
129  auto font_collection = GetTestFontCollection();
130 
131  txt::ParagraphStyle paragraph_style;
132 
133  while (state.KeepRunning()) {
134  txt::ParagraphBuilderTxt builder(paragraph_style, font_collection);
135  builder.AddText(u16_text);
136  }
137 }
139 
141  benchmark::State& state) {
142  const char* text = "Hello World";
143  auto icu_text = icu::UnicodeString::fromUTF8(text);
144  std::u16string u16_text(icu_text.getBuffer(),
145  icu_text.getBuffer() + icu_text.length());
146 
147  txt::ParagraphStyle paragraph_style;
148 
149  txt::TextStyle text_style;
150  text_style.color = SK_ColorBLACK;
151  auto font_collection = GetTestFontCollection();
152  while (state.KeepRunning()) {
153  txt::ParagraphBuilderTxt builder(paragraph_style, font_collection);
154  builder.PushStyle(text_style);
155  builder.AddText(u16_text);
156  builder.Pop();
157  auto paragraph = builder.Build();
158  }
159 }
161 
162 static void BM_ParagraphBuilderLongParagraphConstruct(benchmark::State& state) {
163  const char* text =
164  "This is a very long sentence to test if the text will properly wrap "
165  "around and go to the next line. Sometimes, short sentence. Longer "
166  "sentences are okay too because they are necessary. Very short. "
167  "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
168  "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
169  "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
170  "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
171  "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
172  "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
173  "mollit anim id est laborum. "
174  "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
175  "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
176  "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
177  "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
178  "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
179  "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
180  "mollit anim id est laborum.";
181  auto icu_text = icu::UnicodeString::fromUTF8(text);
182  std::u16string u16_text(icu_text.getBuffer(),
183  icu_text.getBuffer() + icu_text.length());
184 
185  txt::ParagraphStyle paragraph_style;
186 
187  txt::TextStyle text_style;
188  text_style.color = SK_ColorBLACK;
189  auto font_collection = GetTestFontCollection();
190  while (state.KeepRunning()) {
191  txt::ParagraphBuilderTxt builder(paragraph_style, font_collection);
192  builder.PushStyle(text_style);
193  builder.AddText(u16_text);
194  builder.Pop();
195  auto paragraph = builder.Build();
196  }
197 }
199 
200 } // namespace txt
static void BM_ParagraphBuilderAddTextU16stringShort(benchmark::State &state)
static void BM_ParagraphBuilderAddTextU16stringLong(benchmark::State &state)
static void BM_ParagraphBuilderConstruction(benchmark::State &state)
std::shared_ptr< FontCollection > GetTestFontCollection()
virtual void Pop() override
BENCHMARK(BM_PaintRecordInit)
static void BM_ParagraphBuilderPushPop(benchmark::State &state)
static void BM_ParagraphBuilderPushStyle(benchmark::State &state)
static void BM_ParagraphBuilderAddTextChar(benchmark::State &state)
static void BM_ParagraphBuilderShortParagraphConstruct(benchmark::State &state)
SkColor color
Definition: text_style.h:36
virtual void AddText(const std::u16string &text) override
static void BM_ParagraphBuilderAddTextString(benchmark::State &state)
static void BM_ParagraphBuilderLongParagraphConstruct(benchmark::State &state)
virtual void PushStyle(const TextStyle &style) override
virtual std::unique_ptr< Paragraph > Build() override