我正在编写一个 Java 类来解析具有以下内容的文件:
[thread 16] INFO - L3: createOrder: min [239.0] max [1245.0] average [488.06]
[thread 16] INFO - L3: translateBarCode: min [9.0] max [132.0] average [31.1]
[thread 11] INFO - L3: createOrder: min [258.0] max [2458.0] average [506.31]
[thread 13] INFO - L3: createOrder: min [243.0] max [1303.0] average [542.57]
[thread 11] INFO - L3: translateBarCode: min [9.0] max [104.0] average [29.79]
[thread 13] INFO - L3: translateBarCode: min [9.0] max [129.0] average [37.94]
[thread 5] INFO - L3: createOrder: min [269.0] max [1269.0] average [479.95]
[thread 5] INFO - L3: translateBarCode: min [9.0] max [124.0] average [30.34]
[thread 3] INFO - L3: createOrder: min [236.0] max [1238.0] average [492.35]
[thread 3] INFO - L3: translateBarCode: min [10.0] max [108.0] average [32.04]
[thread 16] INFO - L3: changeOrder: min [662.0] max [4204.0] average [1379.84]
[thread 17] INFO - L3: createOrder: min [236.0] max [1335.0] average [521.18]
[thread 16] INFO - L3: translateBarCode: min [10.0] max [112.0] average [34.87]
[thread 17] INFO - L3: translateBarCode: min [10.0] max [103.0] average [36.45]
[thread 13] INFO - L3: changeOrder: min [617.0] max [4094.0] average [1520.84]
[thread 13] INFO - L3: translateBarCode: min [9.0] max [108.0] average [31.38]
[thread 11] INFO - L3: changeOrder: min [620.0] max [4099.0] average [1316.38]
[thread 5] INFO - L3: changeOrder: min [647.0] max [4154.0] average [1384.15]
[thread 5] INFO - L3: translateBarCode: min [8.0] max [110.0] average [31.42]
...
...
我能够使用基本的 substr 和 Collections.sort 类型的东西成功地将它变成类似这样的 CVS 格式:
API, Min, Max, Average
capturePayment, 232.0, 1800.0, 687.68
capturePayment, 268.0, 1853.0, 761.44
capturePayment, 301.0, 2612.0, 753.69
capturePayment, 309.0, 2632.0, 766.31
...
...
我的问题是我想将所有重复的 API 时间平均到每个 API 的一个条目中(即每个 API 的平均最小/最大/平均值)。原始文件中有重复项,并且未排序,因此我不确定如何继续。
我最大的问题是 API 的数量并不总是相同的,即可能有 10 个 capturePayment 调用,但有 20 个 createOrders。否则我有一个大致工作的模型。有人可以给我一些指示吗?
[编辑]
使用下面的“registerAPI”解决方案,我就快到了。计算的平均值与 Excel 中应基于的平均值略有不同。这是我的代码。我唯一的想法可能是从 Strings 转换为 Doubles 会破坏精度?
while ((line = br.readLine()) != null) {
if (line.contains("L3")) {
int x,y;
x = line.indexOf("L3: ") + "L3: ".length();
y = line.indexOf(":", x);
String name = line.substring(x,y).trim();
x = line.indexOf("min [") + "min [".length();
y = line.indexOf("]", x);
String min = line.substring(x,y).trim();
x = line.indexOf("max [") + "max [".length();
y = line.indexOf("]", x);
String max = line.substring(x,y).trim();
x = line.indexOf("average [") + "average [".length();
y = line.indexOf("]", x);
String average = line.substring(x,y).trim();
pStreamArray.add(name + ", " + min + ", " + max + ", " + average);
double[] apiValues = new double[3];
apiValues[0] = Double.valueOf(min);
apiValues[1] = Double.valueOf(max);
apiValues[2] = Double.valueOf(average);
parseAPILogs.registerAPI(name, apiValues);
}
}
Iterator iterator = averagePerAPI.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next().toString();
double[] values = averagePerAPI.get(key);
String valueString = "";
for (int i = 0; i < values.length; i++) {
valueString += values[i] + ", ";
}
System.out.println(key + " " + valueString);
pStreamCombined.println(key + " " + valueString);
}
[编辑]
我在上面的代码中发现了数学缺陷 - 3 个数字的平均值不等于前两个数字的平均值,然后平均到第三个数字。
示例:(395+415+412)/3=407.33
(395+415)/2=405 (405+412)/2=408.5