Home | History | Annotate | Download | only in perftests
      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 <memory>
     19 
     20 #include <minikin/FontCollection.h>
     21 #include <FontTestUtils.h>
     22 #include <UnicodeUtils.h>
     23 #include <MinikinInternal.h>
     24 
     25 namespace minikin {
     26 
     27 const char* SYSTEM_FONT_PATH = "/system/fonts/";
     28 const char* SYSTEM_FONT_XML = "/system/etc/fonts.xml";
     29 
     30 static void BM_FontCollection_construct(benchmark::State& state) {
     31     std::vector<std::shared_ptr<FontFamily>> families =
     32             getFontFamilies(SYSTEM_FONT_PATH, SYSTEM_FONT_XML);
     33     while (state.KeepRunning()) {
     34         std::make_shared<FontCollection>(families);
     35     }
     36 }
     37 
     38 BENCHMARK(BM_FontCollection_construct);
     39 
     40 static void BM_FontCollection_hasVariationSelector(benchmark::State& state) {
     41     std::shared_ptr<FontCollection> collection(
     42             getFontCollection(SYSTEM_FONT_PATH, SYSTEM_FONT_XML));
     43 
     44     uint32_t baseCp = state.range(0);
     45     uint32_t vsCp = state.range(1);
     46 
     47     char titleBuffer[64];
     48     snprintf(titleBuffer, 64, "hasVariationSelector U+%04X,U+%04X", baseCp, vsCp);
     49     state.SetLabel(titleBuffer);
     50 
     51     while (state.KeepRunning()) {
     52         collection->hasVariationSelector(baseCp, vsCp);
     53     }
     54 }
     55 
     56 // TODO: Rewrite with BENCHMARK_CAPTURE for better test name.
     57 BENCHMARK(BM_FontCollection_hasVariationSelector)
     58       ->ArgPair(0x2708, 0xFE0F)
     59       ->ArgPair(0x2708, 0xFE0E)
     60       ->ArgPair(0x3402, 0xE0100);
     61 
     62 struct ItemizeTestCases {
     63     std::string itemizeText;
     64     std::string languageTag;
     65     std::string labelText;
     66 } ITEMIZE_TEST_CASES[] = {
     67     { "'A' 'n' 'd' 'r' 'o' 'i' 'd'", "en", "English" },
     68     { "U+4E16", "zh-Hans", "CJK Ideograph" },
     69     { "U+4E16", "zh-Hans,zh-Hant,ja,en,es,pt,fr,de", "CJK Ideograph with many language fallback" },
     70     { "U+3402 U+E0100", "ja", "CJK Ideograph with variation selector" },
     71     { "'A' 'n' U+0E1A U+0E31 U+0645 U+062D U+0648", "en", "Mixture of English, Thai and Arabic" },
     72     { "U+2708 U+FE0E", "en", "Emoji with variation selector" },
     73     { "U+0031 U+FE0F U+20E3", "en", "KEYCAP" },
     74 };
     75 
     76 static void BM_FontCollection_itemize(benchmark::State& state) {
     77     std::shared_ptr<FontCollection> collection(
     78             getFontCollection(SYSTEM_FONT_PATH, SYSTEM_FONT_XML));
     79 
     80     size_t testIndex = state.range(0);
     81     state.SetLabel("Itemize: " + ITEMIZE_TEST_CASES[testIndex].labelText);
     82 
     83     uint16_t buffer[64];
     84     size_t utf16_length = 0;
     85     ParseUnicode(
     86             buffer, 64, ITEMIZE_TEST_CASES[testIndex].itemizeText.c_str(), &utf16_length, nullptr);
     87     std::vector<FontCollection::Run> result;
     88     FontStyle style(FontStyle::registerLanguageList(ITEMIZE_TEST_CASES[testIndex].languageTag));
     89 
     90     android::AutoMutex _l(gMinikinLock);
     91     while (state.KeepRunning()) {
     92         result.clear();
     93         collection->itemize(buffer, utf16_length, style, &result);
     94     }
     95 }
     96 
     97 // TODO: Rewrite with BENCHMARK_CAPTURE once it is available in Android.
     98 BENCHMARK(BM_FontCollection_itemize)
     99     ->Arg(0)->Arg(1)->Arg(2)->Arg(3)->Arg(4)->Arg(5)->Arg(6);
    100 
    101 }  // namespace minikin
    102