Home | History | Annotate | Download | only in stresstest
      1 /*
      2  * Copyright (C) 2017 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/Layout.h"
     18 
     19 #include <condition_variable>
     20 #include <mutex>
     21 #include <random>
     22 #include <thread>
     23 
     24 #include <cutils/log.h>
     25 #include <gtest/gtest.h>
     26 
     27 #include "minikin/FontCollection.h"
     28 #include "minikin/Macros.h"
     29 
     30 #include "FontTestUtils.h"
     31 #include "MinikinInternal.h"
     32 #include "PathUtils.h"
     33 
     34 namespace minikin {
     35 
     36 constexpr int LAYOUT_COUNT_PER_COLLECTION = 500;
     37 constexpr int COLLECTION_COUNT_PER_THREAD = 15;
     38 constexpr int NUM_THREADS = 10;
     39 
     40 std::mutex gMutex;
     41 std::condition_variable gCv;
     42 bool gReady GUARDED_BY(gMutex) = false;
     43 
     44 static std::vector<uint16_t> generateTestText(std::mt19937* mt, int lettersInWord,
     45                                               int wordsInText) {
     46     std::uniform_int_distribution<uint16_t> dist('A', 'Z');
     47 
     48     std::vector<uint16_t> text;
     49     text.reserve((lettersInWord + 1) * wordsInText - 1);
     50     for (int i = 0; i < wordsInText; ++i) {
     51         if (i != 0) {
     52             text.emplace_back(' ');
     53         }
     54         for (int j = 0; j < lettersInWord; ++j) {
     55             text.emplace_back(dist(*mt));
     56         }
     57     }
     58     return text;
     59 }
     60 
     61 static void thread_main(int tid) {
     62     {
     63         // Wait until all threads are created.
     64         std::unique_lock<std::mutex> lock(gMutex);
     65         gCv.wait(lock, [] { return gReady; });
     66     }
     67 
     68     std::mt19937 mt(tid);
     69 
     70     for (int i = 0; i < COLLECTION_COUNT_PER_THREAD; ++i) {
     71         MinikinPaint paint(buildFontCollection("Ascii.ttf"));
     72         paint.size = 10.0f;  // Make 1em = 10px
     73 
     74         for (int j = 0; j < LAYOUT_COUNT_PER_COLLECTION; ++j) {
     75             // Generates 10 of 3-letter words so that the word sometimes hit the cache.
     76             Layout layout;
     77             std::vector<uint16_t> text = generateTestText(&mt, 3, 10);
     78             layout.doLayout(text, Range(0, text.size()), Bidi::LTR, paint, StartHyphenEdit::NO_EDIT,
     79                             EndHyphenEdit::NO_EDIT);
     80             std::vector<float> advances(text.size());
     81             layout.getAdvances(advances.data());
     82             for (size_t k = 0; k < advances.size(); ++k) {
     83                 // All characters in Ascii.ttf has 1.0em horizontal advance.
     84                 LOG_ALWAYS_FATAL_IF(advances[k] != 10.0f, "Memory corruption detected.");
     85             }
     86         }
     87     }
     88 }
     89 
     90 TEST(MultithreadTest, ThreadSafeStressTest) {
     91     std::vector<std::thread> threads;
     92 
     93     {
     94         std::unique_lock<std::mutex> lock(gMutex);
     95         threads.reserve(NUM_THREADS);
     96         for (int i = 0; i < NUM_THREADS; ++i) {
     97             threads.emplace_back(&thread_main, i);
     98         }
     99         gReady = true;
    100     }
    101     gCv.notify_all();
    102 
    103     for (auto& thread : threads) {
    104         thread.join();
    105     }
    106 }
    107 
    108 }  // namespace minikin
    109