Path: blob/master/test/hotspot/gtest/concurrentTestRunner.inline.hpp
41140 views
/*1* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#ifndef GTEST_CONCURRENT_TEST_RUNNER_INLINE_HPP24#define GTEST_CONCURRENT_TEST_RUNNER_INLINE_HPP2526#include "memory/allocation.hpp"27#include "runtime/semaphore.hpp"28#include "runtime/thread.inline.hpp"29#include "threadHelper.inline.hpp"3031// This file contains helper classes to run unit tests concurrently in multiple threads.3233// Base class for test runnable. Override runUnitTest() to specify what to run.34class TestRunnable {35public:36virtual void runUnitTest() const = 0;37};3839// This class represents a thread for a unit test.40class UnitTestThread : public JavaTestThread {41public:42// runnableArg - what to run43// doneArg - a semaphore to notify when the thread is done running44// testDurationArg - how long to run (in milliseconds)45UnitTestThread(TestRunnable* const runnableArg, Semaphore* doneArg, const long testDurationArg) :46JavaTestThread(doneArg), runnable(runnableArg), testDuration(testDurationArg) {}4748// from JavaTestThread49void main_run() {50long stopTime = os::javaTimeMillis() + testDuration;51while (os::javaTimeMillis() < stopTime) {52runnable->runUnitTest();53}54}55private:56TestRunnable* const runnable;57const long testDuration;58};5960// Helper class for running a given unit test concurrently in multiple threads.61class ConcurrentTestRunner {62public:63// runnableArg - what to run64// nrOfThreadsArg - how many threads to use concurrently65// testDurationMillisArg - duration for each test run66ConcurrentTestRunner(TestRunnable* const runnableArg, int nrOfThreadsArg, long testDurationMillisArg) :67unitTestRunnable(runnableArg),68nrOfThreads(nrOfThreadsArg),69testDurationMillis(testDurationMillisArg) {}7071void run() {72Semaphore done(0);7374UnitTestThread** t = NEW_C_HEAP_ARRAY(UnitTestThread*, nrOfThreads, mtInternal);7576for (int i = 0; i < nrOfThreads; i++) {77t[i] = new UnitTestThread(unitTestRunnable, &done, testDurationMillis);78}7980for (int i = 0; i < nrOfThreads; i++) {81t[i]->doit();82}8384for (int i = 0; i < nrOfThreads; i++) {85done.wait();86}8788FREE_C_HEAP_ARRAY(UnitTestThread**, t);89}9091private:92TestRunnable* const unitTestRunnable;93const int nrOfThreads;94const long testDurationMillis;95};9697#endif // GTEST_CONCURRENT_TEST_RUNNER_INLINE_HPP9899100