GraphemeBreak.cpp 3.0 KB

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