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
This commit is contained in:
Seigo Nonaka
2016-06-14 15:43:57 +09:00
parent c3b9f7dadd
commit df66baa6fa
11 changed files with 341 additions and 29 deletions

View File

@@ -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)

View File

@@ -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 <benchmark/benchmark.h>
#include <cutils/log.h>
#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

View File

@@ -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 <benchmark/benchmark.h>
#include <minikin/Hyphenator.h>
#include <util/FileUtils.h>
#include <util/UnicodeUtils.h>
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<uint16_t> word = utf8ToUtf16("hyphen");
std::vector<uint8_t> 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<uint16_t> word = utf8ToUtf16(
"Pneumonoultramicroscopicsilicovolcanoconiosis");
std::vector<uint8_t> 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

View File

@@ -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 <benchmark/benchmark.h>
#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<uint16_t> 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

View File

@@ -15,4 +15,32 @@
*/
#include <benchmark/benchmark.h>
BENCHMARK_MAIN();
#include <cutils/log.h>
#include <unicode/uclean.h>
#include <unicode/udata.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
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;
}

View File

@@ -81,6 +81,7 @@ LOCAL_SRC_FILES += \
MinikinInternalTest.cpp \
GraphemeBreakTests.cpp \
LayoutUtilsTest.cpp \
UnicodeUtilsTest.cpp \
WordBreakerTests.cpp
LOCAL_C_INCLUDES := \

View File

@@ -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 <gtest/gtest.h>
#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

View File

@@ -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 <cutils/log.h>
#include <stdio.h>
#include <sys/stat.h>
#include <string>
#include <vector>
std::vector<uint8_t> 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<uint8_t> result(st.st_size);
LOG_ALWAYS_FATAL_IF(fread(result.data(), 1, st.st_size, fp) != st.st_size);
fclose(fp);
return result;
}

View File

@@ -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<uint8_t> readWholeFile(const std::string& filePath);

View File

@@ -14,9 +14,12 @@
* limitations under the License.
*/
#include <gtest/gtest.h>
#include <unicode/utf.h>
#include <unicode/utf8.h>
#include <cstdlib>
#include <cutils/log.h>
#include <vector>
#include <string>
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<uint8_t>(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<uint16_t> utf8ToUtf16(const std::string& text) {
std::vector<uint16_t> result;
int32_t i = 0;
const int32_t textLength = static_cast<int32_t>(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

View File

@@ -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<uint16_t> utf8ToUtf16(const std::string& text);
} // namespace minikin