-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestRunnerWindows.cpp
More file actions
129 lines (114 loc) · 3.19 KB
/
TestRunnerWindows.cpp
File metadata and controls
129 lines (114 loc) · 3.19 KB
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
#include "stdafx.h"
#include "TestRunnerWindows.h"
#include "TestRoutinesWindows.h"
#include "ComplexityAndScoreUtils.h"
namespace
{
inline LPTHREAD_START_ROUTINE SelectThreadProcForTestMethod(TestMethod method)
{
switch(method)
{
case TestMethodCPU:
return &ThreadProcCPU;
case TestMethodMemory:
return &ThreadProcMemory;
case TestMethodCPUandMemory:
return &ThreadProcCPUandMemory;
default:
throw std::runtime_error("Unknown test method");
}
}
}
TestRunnerWindows::TestRunnerWindows()
{
if (!::SetPriorityClass(::GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS))
{
throw std::runtime_error("Can't set process priority. Try to launch this application with Administrative privilegies");
}
}
TestRunnerWindows::~TestRunnerWindows()
{
Stop();
}
void TestRunnerWindows::Go(const TestParameters& parameters)
{
// TODO: Add mutex between Go and Stop methods
assert(parameters.method != TestMethodUnknown && "Something goes wrong up on stack");
if (IsRunning())
{
return;
}
if (!parameters.burnMode)
{
for (uint16_t i = 0; i < 350; ++i)
{
QApplication::processEvents();
QThread::msleep(1);
}
}
const Complexity complexity = static_cast<int64_t>(parameters.baseComplexity * utils::GetTestMethodComplexityRatio(parameters.method));
m_stop = false;
m_threads.assign(parameters.numberOfThreads, NULL);
m_threadsInfo.assign(parameters.numberOfThreads, ThreadInfo(m_stop, complexity));
LPTHREAD_START_ROUTINE threadProc = SelectThreadProcForTestMethod(parameters.method);
for (size_t i = 0; i < parameters.numberOfThreads; ++i)
{
m_threads[i] = ::CreateThread(NULL, 0, threadProc, &(m_threadsInfo[i]), CREATE_SUSPENDED, NULL);
if (m_threads[i] == NULL)
{
TerminateThreads();
throw std::runtime_error("Unable to create thread");
}
if (!::SetThreadPriority(m_threads.at(i), THREAD_PRIORITY_TIME_CRITICAL))
{
throw std::runtime_error("Unable to adjust thread priority");
}
}
for (HANDLE thread : m_threads)
{
::ResumeThread(thread);
}
}
bool TestRunnerWindows::IsRunning() const
{
for (const ThreadInfo& threadInfo : m_threadsInfo)
{
if (threadInfo.finishTimestamp == 0)
{
return true;
}
}
return false;
}
void TestRunnerWindows::Stop()
{
if (!IsRunning())
{
return;
}
m_stop = true;
::WaitForMultipleObjects(static_cast<DWORD>(m_threads.size()), m_threads.data(), TRUE, INFINITE);
DeleteThreadObjects();
}
void TestRunnerWindows::TerminateThreads() // This is awful idea, I should think a something better
{
for (size_t i = 0; i < m_threads.size(); ++i)
{
if (m_threads[i] != NULL)
{
::TerminateThread(m_threads[i], 1);
}
}
DeleteThreadObjects();
}
void TestRunnerWindows::DeleteThreadObjects()
{
for (HANDLE thread : m_threads)
{
if (thread != NULL)
{
::CloseHandle(thread);
}
}
m_threads.clear();
}