@@ -3,76 +3,43 @@ void colour(std::string, std::string);
3
3
void ClearKeyboardBuffer ();
4
4
void slowcolourfn (std::string, std::string, std::string);
5
5
void slowcharCentredFn (bool , std::string);
6
- long double NumInput (std::string);
7
6
std::string StrInput (std::string);
8
7
void sleep (long long int );
9
8
void Exiting ();
10
9
11
10
std::atomic<bool > StopCpuStress = false ;
12
11
// Get logical core count of cpu
13
12
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 ;
16
15
bool bCpuStressKeyboardTermination = false ;
17
16
18
17
extern ConfigFileSystem ConfigObjMain;
19
18
20
19
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 );
40
28
}
41
-
29
+
42
30
return ;
43
31
}
44
32
45
- // 1 for single core, 2 for multi core
46
- void CpuBenchmarkWorker (short int nSingleOrMultiCore ) {
33
+ // Worker for CPU Benchmark
34
+ void CpuBenchmarkWorker () {
47
35
long double ldStress = 1.0 ;
48
- unsigned long long int lnBenchmarkReiterationCount = 0 ;
49
36
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++;
74
42
}
75
-
76
43
return ;
77
44
}
78
45
@@ -85,69 +52,109 @@ void CpuBenchmark(short int nSingleOrMulti, long long int nArgNum = -1) {
85
52
86
53
// Take input
87
54
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 << " \n The default number of reiterations is 100,000.\n " ;
90
- else if (nSingleOrMulti == 2 ) std::cout << " \n The default number of reiterations is 1 billion.\n " ;
91
55
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 (" \n Welcome 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 (" \n Welcome 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): > " );
93
74
}
94
75
else nInput = nArgNum;
95
76
96
-
97
77
// Exit if input is less than 0
98
78
if (nInput < 0 ) {
99
79
Exiting ();
100
80
return ;
101
81
}
102
82
else if (nInput == 0 ) {
103
- if (nSingleOrMulti == 1 ) nInput = 100000 ;
104
- else if (nSingleOrMulti == 2 ) nInput = 1000000000 ;
83
+ nInput = 50000000 ;
105
84
}
106
85
nReiterationCount = nInput;
107
86
108
87
// Single Core
109
88
if (nSingleOrMulti == 1 ) {
110
89
111
90
// 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 " ) ;
113
92
slowcolourfn (LGRN, ConfigObjMain.sColourGlobalBack , " Single-core benchmark has started...\n " );
114
93
ClearKeyboardBuffer ();
115
94
116
95
start = std::chrono::steady_clock::now ();
117
96
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 ();
119
119
120
120
// Stop timer
121
121
end = std::chrono::steady_clock::now ();
122
122
ldElapsedTime = end - start;
123
123
124
124
// Output results
125
125
if (bCpuStressKeyboardTermination == false ) {
126
- std::cout << " Single-core benchmark complete. Your computer took " << ldElapsedTime.count () << " seconds to complete the test.\n Compare that with your friend's scores!\n " ;
126
+ std::cout << " Progress: 100%" ;
127
+ std::cout << wordWrap (" \n\n Single-core benchmark complete. Your computer took " + std::to_string (ldElapsedTime.count ()) + " seconds to complete the test.\n Compare that with your friend's scores!\n " );
127
128
}
128
129
else {
129
130
colour (YLW, ConfigObjMain.sColourGlobalBack );
130
131
ClearKeyboardBuffer ();
131
- std::cout << " \n The single-core benchmark was terminated by a keyboard press.\n Exiting...\n " ;
132
+ std::cout << wordWrap (" \n\n The single-core benchmark was terminated by a keyboard press." );
133
+ Exiting ();
132
134
colour (ConfigObjMain.sColourGlobal , ConfigObjMain.sColourGlobalBack );
133
135
}
136
+
137
+ // Reset values to default
138
+ bCpuStressKeyboardTermination = false ;
139
+ nReiterationCount = 0 ;
140
+ StopCpuStress = false ;
141
+ nCurrentReiterationNum = 0 ;
134
142
}
135
143
136
144
// Multi Core
137
145
else if (nSingleOrMulti == 2 ) {
138
146
139
147
// Make std::vector array for threads in next step
140
148
std::vector<std::thread> vThreads (nLogicalCoreCount);
141
- unsigned long long int nPreviousReiterationNum = 0 ;
142
149
143
150
// 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 " ) ;
145
152
slowcolourfn (LGRN, ConfigObjMain.sColourGlobalBack , " Multi-core benchmark has started...\n " );
146
153
ClearKeyboardBuffer ();
147
154
148
155
start = std::chrono::steady_clock::now ();
149
156
for (int i = 0 ; i < nLogicalCoreCount; i++) {
150
- vThreads[i] = std::thread (CpuBenchmarkWorker, 2 );
157
+ vThreads[i] = std::thread (CpuBenchmarkWorker);
151
158
}
152
159
153
160
while (true ) {
@@ -160,13 +167,11 @@ void CpuBenchmark(short int nSingleOrMulti, long long int nArgNum = -1) {
160
167
break ;
161
168
}
162
169
170
+ // Output progress
171
+ std::cout << " Progress: " << (nCurrentReiterationNum * 100 ) / nReiterationCount << " % \r " ;
163
172
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 );
170
175
}
171
176
172
177
StopCpuStress = true ;
@@ -181,11 +186,13 @@ void CpuBenchmark(short int nSingleOrMulti, long long int nArgNum = -1) {
181
186
182
187
// Output results
183
188
if (bCpuStressKeyboardTermination == false ) {
184
- std::cout << " Multi-core benchmark complete. Your computer took " << ldElapsedTime.count () << " seconds to complete the test.\n Compare that with your friend's scores!\n " ;
189
+ std::cout << " Progress: 100%" ;
190
+ std::cout << wordWrap (" \n\n Multi-core benchmark complete. Your computer took " + std::to_string (ldElapsedTime.count ()) + " seconds to complete the test.\n Compare that with your friend's scores!\n " );
185
191
}
186
192
else {
187
193
colour (YLW, ConfigObjMain.sColourGlobalBack );
188
- std::cout << " \n The multi-core benchmark was terminated by a keyboard press.\n Exiting...\n " ;
194
+ std::cout << wordWrap (" \n\n The single-core benchmark was terminated by a keyboard press." );
195
+ Exiting ();
189
196
ClearKeyboardBuffer ();
190
197
colour (ConfigObjMain.sColourGlobal , ConfigObjMain.sColourGlobalBack );
191
198
}
@@ -202,35 +209,71 @@ void CpuBenchmark(short int nSingleOrMulti, long long int nArgNum = -1) {
202
209
void CpuStressTest (short int nSingleOrMulti, bool bIsArgument = false ) {
203
210
204
211
if (bIsArgument == false ) {
205
- std::cout << " Remember, you can always stop the stress test by pressing any key while it's going.\n Press 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 (" \n Welcome 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 (" \n Welcome 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.\n Press any key to begin the test, or ESC to terminate..." );
206
229
char cKeyCST = _getch ();
207
230
if (cKeyCST == 27 ) {
208
231
colour (YLW, ConfigObjMain.sColourGlobalBack );
209
- std::cout << " \n ESC pressed. Exiting ...\n " ;
232
+ std::cout << " \n ESC pressed.\n Exiting ...\n " ;
210
233
colour (ConfigObjMain.sColourGlobal , ConfigObjMain.sColourGlobalBack );
211
234
return ;
212
235
}
213
236
}
214
237
215
238
slowcolourfn (LGRN, ConfigObjMain.sColourGlobalBack , " \n Stress 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 ;
216
259
217
- if (nSingleOrMulti == 1 ) {
218
- nSingleOrMulti = 1 ;
219
- CpuStressTestWorker (1 );
220
260
// CpuStressTestWorker only exits when key is pressed
221
261
colour (YLW, ConfigObjMain.sColourGlobalBack );
222
- std::cout << " \n Stress test terminated. Exiting ...\n " ;
262
+ std::cout << wordWrap ( " \n Stress test terminated.\n Exiting ...\n " ) ;
223
263
colour (ConfigObjMain.sColourGlobal , ConfigObjMain.sColourGlobalBack );
224
264
}
225
- else if (nSingleOrMulti == 2 ) {
265
+
266
+ // Multi Core
267
+ else if (nSingleOrMulti == 2 )
268
+ {
226
269
nSingleOrMulti = 2 ;
227
270
228
271
// Make std::vector array for threads in next step
229
272
std::vector<std::thread> vThreads (nLogicalCoreCount);
230
273
231
274
// Create threads until max number of logical cores hit
232
275
for (int i = 0 ; i < nLogicalCoreCount; i++) {
233
- vThreads[i] = std::thread (CpuStressTestWorker, 2 );
276
+ vThreads[i] = std::thread (CpuStressTestWorker);
234
277
}
235
278
// Then wait for keyboard hit to kill all the processes
236
279
while (!_kbhit ()) {
@@ -249,10 +292,21 @@ void CpuStressTest(short int nSingleOrMulti, bool bIsArgument = false) {
249
292
StopCpuStress = false ;
250
293
251
294
colour (YLW, ConfigObjMain.sColourGlobalBack );
252
- std::cout << " \n Stress test terminated. Exiting ...\n " ;
295
+ std::cout << wordWrap ( " \n Stress test terminated.\n Exiting ...\n " ) ;
253
296
colour (ConfigObjMain.sColourGlobal , ConfigObjMain.sColourGlobalBack );
254
297
}
255
298
256
299
ClearKeyboardBuffer ();
257
300
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