xref: /aosp_15_r20/frameworks/minikin/tests/perftests/Hyphenator.cpp (revision 834a2baab5fdfc28e9a428ee87c7ea8f6a06a53d)
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 "minikin/Hyphenator.h"
17 
18 #include <benchmark/benchmark.h>
19 
20 #include "FileUtils.h"
21 #include "UnicodeUtils.h"
22 
23 namespace minikin {
24 
25 const char* enUsHyph = "/system/usr/hyphen-data/hyph-en-us.hyb";
26 const int enUsMinPrefix = 2;
27 const int enUsMinSuffix = 3;
28 
BM_Hyphenator_short_word(benchmark::State & state)29 static void BM_Hyphenator_short_word(benchmark::State& state) {
30     auto hyData = readWholeFile(enUsHyph);
31     Hyphenator* hyphenator = Hyphenator::loadBinary(hyData.data(), hyData.size(), enUsMinPrefix,
32                                                     enUsMinSuffix, "en");
33     std::vector<uint16_t> word = utf8ToUtf16("hyphen");
34     std::vector<HyphenationType> result;
35     while (state.KeepRunning()) {
36         hyphenator->hyphenate(word, &result);
37     }
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     auto hyData = readWholeFile(enUsHyph);
45     Hyphenator* hyphenator = Hyphenator::loadBinary(hyData.data(), hyData.size(), enUsMinPrefix,
46                                                     enUsMinSuffix, "en");
47     std::vector<uint16_t> word = utf8ToUtf16("Pneumonoultramicroscopicsilicovolcanoconiosis");
48     std::vector<HyphenationType> result;
49     while (state.KeepRunning()) {
50         hyphenator->hyphenate(word, &result);
51     }
52 }
53 
54 // TODO: Use BENCHMARK_CAPTURE for parametrise.
55 BENCHMARK(BM_Hyphenator_long_word);
56 
57 // TODO: Add more tests for other languages.
58 
59 }  // namespace minikin
60