1 /*
2  * Copyright (C) 2016 The Android Open Source Project
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 #include <benchmark/benchmark.h>
17 
18 #include <minikin/Hyphenator.h>
19 #include <util/FileUtils.h>
20 #include <util/UnicodeUtils.h>
21 
22 namespace minikin {
23 
24 const char* enUsHyph = "/system/usr/hyphen-data/hyph-en-us.hyb";
25 const int enUsMinPrefix = 2;
26 const int enUsMinSuffix = 3;
27 const icu::Locale& usLocale = icu::Locale::getUS();
28 
BM_Hyphenator_short_word(benchmark::State & state)29 static void BM_Hyphenator_short_word(benchmark::State& state) {
30     Hyphenator* hyphenator = Hyphenator::loadBinary(
31             readWholeFile(enUsHyph).data(), enUsMinPrefix, enUsMinSuffix);
32     std::vector<uint16_t> word = utf8ToUtf16("hyphen");
33     std::vector<HyphenationType> result;
34     while (state.KeepRunning()) {
35         hyphenator->hyphenate(&result, word.data(), word.size(), usLocale);
36     }
37     Hyphenator::loadBinary(nullptr, 2, 2);
38 }
39 
40 // TODO: Use BENCHMARK_CAPTURE for parametrise.
41 BENCHMARK(BM_Hyphenator_short_word);
42 
BM_Hyphenator_long_word(benchmark::State & state)43 static void BM_Hyphenator_long_word(benchmark::State& state) {
44     Hyphenator* hyphenator = Hyphenator::loadBinary(
45             readWholeFile(enUsHyph).data(), enUsMinPrefix, enUsMinSuffix);
46     std::vector<uint16_t> word = utf8ToUtf16(
47             "Pneumonoultramicroscopicsilicovolcanoconiosis");
48     std::vector<HyphenationType> result;
49     while (state.KeepRunning()) {
50         hyphenator->hyphenate(&result, word.data(), word.size(), usLocale);
51     }
52     Hyphenator::loadBinary(nullptr, 2, 2);
53 }
54 
55 // TODO: Use BENCHMARK_CAPTURE for parametrise.
56 BENCHMARK(BM_Hyphenator_long_word);
57 
58 // TODO: Add more tests for other languages.
59 
60 }  // namespace minikin
61