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