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