0

我正在编写一个 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

4

1 回答 1

1

正如我所看到的,您可以使用Map一个普通的容器来实现这一点。

容器是一个具有当前总和的类MINMAXAVERAGE与当前计数一起:

public class APIData {

    private double min;
    private double max;
    private double average;
    private int amount;

    public void addValues(double[] values) {
        min += values[0];
        max += values[1];
        average += values[2];
        amount++;
    }

    public double getAPIMin() {
        return min / amount;
    }

    public double getAPIMax() {
        return max / amount;
    }

    public double getAPIAverage() {
        return average / amount;
    }

}

在地图上:

Map<String,APIData> averagePerAPI = new LinkedHashMap<String,int[]>();

此映射的键是 API 的名称,值是double大小为 3的数组,{MIN, MAX, AVERAGE}由于上述代码的逻辑,您可以按特定顺序保存该数组。

最后,您只需使用地图提供的不同方法来管理地图。例如,registerAPI保存 API 数据的方法会更新现有数据。

public void registerAPI(String apiName, double[] apiValues) {
    if(!averagePerAPI.containsKey(apiName)) {
        APIData data = new APIData();
        data.addValues(apiValues)
        averagePerAPI.put(apiName, data);
    } else {
        averagePerAPI.get(apiName).addValues(apiValues);
    }
}

当您想知道 API 的特定值时,您只需调用getAPIMin()getAPIMax()方法getAPIAverage()

于 2012-10-03T20:55:44.397 回答