Skip to content

Commit 36f8b4d

Browse files
ZeeTerminal v0.4.2 src
1 parent 2226677 commit 36f8b4d

18 files changed

+3123
-884
lines changed

CommandFiles/CPUStress.cpp

Lines changed: 137 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -3,76 +3,43 @@ void colour(std::string, std::string);
33
void ClearKeyboardBuffer();
44
void slowcolourfn(std::string, std::string, std::string);
55
void slowcharCentredFn(bool, std::string);
6-
long double NumInput(std::string);
76
std::string StrInput(std::string);
87
void sleep(long long int);
98
void Exiting();
109

1110
std::atomic<bool> StopCpuStress = false;
1211
// Get logical core count of cpu
1312
const int nLogicalCoreCount = std::thread::hardware_concurrency();
14-
unsigned long long int nReiterationCount = 0;
15-
unsigned long long int nCurrentReiterationNum = 0;
13+
uint64_t nReiterationCount = 0;
14+
std::atomic<uint64_t> nCurrentReiterationNum = 0;
1615
bool bCpuStressKeyboardTermination = false;
1716

1817
extern ConfigFileSystem ConfigObjMain;
1918

2019

21-
// 1 for single core, 2 for multi core
22-
void CpuStressTestWorker(short int nSingleOrMultiCore) {
23-
long double ldStress = 1.0;
24-
25-
if (nSingleOrMultiCore == 1) {
26-
while (!_kbhit()) {
27-
ldStress *= 1.5;
28-
ldStress /= 1.1;
29-
ldStress += 1;
30-
}
31-
return;
32-
}
33-
else if (nSingleOrMultiCore == 2) {
34-
while (!StopCpuStress) {
35-
ldStress *= 1.5;
36-
ldStress /= 1.1;
37-
ldStress += 1;
38-
}
39-
return;
20+
// Worker for CPU Stress Test
21+
void CpuStressTestWorker() {
22+
long double ldStress = 1.0;
23+
24+
while (!StopCpuStress) {
25+
ldStress *= RandNum(0, 5);
26+
ldStress /= RandNum(0.00000001, 5);
27+
ldStress += RandNum(0, 5);
4028
}
41-
29+
4230
return;
4331
}
4432

45-
// 1 for single core, 2 for multi core
46-
void CpuBenchmarkWorker(short int nSingleOrMultiCore) {
33+
// Worker for CPU Benchmark
34+
void CpuBenchmarkWorker() {
4735
long double ldStress = 1.0;
48-
unsigned long long int lnBenchmarkReiterationCount = 0;
4936

50-
if (nSingleOrMultiCore == 1) {
51-
while (lnBenchmarkReiterationCount <= nReiterationCount) {
52-
if (_kbhit()) {
53-
bCpuStressKeyboardTermination = true;
54-
break;
55-
}
56-
ldStress *= 1.5;
57-
ldStress /= 1.1;
58-
ldStress += 1;
59-
lnBenchmarkReiterationCount++;
60-
std::cout << "Progress: " << (lnBenchmarkReiterationCount * 100) / nReiterationCount << "%\r";
61-
}
62-
std::cout << "\n";
63-
return;
64-
}
65-
else if (nSingleOrMultiCore == 2) {
66-
while (!StopCpuStress) {
67-
ldStress *= 1.5;
68-
ldStress /= 1.1;
69-
ldStress += 1;
70-
lnBenchmarkReiterationCount++;
71-
nCurrentReiterationNum = lnBenchmarkReiterationCount;
72-
}
73-
return;
37+
while (!StopCpuStress) {
38+
ldStress *= RandNum(0, 5);
39+
ldStress /= RandNum(0.00000001, 5);
40+
ldStress += RandNum(0, 5);
41+
nCurrentReiterationNum++;
7442
}
75-
7643
return;
7744
}
7845

@@ -85,69 +52,109 @@ void CpuBenchmark(short int nSingleOrMulti, long long int nArgNum = -1) {
8552

8653
// Take input
8754
if (nArgNum < 0) {
88-
// Multi core is more as more cores contribute to the same workload, so less reiterations would basically be under a second
89-
if (nSingleOrMulti == 1) std::cout << "\nThe default number of reiterations is 100,000.\n";
90-
else if (nSingleOrMulti == 2) std::cout << "\nThe default number of reiterations is 1 billion.\n";
9155

92-
nInput = NumInput("Please input the number of reiterations that you want (negative number to exit, 0 for default, more is more intensive): > ");
56+
if (nSingleOrMulti == 1) {
57+
CentreColouredText(" ___CPU SINGLE-CORE BENCHMARK___ ", 1);
58+
59+
colour(GRN, ConfigObjMain.sColourGlobalBack);
60+
std::cout << wordWrap("\nWelcome to the Single-Core CPU Benchmark!\n");
61+
colour(ConfigObjMain.sColourGlobal, ConfigObjMain.sColourGlobalBack);
62+
}
63+
else if (nSingleOrMulti == 2) {
64+
CentreColouredText(" ___CPU MULTI-CORE BENCHMARK___ ", 1);
65+
66+
colour(GRN, ConfigObjMain.sColourGlobalBack);
67+
std::cout << wordWrap("\nWelcome to the Multi-Core CPU Benchmark!\n");
68+
colour(ConfigObjMain.sColourGlobal, ConfigObjMain.sColourGlobalBack);
69+
}
70+
71+
std::cout << wordWrap("The default number of reiterations is 50 million (50000000).\n");
72+
73+
nInput = NumInputll("Please input the number of reiterations that you want (negative number to exit, 0 for default, more is more intensive): > ");
9374
}
9475
else nInput = nArgNum;
9576

96-
9777
// Exit if input is less than 0
9878
if (nInput < 0) {
9979
Exiting();
10080
return;
10181
}
10282
else if (nInput == 0) {
103-
if (nSingleOrMulti == 1) nInput = 100000;
104-
else if (nSingleOrMulti == 2) nInput = 1000000000;
83+
nInput = 50000000;
10584
}
10685
nReiterationCount = nInput;
10786

10887
// Single Core
10988
if (nSingleOrMulti == 1) {
11089

11190
// Start timer and call the cpu benchmark worker only once for single core
112-
std::cout << "Starting single-core benchmark with " << nReiterationCount << " reiterations, 1 core.\n";
91+
std::cout << wordWrap("Starting single-core benchmark with " + std::to_string(nReiterationCount) + " reiterations, 1 logical core.\n");
11392
slowcolourfn(LGRN, ConfigObjMain.sColourGlobalBack, "Single-core benchmark has started...\n");
11493
ClearKeyboardBuffer();
11594

11695
start = std::chrono::steady_clock::now();
11796

118-
CpuBenchmarkWorker(1);
97+
std::thread SingleCoreWorker(CpuBenchmarkWorker);
98+
99+
while (true) {
100+
101+
if (_kbhit()) {
102+
bCpuStressKeyboardTermination = true;
103+
break;
104+
}
105+
else if (nCurrentReiterationNum >= nReiterationCount) {
106+
break;
107+
}
108+
109+
// Output progress
110+
std::cout << "Progress: " << (nCurrentReiterationNum * 100) / nReiterationCount << "% \r";
111+
112+
// Optimisation to prevent output messing up with cpu speed
113+
sleep(10);
114+
}
115+
116+
StopCpuStress = true;
117+
118+
SingleCoreWorker.join();
119119

120120
// Stop timer
121121
end = std::chrono::steady_clock::now();
122122
ldElapsedTime = end - start;
123123

124124
// Output results
125125
if (bCpuStressKeyboardTermination == false) {
126-
std::cout << "Single-core benchmark complete. Your computer took " << ldElapsedTime.count() << " seconds to complete the test.\nCompare that with your friend's scores!\n";
126+
std::cout << "Progress: 100%";
127+
std::cout << wordWrap("\n\nSingle-core benchmark complete. Your computer took " + std::to_string(ldElapsedTime.count()) + " seconds to complete the test.\nCompare that with your friend's scores!\n");
127128
}
128129
else {
129130
colour(YLW, ConfigObjMain.sColourGlobalBack);
130131
ClearKeyboardBuffer();
131-
std::cout << "\nThe single-core benchmark was terminated by a keyboard press.\nExiting...\n";
132+
std::cout << wordWrap("\n\nThe single-core benchmark was terminated by a keyboard press.");
133+
Exiting();
132134
colour(ConfigObjMain.sColourGlobal, ConfigObjMain.sColourGlobalBack);
133135
}
136+
137+
// Reset values to default
138+
bCpuStressKeyboardTermination = false;
139+
nReiterationCount = 0;
140+
StopCpuStress = false;
141+
nCurrentReiterationNum = 0;
134142
}
135143

136144
// Multi Core
137145
else if (nSingleOrMulti == 2) {
138146

139147
// Make std::vector array for threads in next step
140148
std::vector<std::thread> vThreads(nLogicalCoreCount);
141-
unsigned long long int nPreviousReiterationNum = 0;
142149

143150
// Start timer and call the cpu benchmark worker nLogicalCoreCount times
144-
std::cout << "Starting multi-core benchmark with " << nReiterationCount << " reiterations, " << nLogicalCoreCount << " logical cores.\n";
151+
std::cout << wordWrap("Starting multi-core benchmark with " + std::to_string(nReiterationCount) + " reiterations, " + std::to_string(nLogicalCoreCount) + " logical cores.\n");
145152
slowcolourfn(LGRN, ConfigObjMain.sColourGlobalBack, "Multi-core benchmark has started...\n");
146153
ClearKeyboardBuffer();
147154

148155
start = std::chrono::steady_clock::now();
149156
for (int i = 0; i < nLogicalCoreCount; i++) {
150-
vThreads[i] = std::thread(CpuBenchmarkWorker, 2);
157+
vThreads[i] = std::thread(CpuBenchmarkWorker);
151158
}
152159

153160
while (true) {
@@ -160,13 +167,11 @@ void CpuBenchmark(short int nSingleOrMulti, long long int nArgNum = -1) {
160167
break;
161168
}
162169

170+
// Output progress
171+
std::cout << "Progress: " << (nCurrentReiterationNum * 100) / nReiterationCount << "% \r";
163172

164-
if (nPreviousReiterationNum < nCurrentReiterationNum) { // <-- Speed is required to make the percentage output operation accurate,
165-
nPreviousReiterationNum = nCurrentReiterationNum; // <-- so these two lines were put right next to each other to make that possible.
166-
// Output progress
167-
std::cout << "Progress: " << (nPreviousReiterationNum * 100) / nReiterationCount << "% \r";
168-
}
169-
173+
// Optimisation to prevent output messing up with cpu speed
174+
sleep(10);
170175
}
171176

172177
StopCpuStress = true;
@@ -181,11 +186,13 @@ void CpuBenchmark(short int nSingleOrMulti, long long int nArgNum = -1) {
181186

182187
// Output results
183188
if (bCpuStressKeyboardTermination == false) {
184-
std::cout << "Multi-core benchmark complete. Your computer took " << ldElapsedTime.count() << " seconds to complete the test.\nCompare that with your friend's scores!\n";
189+
std::cout << "Progress: 100%";
190+
std::cout << wordWrap("\n\nMulti-core benchmark complete. Your computer took " + std::to_string(ldElapsedTime.count()) + " seconds to complete the test.\nCompare that with your friend's scores!\n");
185191
}
186192
else {
187193
colour(YLW, ConfigObjMain.sColourGlobalBack);
188-
std::cout << "\nThe multi-core benchmark was terminated by a keyboard press.\nExiting...\n";
194+
std::cout << wordWrap("\n\nThe single-core benchmark was terminated by a keyboard press.");
195+
Exiting();
189196
ClearKeyboardBuffer();
190197
colour(ConfigObjMain.sColourGlobal, ConfigObjMain.sColourGlobalBack);
191198
}
@@ -202,35 +209,71 @@ void CpuBenchmark(short int nSingleOrMulti, long long int nArgNum = -1) {
202209
void CpuStressTest(short int nSingleOrMulti, bool bIsArgument = false) {
203210

204211
if (bIsArgument == false) {
205-
std::cout << "Remember, you can always stop the stress test by pressing any key while it's going.\nPress any key to begin the test, or ESC to terminate...";
212+
213+
if (nSingleOrMulti == 1) {
214+
CentreColouredText(" ___CPU SINGLE-CORE STRESS TEST___ ", 1);
215+
216+
colour(GRN, ConfigObjMain.sColourGlobalBack);
217+
std::cout << wordWrap("\nWelcome to the Single-Core CPU Stress Test!\n");
218+
colour(ConfigObjMain.sColourGlobal, ConfigObjMain.sColourGlobalBack);
219+
}
220+
else if (nSingleOrMulti == 2) {
221+
CentreColouredText(" ___CPU MULTI-CORE STRESS TEST___ ", 1);
222+
223+
colour(GRN, ConfigObjMain.sColourGlobalBack);
224+
std::cout << wordWrap("\nWelcome to the Multi-Core CPU Stress Test!\n");
225+
colour(ConfigObjMain.sColourGlobal, ConfigObjMain.sColourGlobalBack);
226+
}
227+
228+
std::cout << wordWrap("Remember, you can always stop the stress test by pressing any key while it's going.\nPress any key to begin the test, or ESC to terminate...");
206229
char cKeyCST = _getch();
207230
if (cKeyCST == 27) {
208231
colour(YLW, ConfigObjMain.sColourGlobalBack);
209-
std::cout << "\nESC pressed. Exiting...\n";
232+
std::cout << "\nESC pressed.\nExiting...\n";
210233
colour(ConfigObjMain.sColourGlobal, ConfigObjMain.sColourGlobalBack);
211234
return;
212235
}
213236
}
214237

215238
slowcolourfn(LGRN, ConfigObjMain.sColourGlobalBack, "\nStress test has started...\n");
239+
240+
// Single Core
241+
if (nSingleOrMulti == 1)
242+
{
243+
// Firstly, create thread for stress test
244+
std::thread SingleCoreStressTest(CpuStressTestWorker);
245+
246+
// Then wait for keyboard hit to kill all the processes
247+
while (!_kbhit()) {
248+
sleep(10);
249+
}
250+
251+
// Assume keyboard has been pressed, so set StopCpuStress to true
252+
StopCpuStress = true;
253+
254+
// Wait for thread to react
255+
SingleCoreStressTest.join();
256+
257+
// Put back StopCpuStress to default to not affect next round
258+
StopCpuStress = false;
216259

217-
if (nSingleOrMulti == 1) {
218-
nSingleOrMulti = 1;
219-
CpuStressTestWorker(1);
220260
// CpuStressTestWorker only exits when key is pressed
221261
colour(YLW, ConfigObjMain.sColourGlobalBack);
222-
std::cout << "\nStress test terminated. Exiting...\n";
262+
std::cout << wordWrap("\nStress test terminated.\nExiting...\n");
223263
colour(ConfigObjMain.sColourGlobal, ConfigObjMain.sColourGlobalBack);
224264
}
225-
else if (nSingleOrMulti == 2) {
265+
266+
// Multi Core
267+
else if (nSingleOrMulti == 2)
268+
{
226269
nSingleOrMulti = 2;
227270

228271
// Make std::vector array for threads in next step
229272
std::vector<std::thread> vThreads(nLogicalCoreCount);
230273

231274
// Create threads until max number of logical cores hit
232275
for (int i = 0; i < nLogicalCoreCount; i++) {
233-
vThreads[i] = std::thread(CpuStressTestWorker, 2);
276+
vThreads[i] = std::thread(CpuStressTestWorker);
234277
}
235278
// Then wait for keyboard hit to kill all the processes
236279
while (!_kbhit()) {
@@ -249,10 +292,21 @@ void CpuStressTest(short int nSingleOrMulti, bool bIsArgument = false) {
249292
StopCpuStress = false;
250293

251294
colour(YLW, ConfigObjMain.sColourGlobalBack);
252-
std::cout << "\nStress test terminated. Exiting...\n";
295+
std::cout << wordWrap("\nStress test terminated.\nExiting...\n");
253296
colour(ConfigObjMain.sColourGlobal, ConfigObjMain.sColourGlobalBack);
254297
}
255298

256299
ClearKeyboardBuffer();
257300
return;
258-
}
301+
}
302+
303+
// Lucas Lehmer Primality Stress Test
304+
/*
305+
long long int s = 4;
306+
long long int randnum = RandNum(0, 50);
307+
long long int m = std::powl(2, randnum) - 1;
308+
int nIterator = 0;
309+
while (++nIterator < randnum - 2) {
310+
s = ((s * s) - 2) % m;
311+
}
312+
*/

0 commit comments

Comments
 (0)