-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.java
More file actions
234 lines (195 loc) · 7.5 KB
/
Main.java
File metadata and controls
234 lines (195 loc) · 7.5 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package prBenchmark;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
/* A brief and simple to-do list for the future:
*
* Implement other classes
* Search of previous files in order to calculate a mean
* GUI
* */
public class Main {
static File log = new File(
"C:\\Users\\Public\\log.txt"); /*
* *Your path goes here* (it's where
* the file is going to be created).
* Make sure you use double back
* slashes (\\), otherwise Java will
* understand this as commands
*/
static Date date = new Date();
static String fileName;
static double totalTime = 0;
static int times = 0;
static int cores;
public static void main(String[] args) throws BenchmarkException {
fileName = date.toString();
Scanner sc = new Scanner(System.in);
System.out.println(
"Options: \n M -> Multicore Test \n S -> Single Core Test \n C -> Test on a custom number of cores \n E -> Exit \nEnter a running mode: \n");
String st = sc.next();
if (st.equalsIgnoreCase("m")) {
cores = Runtime.getRuntime().availableProcessors();
} else if (st.equalsIgnoreCase("s")) {
cores = 1;
} else if (st.equalsIgnoreCase("c")) {
System.out.println("Enter a number of cores: \n");
int c = sc.nextInt();
if (c <= 0 || c > Runtime.getRuntime().availableProcessors()) {
sc.close();
throw new BenchmarkException("Invalid number of cores.");
}
cores = c;
} else {
System.exit(0);
}
sc.close();
System.out.println("Application running on " + cores + " cores.");
for (int i = 1; i <= cores; i++) {
(new Full_CPU_Test()).start();
}
// calculateMean(log); /*It doesn't work yet*/
}
public static void sumTimesAndDisplayResults(double time) throws FileNotFoundException, SigarException {
totalTime += time;
times++;
double totalIterations = 1000 * cores;
if (times == cores) {
StringBuilder sb = new StringBuilder();
Sigar sigar = new Sigar();
String mod = null;
org.hyperic.sigar.CpuInfo[] cpuInfoList = sigar.getCpuInfoList();
for (org.hyperic.sigar.CpuInfo info : cpuInfoList) {
mod = info.getModel();
}
System.out.println("\nCPU Model: " + mod);
System.out.println("----------------------------------------------------------------------------------");
sb.append(System.lineSeparator());
sb.append("----------------------------------------------------------------------------------");
sb.append(System.lineSeparator());
sb.append("\nCPU Model: " + mod);
sb.append("\nThe average time per core is: " + totalTime / cores + " seconds.");
sb.append(System.lineSeparator());
sb.append("----------------------------------------------------------------------------------");
sb.append(System.lineSeparator());
System.out.println("The average time per core is: " + totalTime / cores + " seconds.");
System.out.println("----------------------------------------------------------------------------------");
sb.append("The total number of iterations is: " + totalIterations + " millions,");
sb.append(System.lineSeparator());
System.out.println("The total number of iterations is: " + totalIterations + " millions,");
System.out.println("Total time spent on " + Full_CPU_Test.getAuxIterations() * Full_CPU_Test.getSprints()
+ " iterations was: " + Full_CPU_Test.getTimeSpent() + ".");
sb.append("Total time spent on " + Full_CPU_Test.getAuxIterations() * Full_CPU_Test.getSprints()
+ " iterations was: " + Full_CPU_Test.getTimeSpent() + ".");
sb.append(System.lineSeparator());
sb.append("---> Those values make a final result of: " + (totalIterations) / (totalTime / cores)
+ " million iterations per second <---");
sb.append(System.lineSeparator());
sb.append("----------------------------------------------------------------------------------");
sb.append(System.lineSeparator());
System.out.println("---> Those values make a final result of: " + (totalIterations) / (totalTime / cores)
+ " million iterations per second <---");
System.out.println("----------------------------------------------------------------------------------");
writeFile(sb.toString());
}
}
private static void writeFile(String data) throws FileNotFoundException {
try {
if (log.exists() == false) { /*
* If the file doesn't exist, it
* creates a new one in the
* specified path
*/
System.out.println("I made a new file at " + log.getAbsolutePath());
log.createNewFile();
}
PrintWriter out = new PrintWriter(new FileWriter(log, true));
System.out.println("Writing to " + log.getName());
out.append(System.lineSeparator());
out.append("******* " + date + "******* "
+ "\n");/*
* It adds the date to the new log's text and writes
* the results below
*/
out.append(data);
out.close();
} catch (IOException e) {
System.out.println("I couldn't log."); // Something went wrong
}
}
@SuppressWarnings("unused")
private static void calculateMean(File file) {
/*
* This calculates the mean of the results written on your log file.
* Make sure you have at least 4 runs present on your log, otherwise it
* won't work. The more you have, the more accurate the mean will be.
* Also make sure to run your test with everything else closed, as every
* tiny bit of resources being drawn is going to affect the result.
*
* If you see that a particular run gets irregular results, delete that
* part of the log manually, as it would affect the mean.
*/
try {
// Opens the log as a buffered reader
BufferedReader bf = new BufferedReader(new FileReader(log));
/*
* Start a line count and declare a string to hold our current line
* as well as a hit counter and a String array and double array
*/
int linecount = 0;
int hit = 0;
String line;
double[] results = new double[10];
String[] matchingLines = new String[10];
for (int i = 0; i < results.length; i++) {
results[i] = 0;
}
Pattern p = Pattern.compile("\\bThose values make a final result of:\\b", Pattern.CASE_INSENSITIVE);
while ((line = bf.readLine()) != null) {
linecount++;
Matcher m = p.matcher(line);
// indicate all matches on the line
while (m.find()) {
if (matchingLines.length >= hit) {
matchingLines = Arrays.copyOf(matchingLines, 2 * matchingLines.length);
results = Arrays.copyOf(results, 2 * results.length);
}
matchingLines[hit] = line;
String tmpstr = matchingLines[hit];
String asd[] = tmpstr.split(" ");
double dbl = Double.parseDouble(asd[1]);
results[hit] = dbl;
hit++;
System.out.println("String was found at position " + m.start() + " on line " + linecount);
}
}
// Close the file after done searching
bf.close();
if (hit >= 4) {
double res, sum = 0;
int max = 1;
for (int i = 0; i < results.length; i++) {
if (results[i] != 0) {
sum += results[i];
max = i + 1;
}
}
res = sum / max;
System.out.println("The mean of " + max + " runs is " + res + " million iterations per second.");
}
} catch (IOException e) {
System.out.println("IO Error Occurred: " + e.toString());
}
}
}