GraphemeBreak.cpp 3.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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"
21
#include "UnicodeUtils.h"
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

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);
M
Martijn Coenen 已提交
39
    const size_t testIndex = state.range(0);
40
    while (state.KeepRunning()) {
41
        GraphemeBreak::isGraphemeBreak(nullptr, buffer, 0, result_size, testIndex);
42 43 44 45 46 47 48 49 50 51 52 53
    }
}
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);
M
Martijn Coenen 已提交
54
    const size_t testIndex = state.range(0);
55
    while (state.KeepRunning()) {
56
        GraphemeBreak::isGraphemeBreak(nullptr, buffer, 0, result_size, testIndex);
57 58 59 60 61 62 63 64 65 66 67 68
    }
}
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);
M
Martijn Coenen 已提交
69
    const size_t testIndex = state.range(0);
70
    while (state.KeepRunning()) {
71
        GraphemeBreak::isGraphemeBreak(nullptr, buffer, 0, result_size, testIndex);
72 73 74 75 76 77 78 79
    }
}
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