summaryrefslogtreecommitdiffstats
path: root/fastos/src/tests/thread_stats_test.cpp
blob: 7628fb2816905bc19874f1cc366f8d45220672c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "tests.h"
#include "job.h"
#include "thread_test_base.hpp"

class Thread_Stats_Test : public ThreadTestBase
{
   void ThreadStatsTest ()
   {
      int inactiveThreads;
      int activeThreads;
      int startedThreads;

      TestHeader("Thread Statistics Test");

      FastOS_ThreadPool pool(128*1024);
      Job job[2];

      inactiveThreads = pool.GetNumInactiveThreads();
      Progress(inactiveThreads == 0, "Initial inactive threads = %d",
               inactiveThreads);
      activeThreads = pool.GetNumActiveThreads();
      Progress(activeThreads == 0, "Initial active threads = %d",
               activeThreads);
      startedThreads = pool.GetNumStartedThreads();
      Progress(startedThreads == 0, "Initial started threads = %d",
               startedThreads);

      job[0].code = WAIT_FOR_BREAK_FLAG;
      job[0].ownThread = pool.NewThread(this,
                                        static_cast<void *>(&job[0]));

      FastOS_Thread::Sleep(1000);

      inactiveThreads = pool.GetNumInactiveThreads();
      Progress(inactiveThreads == 0, "Inactive threads = %d", inactiveThreads);
      activeThreads = pool.GetNumActiveThreads();
      Progress(activeThreads == 1, "Active threads = %d", activeThreads);
      startedThreads = pool.GetNumStartedThreads();
      Progress(startedThreads == 1, "Started threads = %d", startedThreads);

      job[1].code = WAIT_FOR_BREAK_FLAG;
      job[1].ownThread = pool.NewThread(this,
                                        static_cast<void *>(&job[1]));

      FastOS_Thread::Sleep(1000);

      inactiveThreads = pool.GetNumInactiveThreads();
      Progress(inactiveThreads == 0, "Inactive threads = %d", inactiveThreads);
      activeThreads = pool.GetNumActiveThreads();
      Progress(activeThreads == 2, "Active threads = %d", activeThreads);
      startedThreads = pool.GetNumStartedThreads();
      Progress(startedThreads == 2, "Started threads = %d", startedThreads);

      Progress(true, "Setting breakflag on threads...");
      job[0].ownThread->SetBreakFlag();
      job[1].ownThread->SetBreakFlag();

      FastOS_Thread::Sleep(3000);

      inactiveThreads = pool.GetNumInactiveThreads();
      Progress(inactiveThreads == 2, "Inactive threads = %d", inactiveThreads);
      activeThreads = pool.GetNumActiveThreads();
      Progress(activeThreads == 0, "Active threads = %d", activeThreads);
      startedThreads = pool.GetNumStartedThreads();
      Progress(startedThreads == 2, "Started threads = %d", startedThreads);


      Progress(true, "Repeating process in the same pool...");

      job[0].code = WAIT_FOR_BREAK_FLAG;
      job[0].ownThread = pool.NewThread(this, static_cast<void *>(&job[0]));

      FastOS_Thread::Sleep(1000);

      inactiveThreads = pool.GetNumInactiveThreads();
      Progress(inactiveThreads == 1, "Inactive threads = %d", inactiveThreads);
      activeThreads = pool.GetNumActiveThreads();
      Progress(activeThreads == 1, "Active threads = %d", activeThreads);
      startedThreads = pool.GetNumStartedThreads();
      Progress(startedThreads == 3, "Started threads = %d", startedThreads);

      job[1].code = WAIT_FOR_BREAK_FLAG;
      job[1].ownThread = pool.NewThread(this, static_cast<void *>(&job[1]));

      FastOS_Thread::Sleep(1000);

      inactiveThreads = pool.GetNumInactiveThreads();
      Progress(inactiveThreads == 0, "Inactive threads = %d", inactiveThreads);
      activeThreads = pool.GetNumActiveThreads();
      Progress(activeThreads == 2, "Active threads = %d", activeThreads);
      startedThreads = pool.GetNumStartedThreads();
      Progress(startedThreads == 4, "Started threads = %d", startedThreads);

      Progress(true, "Setting breakflag on threads...");
      job[0].ownThread->SetBreakFlag();
      job[1].ownThread->SetBreakFlag();

      FastOS_Thread::Sleep(3000);

      inactiveThreads = pool.GetNumInactiveThreads();
      Progress(inactiveThreads == 2, "Inactive threads = %d", inactiveThreads);
      activeThreads = pool.GetNumActiveThreads();
      Progress(activeThreads == 0, "Active threads = %d", activeThreads);
      startedThreads = pool.GetNumStartedThreads();
      Progress(startedThreads == 4, "Started threads = %d", startedThreads);


      pool.Close();
      Progress(true, "Pool closed.");

      PrintSeparator();
   }

   int Main () override;
};

int Thread_Stats_Test::Main ()
{
   printf("grep for the string '%s' to detect failures.\n\n", failString);
   time_t before = time(0);

   ThreadStatsTest();
   { time_t now = time(0); printf("[%ld seconds]\n", now-before); before = now; }

   printf("END OF TEST (%s)\n", _argv[0]);
   return allWasOk() ? 0 : 1;
}

int main (int argc, char **argv)
{
   Thread_Stats_Test app;
   setvbuf(stdout, NULL, _IOLBF, 8192);
   return app.Entry(argc, argv);
}