root/examples/dataflow_priority.cpp

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. BOOST_AUTO_TEST_SUITE
  2. BOOST_AUTO_TEST_SUITE_END
  3. BOOST_AUTO_TEST_SUITE_END

   1 /******************************************************************************
   2 ** $Header: svn+ssh://jmmcg@svn.code.sf.net/p/libjmmcg/code/trunk/libjmmcg/examples/dataflow_priority.cpp 2185 2017-10-13 10:14:17Z jmmcg $
   3 **
   4 ** Copyright © 2015 by J.M.McGuiness, coder@hussar.me.uk
   5 **
   6 ** This library is free software; you can redistribute it and/or
   7 ** modify it under the terms of the GNU Lesser General Public
   8 ** License as published by the Free Software Foundation; either
   9 ** version 2.1 of the License, or (at your option) any later version.
  10 **
  11 ** This library is distributed in the hope that it will be useful,
  12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14 ** Lesser General Public License for more details.
  15 **
  16 ** You should have received a copy of the GNU Lesser General Public
  17 ** License along with this library; if not, write to the Free Software
  18 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19 */
  20 
  21 #include "stdafx.h"
  22 
  23 #define BOOST_TEST_MODULE libjmmcg_tests
  24 #include <boost/test/included/unit_test.hpp>
  25 
  26 #include <boost/test/test_case_template.hpp>
  27 #include <boost/mpl/list.hpp>
  28 
  29 #include "core/thread_pool_sequential.hpp"
  30 #include "core/thread_pool_master.hpp"
  31 #include "core/thread_pool_workers.hpp"
  32 
  33 #include <random>
  34 
  35 using namespace jmmcg;
  36 using namespace jmmcg::ppd;
  37 
  38 template<class Db, pool_traits::size_mode_t Sz, generic_traits::return_data Jn, class Mdl, unsigned int PoolSize=0, unsigned int GSSk=1>
  39 struct priority_queue_t {
  40         typedef pool_aspects<
  41                 Jn,
  42                 platform_api,
  43                 Mdl,
  44                 pool_traits::prioritised_queue,
  45                 std::less,
  46                 GSSk
  47         > thread_pool_traits;
  48 
  49         typedef thread_pool<Db, Sz, thread_pool_traits> pool_type;
  50 
  51         static const typename pool_type::pool_type::size_type pool_size=PoolSize;
  52 };
  53 
  54 template<class Db, pool_traits::size_mode_t Sz, generic_traits::return_data Jn, class Mdl, unsigned int PoolSize, unsigned int GSSk>
  55 const typename priority_queue_t<Db, Sz, Jn, Mdl, PoolSize, GSSk>::pool_type::pool_type::size_type priority_queue_t<Db, Sz, Jn, Mdl, PoolSize, GSSk>::pool_size;
  56 
  57 typedef boost::mpl::list<
  58 //      priority_queue_t<pool_traits::work_distribution_mode_t::worker_threads_get_work<pool_traits::work_distribution_mode_t::queue_model_t::pool_owns_queue>, pool_traits::size_mode_t::sequential, generic_traits::return_data::joinable, sequential_mode>,
  59         priority_queue_t<pool_traits::work_distribution_mode_t::worker_threads_get_work<pool_traits::work_distribution_mode_t::queue_model_t::pool_owns_queue>, pool_traits::size_mode_t::fixed_size, generic_traits::return_data::joinable, heavyweight_threading, 1>
  60 // TODO priority_queue_t<pool_traits::work_distribution_mode_t::worker_threads_get_work<pool_traits::work_distribution_mode_t::queue_model_t::pool_owns_queue>, pool_traits::size_mode_t::fixed_size, generic_traits::return_data::joinable, heavyweight_threading, 1, 2>,
  61 
  62 //      priority_queue_t<pool_traits::work_distribution_mode_t::worker_threads_get_work<pool_traits::work_distribution_mode_t::queue_model_t::pool_owns_queue>, pool_traits::size_mode_t::fixed_size, generic_traits::return_data::joinable, heavyweight_threading, 2>
  63 > finite_test_types;
  64 
  65 typedef boost::mpl::list<
  66 //      priority_queue_t<pool_traits::work_distribution_mode_t::one_thread_distributes<>, pool_traits::size_mode_t::sequential, generic_traits::return_data::joinable, sequential_mode>,
  67 //      priority_queue_t<pool_traits::work_distribution_mode_t::one_thread_distributes<>, pool_traits::size_mode_t::infinite, generic_traits::return_data::joinable, heavyweight_threading>
  68 > infinite_test_types;
  69 
  70 // TODO Need to test: pool_traits::size_mode_t::tracks_to_max
  71 
  72 struct res_t {
  73         int i;
  74 };
  75 
  76 struct work_type_simple {
  77         int i_;
  78 
  79         work_type_simple(const int i)
  80         : i_(i) {
  81         }
  82         void __fastcall process(res_t &r) {
  83                 sleep(1);
  84                 r.i=i_;
  85         }
  86 
  87         bool __fastcall operator<(work_type_simple const &i) const {
  88                 return i_<i.i_;
  89         }
  90 };
  91 
  92 BOOST_AUTO_TEST_SUITE(thread_pool_tests)
  93 
  94 BOOST_AUTO_TEST_SUITE(joinable)
  95 
  96 BOOST_AUTO_TEST_SUITE(finite)
  97 
  98 BOOST_AUTO_TEST_SUITE(nowait)
  99 
 100 BOOST_AUTO_TEST_CASE_TEMPLATE(n_threads, T, finite_test_types) {
 101         typedef typename T::pool_type pool_type;
 102         typedef typename pool_type::joinable joinable;
 103         typedef typename pool_type::nonjoinable nonjoinable;
 104 
 105         constexpr unsigned test_size=1000;
 106 
 107         std::mt19937_64 gen(42);
 108         std::uniform_int_distribution<int> distribution(std::numeric_limits<int>::min()+1, std::numeric_limits<int>::max()-1);
 109         auto rand=std::bind(distribution, gen);
 110 
 111         pool_type pool(T::pool_size);
 112         for (unsigned i=0;i<test_size; ++i) {
 113                 pool<<nonjoinable()<<work_type_simple(rand());
 114         }
 115         BOOST_CHECK_GT(pool.queue_size(), 0U);
 116         auto const &exec=pool<<joinable()<<work_type_simple(std::numeric_limits<int>::min());
 117         *exec;
 118         BOOST_CHECK_EQUAL(pool.pool_size(), T::pool_size);
 119         BOOST_CHECK_EQUAL(pool.queue_size(), 0U);
 120 }
 121 
 122 BOOST_AUTO_TEST_SUITE_END()
 123 
 124 BOOST_AUTO_TEST_SUITE(wait_dataflow)
 125 
 126 BOOST_AUTO_TEST_CASE_TEMPLATE(add_one_work, T, finite_test_types) {
 127         typedef typename T::pool_type pool_type;
 128         typedef typename pool_type::joinable joinable;
 129 
 130         constexpr unsigned test_size=10;
 131 
 132         std::mt19937_64 gen(42);
 133         std::uniform_int_distribution<int> distribution(std::numeric_limits<int>::min()+1, std::numeric_limits<int>::max()-1);
 134         auto rand=std::bind(distribution, gen);
 135         
 136         pool_type pool(T::pool_size);
 137         for (unsigned i=0;i<test_size; ++i) {
 138                 auto const &exec0=pool<<joinable()<<work_type_simple(rand());
 139                 auto const &exec1=pool<<joinable()<<work_type_simple(rand());
 140                 auto const &exec2=pool<<joinable()<<work_type_simple(rand());
 141                 auto const &exec3=pool<<joinable()<<work_type_simple(rand());
 142                 auto const &exec4=pool<<joinable()<<work_type_simple(rand());
 143                 auto const &exec5=pool<<joinable()<<work_type_simple(rand());
 144                 auto const &exec6=pool<<joinable()<<work_type_simple(rand());
 145                 auto const &exec7=pool<<joinable()<<work_type_simple(rand());
 146                 auto const &exec8=pool<<joinable()<<work_type_simple(rand());
 147                 auto const &exec9=pool<<joinable()<<work_type_simple(rand());
 148                 *exec0;
 149                 *exec1;
 150                 *exec2;
 151                 *exec3;
 152                 *exec4;
 153                 *exec5;
 154                 *exec6;
 155                 *exec7;
 156                 *exec8;
 157                 *exec9;
 158         }
 159         BOOST_CHECK_EQUAL(pool.pool_size(), T::pool_size);
 160         BOOST_CHECK_EQUAL(pool.queue_size(), 0U);
 161 }
 162 
 163 BOOST_AUTO_TEST_SUITE_END()
 164 
 165 BOOST_AUTO_TEST_SUITE_END()
 166 
 167 BOOST_AUTO_TEST_SUITE(infinite)
 168 
 169 BOOST_AUTO_TEST_SUITE(wait_dataflow)
 170 
 171 BOOST_AUTO_TEST_CASE_TEMPLATE(add_one_work, T, infinite_test_types) {
 172         typedef typename T::pool_type pool_type;
 173         typedef typename pool_type::joinable joinable;
 174 
 175         constexpr unsigned test_size=100;
 176 
 177         std::mt19937_64 gen(42);
 178         std::uniform_int_distribution<int> distribution(std::numeric_limits<int>::min()+1, std::numeric_limits<int>::max()-1);
 179         auto rand=std::bind(distribution, gen);
 180         
 181         pool_type pool;
 182         for (unsigned i=0;i<test_size; ++i) {
 183                 auto const &exec0=pool<<joinable()<<work_type_simple(rand());
 184                 auto const &exec1=pool<<joinable()<<work_type_simple(rand());
 185                 auto const &exec2=pool<<joinable()<<work_type_simple(rand());
 186                 auto const &exec3=pool<<joinable()<<work_type_simple(rand());
 187                 auto const &exec4=pool<<joinable()<<work_type_simple(rand());
 188                 auto const &exec5=pool<<joinable()<<work_type_simple(rand());
 189                 auto const &exec6=pool<<joinable()<<work_type_simple(rand());
 190                 auto const &exec7=pool<<joinable()<<work_type_simple(rand());
 191                 auto const &exec8=pool<<joinable()<<work_type_simple(rand());
 192                 auto const &exec9=pool<<joinable()<<work_type_simple(rand());
 193                 *exec0;
 194                 *exec1;
 195                 *exec2;
 196                 *exec3;
 197                 *exec4;
 198                 *exec5;
 199                 *exec6;
 200                 *exec7;
 201                 *exec8;
 202                 *exec9;
 203         }
 204         BOOST_CHECK_EQUAL(pool.queue_size(), 0U);
 205 }
 206 
 207 BOOST_AUTO_TEST_SUITE_END()
 208 
 209 BOOST_AUTO_TEST_SUITE_END()
 210 
 211 BOOST_AUTO_TEST_SUITE_END()
 212 
 213 BOOST_AUTO_TEST_SUITE_END()

/* [<][>][^][v][top][bottom][index][help] */