From df66baa6fa6461e7acd1e3521c6a0a66a0f6d6a1 Mon Sep 17 00:00:00 2001 From: Seigo Nonaka Date: Tue, 14 Jun 2016 15:43:57 +0900 Subject: [PATCH] Add more native perf tests to minikin. This CL introduces performance tests for following three modules: - Hyphenator - WordBreaker - GraphemeBreak During using UnicodeUtils, need to decouple it from gtest since perftest doesn't have gtest dependencies. Bug:29142734 Change-Id: I700c662fa7d0a52f19d8e93150ad1a85dc28769f --- engine/src/flutter/tests/perftests/Android.mk | 9 +++ .../flutter/tests/perftests/GraphemeBreak.cpp | 79 +++++++++++++++++++ .../flutter/tests/perftests/Hyphenator.cpp | 55 +++++++++++++ .../flutter/tests/perftests/WordBreaker.cpp | 39 +++++++++ engine/src/flutter/tests/perftests/main.cpp | 30 ++++++- engine/src/flutter/tests/unittest/Android.mk | 1 + .../tests/unittest/UnicodeUtilsTest.cpp | 37 +++++++++ engine/src/flutter/tests/util/FileUtils.cpp | 35 ++++++++ engine/src/flutter/tests/util/FileUtils.h | 18 +++++ .../src/flutter/tests/util/UnicodeUtils.cpp | 64 ++++++++------- engine/src/flutter/tests/util/UnicodeUtils.h | 3 + 11 files changed, 341 insertions(+), 29 deletions(-) create mode 100644 engine/src/flutter/tests/perftests/GraphemeBreak.cpp create mode 100644 engine/src/flutter/tests/perftests/Hyphenator.cpp create mode 100644 engine/src/flutter/tests/perftests/WordBreaker.cpp create mode 100644 engine/src/flutter/tests/unittest/UnicodeUtilsTest.cpp create mode 100644 engine/src/flutter/tests/util/FileUtils.cpp create mode 100644 engine/src/flutter/tests/util/FileUtils.h diff --git a/engine/src/flutter/tests/perftests/Android.mk b/engine/src/flutter/tests/perftests/Android.mk index 3b49036cee..d3ed600b6e 100644 --- a/engine/src/flutter/tests/perftests/Android.mk +++ b/engine/src/flutter/tests/perftests/Android.mk @@ -17,7 +17,12 @@ LOCAL_PATH := $(call my-dir) perftest_src_files := \ + ../util/FileUtils.cpp \ + ../util/UnicodeUtils.cpp \ FontLanguage.cpp \ + GraphemeBreak.cpp \ + Hyphenator.cpp \ + WordBreaker.cpp \ main.cpp include $(CLEAR_VARS) @@ -25,7 +30,11 @@ LOCAL_MODULE := minikin_perftests LOCAL_CPPFLAGS := -Werror -Wall -Wextra LOCAL_SRC_FILES := $(perftest_src_files) LOCAL_STATIC_LIBRARIES := libminikin +LOCAL_SHARED_LIBRARIES := \ + libicuuc \ + liblog LOCAL_C_INCLUDES := \ + $(LOCAL_PATH)/../ \ $(LOCAL_PATH)/../../libs/minikin \ external/harfbuzz_ng/src include $(BUILD_NATIVE_BENCHMARK) diff --git a/engine/src/flutter/tests/perftests/GraphemeBreak.cpp b/engine/src/flutter/tests/perftests/GraphemeBreak.cpp new file mode 100644 index 0000000000..4db8f75d3d --- /dev/null +++ b/engine/src/flutter/tests/perftests/GraphemeBreak.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * 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 + +#include + +#include "minikin/GraphemeBreak.h" +#include "util/UnicodeUtils.h" + +namespace minikin { + +const char* ASCII_TEST_STR = "'L' 'o' 'r' 'e' 'm' ' ' 'i' 'p' 's' 'u' 'm' '.'"; +// U+261D: WHITE UP POINTING INDEX +// U+1F3FD: EMOJI MODIFIER FITZPATRICK TYPE-4 +const char* EMOJI_TEST_STR = "U+261D U+1F3FD U+261D U+1F3FD U+261D U+1F3FD U+261D U+1F3FD"; +// U+1F1FA: REGIONAL INDICATOR SYMBOL LETTER U +// U+1F1F8: REGIONAL INDICATOR SYMBOL LETTER S +const char* FLAGS_TEST_STR = "U+1F1FA U+1F1F8 U+1F1FA U+1F1F8 U+1F1FA U+1F1F8"; + +// TODO: Migrate BENCHMARK_CAPTURE for parameterizing. +static void BM_GraphemeBreak_Ascii(benchmark::State& state) { + size_t result_size; + uint16_t buffer[12]; + ParseUnicode(buffer, 12, ASCII_TEST_STR, &result_size, nullptr); + LOG_ALWAYS_FATAL_IF(result_size != 12); + const size_t testIndex = state.range_x(); + while (state.KeepRunning()) { + GraphemeBreak::isGraphemeBreak(buffer, 0, result_size, testIndex); + } +} +BENCHMARK(BM_GraphemeBreak_Ascii) + ->Arg(0) // Begining of the text. + ->Arg(1) // Middle of the text. + ->Arg(12); // End of the text. + +static void BM_GraphemeBreak_Emoji(benchmark::State& state) { + size_t result_size; + uint16_t buffer[12]; + ParseUnicode(buffer, 12, EMOJI_TEST_STR, &result_size, nullptr); + LOG_ALWAYS_FATAL_IF(result_size != 12); + const size_t testIndex = state.range_x(); + while (state.KeepRunning()) { + GraphemeBreak::isGraphemeBreak(buffer, 0, result_size, testIndex); + } +} +BENCHMARK(BM_GraphemeBreak_Emoji) + ->Arg(1) // Middle of emoji modifier sequence. + ->Arg(2) // Middle of the surrogate pairs. + ->Arg(3); // After emoji modifier sequence. Here is boundary of grapheme cluster. + +static void BM_GraphemeBreak_Emoji_Flags(benchmark::State& state) { + size_t result_size; + uint16_t buffer[12]; + ParseUnicode(buffer, 12, FLAGS_TEST_STR, &result_size, nullptr); + LOG_ALWAYS_FATAL_IF(result_size != 12); + const size_t testIndex = state.range_x(); + while (state.KeepRunning()) { + GraphemeBreak::isGraphemeBreak(buffer, 0, result_size, testIndex); + } +} +BENCHMARK(BM_GraphemeBreak_Emoji_Flags) + ->Arg(2) // Middle of flag sequence. + ->Arg(4) // After flag sequence. Here is boundary of grapheme cluster. + ->Arg(10); // Middle of 3rd flag sequence. + +} // namespace minikin diff --git a/engine/src/flutter/tests/perftests/Hyphenator.cpp b/engine/src/flutter/tests/perftests/Hyphenator.cpp new file mode 100644 index 0000000000..692e06d2bf --- /dev/null +++ b/engine/src/flutter/tests/perftests/Hyphenator.cpp @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * 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 + +#include +#include +#include + +namespace minikin { + +const char* enUsHyph = "/system/usr/hyphen-data/hyph-en-us.hyb"; + +static void BM_Hyphenator_short_word(benchmark::State& state) { + Hyphenator* hyphenator = Hyphenator::loadBinary(readWholeFile(enUsHyph).data()); + std::vector word = utf8ToUtf16("hyphen"); + std::vector result; + while (state.KeepRunning()) { + hyphenator->hyphenate(&result, word.data(), word.size()); + } + Hyphenator::loadBinary(nullptr); +} + +// TODO: Use BENCHMARK_CAPTURE for parametrise. +BENCHMARK(BM_Hyphenator_short_word); + +static void BM_Hyphenator_long_word(benchmark::State& state) { + Hyphenator* hyphenator = Hyphenator::loadBinary(readWholeFile(enUsHyph).data()); + std::vector word = utf8ToUtf16( + "Pneumonoultramicroscopicsilicovolcanoconiosis"); + std::vector result; + while (state.KeepRunning()) { + hyphenator->hyphenate(&result, word.data(), word.size()); + } + Hyphenator::loadBinary(nullptr); +} + +// TODO: Use BENCHMARK_CAPTURE for parametrise. +BENCHMARK(BM_Hyphenator_long_word); + +// TODO: Add more tests for other languages. + +} // namespace minikin diff --git a/engine/src/flutter/tests/perftests/WordBreaker.cpp b/engine/src/flutter/tests/perftests/WordBreaker.cpp new file mode 100644 index 0000000000..6758cf97e5 --- /dev/null +++ b/engine/src/flutter/tests/perftests/WordBreaker.cpp @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * 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 + +#include "minikin/WordBreaker.h" +#include "util/UnicodeUtils.h" + +namespace minikin { + +static void BM_WordBreaker_English(benchmark::State& state) { + const char* kLoremIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do " + "eiusmod tempor incididunt ut labore et dolore magna aliqua."; + + WordBreaker wb; + wb.setLocale(icu::Locale::getEnglish()); + std::vector text = utf8ToUtf16(kLoremIpsum); + while (state.KeepRunning()) { + wb.setText(text.data(), text.size()); + while (wb.next() != -1) {} + } +} +BENCHMARK(BM_WordBreaker_English); + +// TODO: Add more tests for other languages. + +} // namespace minikin diff --git a/engine/src/flutter/tests/perftests/main.cpp b/engine/src/flutter/tests/perftests/main.cpp index e39c7f8838..e6f9d14cda 100644 --- a/engine/src/flutter/tests/perftests/main.cpp +++ b/engine/src/flutter/tests/perftests/main.cpp @@ -15,4 +15,32 @@ */ #include -BENCHMARK_MAIN(); +#include + +#include +#include + +#include +#include +#include + +int main(int argc, char** argv) { + const char* fn = "/system/usr/icu/" U_ICUDATA_NAME ".dat"; + int fd = open(fn, O_RDONLY); + LOG_ALWAYS_FATAL_IF(fd == -1); + struct stat st; + LOG_ALWAYS_FATAL_IF(fstat(fd, &st) != 0); + void* data = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); + + UErrorCode errorCode = U_ZERO_ERROR; + udata_setCommonData(data, &errorCode); + LOG_ALWAYS_FATAL_IF(U_FAILURE(errorCode)); + u_init(&errorCode); + LOG_ALWAYS_FATAL_IF(U_FAILURE(errorCode)); + + benchmark::Initialize(&argc, argv); + benchmark::RunSpecifiedBenchmarks(); + + u_cleanup(); + return 0; +} diff --git a/engine/src/flutter/tests/unittest/Android.mk b/engine/src/flutter/tests/unittest/Android.mk index 885127199d..b43e3c8570 100644 --- a/engine/src/flutter/tests/unittest/Android.mk +++ b/engine/src/flutter/tests/unittest/Android.mk @@ -81,6 +81,7 @@ LOCAL_SRC_FILES += \ MinikinInternalTest.cpp \ GraphemeBreakTests.cpp \ LayoutUtilsTest.cpp \ + UnicodeUtilsTest.cpp \ WordBreakerTests.cpp LOCAL_C_INCLUDES := \ diff --git a/engine/src/flutter/tests/unittest/UnicodeUtilsTest.cpp b/engine/src/flutter/tests/unittest/UnicodeUtilsTest.cpp new file mode 100644 index 0000000000..99327235c5 --- /dev/null +++ b/engine/src/flutter/tests/unittest/UnicodeUtilsTest.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * 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 + +#include "UnicodeUtils.h" + +namespace minikin { + +TEST(UnicodeUtils, parse) { + const size_t BUF_SIZE = 256; + uint16_t buf[BUF_SIZE]; + size_t offset; + size_t size; + ParseUnicode(buf, BUF_SIZE, "U+000D U+1F431 | 'a'", &size, &offset); + EXPECT_EQ(size, 4u); + EXPECT_EQ(offset, 3u); + EXPECT_EQ(buf[0], 0x000D); + EXPECT_EQ(buf[1], 0xD83D); + EXPECT_EQ(buf[2], 0xDC31); + EXPECT_EQ(buf[3], 'a'); +} + +} // namespace minikin diff --git a/engine/src/flutter/tests/util/FileUtils.cpp b/engine/src/flutter/tests/util/FileUtils.cpp new file mode 100644 index 0000000000..dfe15225dd --- /dev/null +++ b/engine/src/flutter/tests/util/FileUtils.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * 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 + +#include +#include + +#include +#include + +std::vector readWholeFile(const std::string& filePath) { + FILE* fp = fopen(filePath.c_str(), "r"); + LOG_ALWAYS_FATAL_IF(fp == nullptr); + struct stat st; + LOG_ALWAYS_FATAL_IF(fstat(fileno(fp), &st) != 0); + + std::vector result(st.st_size); + LOG_ALWAYS_FATAL_IF(fread(result.data(), 1, st.st_size, fp) != st.st_size); + fclose(fp); + return result; +} diff --git a/engine/src/flutter/tests/util/FileUtils.h b/engine/src/flutter/tests/util/FileUtils.h new file mode 100644 index 0000000000..1e66d1b701 --- /dev/null +++ b/engine/src/flutter/tests/util/FileUtils.h @@ -0,0 +1,18 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * 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. + */ + +std::vector readWholeFile(const std::string& filePath); + diff --git a/engine/src/flutter/tests/util/UnicodeUtils.cpp b/engine/src/flutter/tests/util/UnicodeUtils.cpp index bb652ff63d..2f811daec2 100644 --- a/engine/src/flutter/tests/util/UnicodeUtils.cpp +++ b/engine/src/flutter/tests/util/UnicodeUtils.cpp @@ -14,9 +14,12 @@ * limitations under the License. */ -#include #include +#include #include +#include +#include +#include namespace minikin { @@ -32,33 +35,35 @@ void ParseUnicode(uint16_t* buf, size_t buf_size, const char* src, size_t* resul switch (src[input_ix]) { case '\'': // single ASCII char - ASSERT_LT(src[input_ix], 0x80); + LOG_ALWAYS_FATAL_IF(static_cast(src[input_ix]) >= 0x80); input_ix++; - ASSERT_NE(src[input_ix], 0); - ASSERT_LT(output_ix, buf_size); + LOG_ALWAYS_FATAL_IF(src[input_ix] == 0); + LOG_ALWAYS_FATAL_IF(output_ix >= buf_size); buf[output_ix++] = (uint16_t)src[input_ix++]; - ASSERT_EQ(src[input_ix], '\''); + LOG_ALWAYS_FATAL_IF(src[input_ix] != '\''); input_ix++; break; case 'u': case 'U': { // Unicode codepoint in hex syntax input_ix++; - ASSERT_EQ(src[input_ix], '+'); + LOG_ALWAYS_FATAL_IF(src[input_ix] != '+'); input_ix++; char* endptr = (char*)src + input_ix; unsigned long int codepoint = strtoul(src + input_ix, &endptr, 16); size_t num_hex_digits = endptr - (src + input_ix); - ASSERT_GE(num_hex_digits, 4u); // also triggers on invalid number syntax, digits = 0 - ASSERT_LE(num_hex_digits, 6u); - ASSERT_LE(codepoint, 0x10FFFFu); + + // also triggers on invalid number syntax, digits = 0 + LOG_ALWAYS_FATAL_IF(num_hex_digits < 4u); + LOG_ALWAYS_FATAL_IF(num_hex_digits > 6u); + LOG_ALWAYS_FATAL_IF(codepoint > 0x10FFFFu); input_ix += num_hex_digits; if (U16_LENGTH(codepoint) == 1) { - ASSERT_LE(output_ix + 1, buf_size); + LOG_ALWAYS_FATAL_IF(output_ix + 1 > buf_size); buf[output_ix++] = codepoint; } else { // UTF-16 encoding - ASSERT_LE(output_ix + 2, buf_size); + LOG_ALWAYS_FATAL_IF(output_ix + 2 > buf_size); buf[output_ix++] = U16_LEAD(codepoint); buf[output_ix++] = U16_TRAIL(codepoint); } @@ -68,33 +73,36 @@ void ParseUnicode(uint16_t* buf, size_t buf_size, const char* src, size_t* resul input_ix++; break; case '|': - ASSERT_FALSE(seen_offset); - ASSERT_NE(offset, nullptr); + LOG_ALWAYS_FATAL_IF(seen_offset); + LOG_ALWAYS_FATAL_IF(offset == nullptr); *offset = output_ix; seen_offset = true; input_ix++; break; default: - FAIL(); // unexpected character + LOG_ALWAYS_FATAL("Unexpected Character"); } } - ASSERT_NE(result_size, nullptr); + LOG_ALWAYS_FATAL_IF(result_size == nullptr); *result_size = output_ix; - ASSERT_TRUE(seen_offset || offset == nullptr); + LOG_ALWAYS_FATAL_IF(!seen_offset && offset != nullptr); } -TEST(UnicodeUtils, parse) { - const size_t BUF_SIZE = 256; - uint16_t buf[BUF_SIZE]; - size_t offset; - size_t size; - ParseUnicode(buf, BUF_SIZE, "U+000D U+1F431 | 'a'", &size, &offset); - EXPECT_EQ(size, 4u); - EXPECT_EQ(offset, 3u); - EXPECT_EQ(buf[0], 0x000D); - EXPECT_EQ(buf[1], 0xD83D); - EXPECT_EQ(buf[2], 0xDC31); - EXPECT_EQ(buf[3], 'a'); +std::vector utf8ToUtf16(const std::string& text) { + std::vector result; + int32_t i = 0; + const int32_t textLength = static_cast(text.size()); + uint32_t c = 0; + while (i < textLength) { + U8_NEXT(text.c_str(), i, textLength, c); + if (U16_LENGTH(c) == 1) { + result.push_back(c); + } else { + result.push_back(U16_LEAD(c)); + result.push_back(U16_TRAIL(c)); + } + } + return result; } } // namespace minikin diff --git a/engine/src/flutter/tests/util/UnicodeUtils.h b/engine/src/flutter/tests/util/UnicodeUtils.h index 64b90c91b1..571be7a31e 100644 --- a/engine/src/flutter/tests/util/UnicodeUtils.h +++ b/engine/src/flutter/tests/util/UnicodeUtils.h @@ -19,4 +19,7 @@ namespace minikin { void ParseUnicode(uint16_t* buf, size_t buf_size, const char* src, size_t* result_size, size_t* offset); +// Converts UTF-8 to UTF-16. +std::vector utf8ToUtf16(const std::string& text); + } // namespace minikin