ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
stressThreadPool.C
Go to the documentation of this file.
1 // Usage:
2 // root [0] .L stressThreadPool.C++
3 // root [1] stressThreadPool(5, true)
4 // where 5 is a number of Threads in the pool
5 // there will be then nThreads * 10 tasks pushed to the test
6 
7 // STD
8 #include <iostream>
9 #include <iterator>
10 #include <vector>
11 #ifndef _WIN32
12 #include <unistd.h>
13 #endif
14 // ThreadPool
15 #include "TThreadPool.h"
16 // ROOT
17 #include "TThread.h"
18 
19 //=============================================================================
20 using namespace std;
21 //=============================================================================
22 // Don't set it less than 1, otherwise autotest won't be able to detect whether tests were successful or not
23 const size_t g_sleeptime = 2; // in secs.
24 const size_t g_multTasks = 10;
25 //=============================================================================
26 
27 enum EProc {start, clean};
28 
29 class TTestTask: public TThreadPoolTaskImp<TTestTask, EProc> {
30 public:
31  bool runTask(EProc /*_param*/) {
32  m_tid = TThread::SelfId();
34  return true;
35  }
36  unsigned long threadID() const {
37  return m_tid;
38  }
39 
40 private:
41  unsigned long m_tid;
42 };
43 ostream &operator<< (ostream &_stream, const TTestTask &_task)
44 {
45  _stream << _task.threadID();
46  return _stream;
47 }
48 
49 //=============================================================================
50 void stressThreadPool(size_t _numThreads = 5, bool _needDbg = false)
51 {
52  size_t numTasks(_numThreads * g_multTasks);
53  TThreadPool<TTestTask, EProc> threadPool(_numThreads, _needDbg);
54  vector <TTestTask> tasksList(numTasks);
55  // Pushing 4 * numTasks task in the pool
56  // We want to dain the task queue before pushing a next bunch of tasks (just to show you a Drain method ;) )
57  for (size_t j = 0; j < 4; ++j )
58  {
59  cout << "+++++++++ Starting iteration #" << j << " ++++++++++++"<< endl;
60  for (size_t i = 0; i < numTasks; ++i) {
61  threadPool.PushTask(tasksList[i], start);
62  }
63 
64  cout << "\n ****** Drain the tasks queue ******" << endl;
65  threadPool.Drain();
66  }
67  cout << "\n Stopping..." << endl;
68  threadPool.Stop(true);
69 
70  // ostream_iterator<TTestTask> out_it( cout, "\n" );
71  // copy( tasksList.begin(), tasksList.end(),
72  // out_it );
73 
74  typedef map<unsigned long, size_t> counter_t;
75  counter_t counter;
76  {
77  vector <TTestTask>::const_iterator iter = tasksList.begin();
78  vector <TTestTask>::const_iterator iter_end = tasksList.end();
79  for (; iter != iter_end; ++iter) {
80  counter_t::iterator found = counter.find(iter->threadID());
81  if (found == counter.end())
82  counter.insert(counter_t::value_type(iter->threadID(), 1));
83  else {
84  found->second = found->second + 1;
85  }
86  }
87  }
88 
89  cout << "\n************* RESULT ****************" << endl;
90 
91  counter_t::const_iterator iter = counter.begin();
92  counter_t::const_iterator iter_end = counter.end();
93  bool testOK = true;
94  for (; iter != iter_end; ++iter) {
95  cout << "Thread " << iter->first << " was used " << iter->second << " times\n";
96  // each thread suppose to be used equal amount of time,
97  // exactly (g_numTasks/g_numThreads) times
98  if (iter->second != g_multTasks)
99  testOK = false;
100  }
101 
102  cout << "ThreadPool: the simple test status: " << (testOK ? "OK" : "Failed") << endl;
103 }
RooArgList L(const RooAbsArg &v1)
void Drain()
Definition: TThreadPool.h:215
const size_t g_sleeptime
std::map< std::string, std::string >::const_iterator iter
Definition: TAlienJob.cxx:54
const size_t g_multTasks
static Long_t SelfId()
Static method returning the id for the current thread.
Definition: TThread.cxx:538
void PushTask(typename TThreadPoolTask< aTask, aParam >::task_t &task, aParam param)
Definition: TThreadPool.h:173
void threadPool(size_t _numThreads=10, bool _needDbg=false)
Definition: threadPool.C:45
void Stop(bool processRemainingJobs=false)
Definition: TThreadPool.h:188
void stressThreadPool(size_t _numThreads=5, bool _needDbg=false)
static Int_t Sleep(ULong_t secs, ULong_t nanos=0)
Static method to sleep the calling thread.
Definition: TThread.cxx:736
ostream & operator<<(ostream &_stream, const TTestTask &_task)