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 <cutils/log.h>
19
20 #include "minikin/GraphemeBreak.h"
21 #include "UnicodeUtils.h"
22
23 namespace minikin {
24
25 const char* ASCII_TEST_STR = "'L' 'o' 'r' 'e' 'm' ' ' 'i' 'p' 's' 'u' 'm' '.'";
26 // U+261D: WHITE UP POINTING INDEX
27 // U+1F3FD: EMOJI MODIFIER FITZPATRICK TYPE-4
28 const char* EMOJI_TEST_STR = "U+261D U+1F3FD U+261D U+1F3FD U+261D U+1F3FD U+261D U+1F3FD";
29 // U+1F1FA: REGIONAL INDICATOR SYMBOL LETTER U
30 // U+1F1F8: REGIONAL INDICATOR SYMBOL LETTER S
31 const char* FLAGS_TEST_STR = "U+1F1FA U+1F1F8 U+1F1FA U+1F1F8 U+1F1FA U+1F1F8";
32
33 // TODO: Migrate BENCHMARK_CAPTURE for parameterizing.
BM_GraphemeBreak_Ascii(benchmark::State & state)34 static void BM_GraphemeBreak_Ascii(benchmark::State& state) {
35 size_t result_size;
36 uint16_t buffer[12];
37 ParseUnicode(buffer, 12, ASCII_TEST_STR, &result_size, nullptr);
38 LOG_ALWAYS_FATAL_IF(result_size != 12);
39 const size_t testIndex = state.range(0);
40 while (state.KeepRunning()) {
41 GraphemeBreak::isGraphemeBreak(nullptr, buffer, 0, result_size, testIndex);
42 }
43 }
44 BENCHMARK(BM_GraphemeBreak_Ascii)
45 ->Arg(0) // Begining of the text.
46 ->Arg(1) // Middle of the text.
47 ->Arg(12); // End of the text.
48
BM_GraphemeBreak_Emoji(benchmark::State & state)49 static void BM_GraphemeBreak_Emoji(benchmark::State& state) {
50 size_t result_size;
51 uint16_t buffer[12];
52 ParseUnicode(buffer, 12, EMOJI_TEST_STR, &result_size, nullptr);
53 LOG_ALWAYS_FATAL_IF(result_size != 12);
54 const size_t testIndex = state.range(0);
55 while (state.KeepRunning()) {
56 GraphemeBreak::isGraphemeBreak(nullptr, buffer, 0, result_size, testIndex);
57 }
58 }
59 BENCHMARK(BM_GraphemeBreak_Emoji)
60 ->Arg(1) // Middle of emoji modifier sequence.
61 ->Arg(2) // Middle of the surrogate pairs.
62 ->Arg(3); // After emoji modifier sequence. Here is boundary of grapheme cluster.
63
BM_GraphemeBreak_Emoji_Flags(benchmark::State & state)64 static void BM_GraphemeBreak_Emoji_Flags(benchmark::State& state) {
65 size_t result_size;
66 uint16_t buffer[12];
67 ParseUnicode(buffer, 12, FLAGS_TEST_STR, &result_size, nullptr);
68 LOG_ALWAYS_FATAL_IF(result_size != 12);
69 const size_t testIndex = state.range(0);
70 while (state.KeepRunning()) {
71 GraphemeBreak::isGraphemeBreak(nullptr, buffer, 0, result_size, testIndex);
72 }
73 }
74 BENCHMARK(BM_GraphemeBreak_Emoji_Flags)
75 ->Arg(2) // Middle of flag sequence.
76 ->Arg(4) // After flag sequence. Here is boundary of grapheme cluster.
77 ->Arg(10); // Middle of 3rd flag sequence.
78
79 } // namespace minikin
80