-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.cpp
More file actions
160 lines (145 loc) · 4.56 KB
/
MainWindow.cpp
File metadata and controls
160 lines (145 loc) · 4.56 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "stdafx.h"
#include "MainWindow.h"
#include "ComplexityAndScoreUtils.h"
namespace
{
size_t GetProcessorsCount()
{
::SYSTEM_INFO sysInfo = { };
::GetSystemInfo(&sysInfo);
return sysInfo.dwNumberOfProcessors;
}
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, m_ui(new Ui::MainWindow)
, m_processorsCount(GetProcessorsCount())
, m_observer(nullptr)
{
m_observer = new TestObserver(this, &m_testRunner);
connect(m_observer, &QThread::finished, this, &MainWindow::OnTestFinished);
m_ui->setupUi(this);
m_ui->lblProgramName->setText(tr("%1 v%2.%3").arg(PROGRAM_NAME).arg(VER_MAJOR).arg(VER_MINOR));
m_ui->lblThreads->setText(tr("Threads: %1").arg(m_processorsCount));
}
MainWindow::~MainWindow()
{
const size_t maxWaitTime = 5000; // ms
if (!m_observer->wait(maxWaitTime))
{
qDebug() << "The observer has to be terminated";
m_observer->terminate();
}
}
void MainWindow::on_btnGo_clicked()
{
m_ui->listWidget->clear();
m_ui->progressBar->setValue(0);
try
{
m_ui->lblScore->setText("0");
m_ui->lblPerThread->setText("0");
m_stateKeeper.TestStarted(DEFAULT_STAGES_COUNT, m_ui->chkBurn->isChecked());
RunSingleTest();
}
catch(const std::exception& ex)
{
QMessageBox::warning(this, tr("Start test"), tr("Unable to start the test: %1").arg(ex.what()));
}
}
void MainWindow::OnTestFinished()
{
try
{
UpdateList();
UpdateTimeElapsed();
UpdateTheScore();
UpdateProgress();
}
catch (const std::exception& ex)
{
m_ui->progressBar->setValue(m_ui->progressBar->maximum());
QMessageBox::critical(this, "Error", ex.what());
SetControlsEnabled(true);
}
}
void MainWindow::SetControlsEnabled(bool enabled)
{
m_ui->btnGo->setEnabled(enabled);
m_ui->chkBurn->setEnabled(enabled);
m_ui->optCPU->setEnabled(enabled);
m_ui->optMemory->setEnabled(enabled);
m_ui->optCPUandMemory->setEnabled(enabled);
}
TestMethod MainWindow::DefineTestMethod() const
{
TestMethod method = TestMethodUnknown;
if (m_ui->optCPU->isChecked())
{
method = TestMethodCPU;
}
if (m_ui->optMemory->isChecked())
{
method = TestMethodMemory;
}
else if (m_ui->optCPUandMemory->isChecked())
{
method = TestMethodCPUandMemory;
}
return method;
}
void MainWindow::RunSingleTest()
{
SetControlsEnabled(false);
m_testRunner.Go(TestParameters { m_processorsCount, DefineTestMethod(), utils::CalculateComplexity(), m_ui->chkBurn->isChecked() } );
m_observer->start();
}
void MainWindow::UpdateList()
{
SingleTestResult result = m_observer->GetSingleTestResult();
m_ui->listWidget->addItem(tr("Stage %1/%2").arg(m_stateKeeper.CurrentStage() + 1).arg(m_stateKeeper.GetStagesCount()));
for (size_t i = 0; i < result.size(); ++i)
{
double threadDuration = static_cast<double>(result.at(i).duration) / 1000;
if (result.at(i).error.empty())
{
m_ui->listWidget->addItem(QString(" ") + tr("Thread %1: %2").arg(i).arg(threadDuration));
}
else
{
m_ui->listWidget->addItem(QString(" ") + tr("Thread %1: error \"%2\"").arg(i).arg(result.at(i).error.c_str()));
}
}
m_stateKeeper.SubmitSingleTestResult(m_observer->GetSingleTestResult());
}
void MainWindow::UpdateTimeElapsed()
{
TimesElapsed times = m_stateKeeper.GetTimesElapsed();
TimeElapsed timeElapsedTotal = std::accumulate(times.begin(), times.end(), 0);
m_ui->lblTimeElapsed->setText(tr("%1 seconds").arg(static_cast<double>(timeElapsedTotal) / 1000));
}
void MainWindow::UpdateTheScore()
{
auto averageResults = m_stateKeeper.GetAverageResults();
if (averageResults.empty())
{
throw std::runtime_error("The test is over due to error");
}
m_ui->lblScore->setText(QString::number(utils::CalculateTheScoreMultithreaded(averageResults, m_processorsCount)));
m_ui->lblPerThread->setText(QString::number(utils::CalculateTheScorePerThread(averageResults)));
}
void MainWindow::UpdateProgress()
{
if (m_stateKeeper.CurrentStage() < m_stateKeeper.GetStagesCount())
{
int progress = static_cast<int>((static_cast<double>(m_stateKeeper.CurrentStage()) / m_stateKeeper.GetStagesCount()) * 100);
m_ui->progressBar->setValue(progress);
m_ui->progressBar->update();
RunSingleTest();
}
else
{
m_ui->progressBar->setValue(m_ui->progressBar->maximum());
SetControlsEnabled(true);
}
}