您的位置:首页 > 其它

计算QPS

2015-10-20 23:11 197 查看
文件nglog.txt文件如下

时间点 返回的状态吗 用时(秒)

13:14:15 200 10
13:14:15 500 30
13:14:15 200 20
13:14:15 200 10
13:14:15 200 10
13:14:16 200 30
13:14:16 500 36
13:14:16 200 300
13:14:16 500 30

。。。。

求 QPS

JAVA版搓代码如下(仅限功能实现,没有考虑性能,代码优化等问题)

package com.qunar.hotel.qta.order.core.common;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

/**
* Created by ling.yu on 2015/10/19.
*/
public class doNGlog {

public static void main(String[] agrs) throws FileNotFoundException {
String file="E:\\nglog.txt";
readFileByLines(file);

}
/**
* 以行为单位读取文件,常用于读面向行的格式化文件
*/
public static void readFileByLines(String fileName) throws FileNotFoundException {
File file = new File(fileName);
BufferedReader reader = null;
HashMap<String,String> mapRes_sum=new HashMap<String,String>();
HashMap<String,String> mapRes_count=new HashMap<String, String>();
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
//                System.out.println("line " + line + ": " + tempString);

if((tempString.split(" ")[1]).equals("200")){
String keyString=tempString.split(" ")[0].toString();
String valueString2=tempString.split(" ")[2].toString();
Set<String> keySetRes=mapRes_sum.keySet();
if (keySetRes.contains(keyString)){
String valueString1=mapRes_sum.get(keyString);

try {
String  tmp=(Integer.parseInt(valueString1)+Integer.parseInt(valueString2))+"";

int count_value=Integer.parseInt(mapRes_count.get(keyString))+1;
mapRes_count.put(keyString,count_value+"");
mapRes_sum.put(keyString, tmp);
}catch (Exception e){
System.out.println("怎么就put不进去");
}
}else {
mapRes_sum.put(keyString,valueString2);
mapRes_count.put(keyString,"1");
}
}

line++;
}

reader.close();

} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}

Set<String> keySet=mapRes_sum.keySet();
for(String key:keySet){
System.out.println(key+"总用时:"+mapRes_sum.get(key));

System.out.println(key+"出现:"+mapRes_count.get(key)+"次");
System.out.println(key+"平均用时:"+Integer.parseInt(mapRes_sum.get(key))/Integer.parseInt(mapRes_count.get(key)));

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: