用途:埋点测试
实现功能:查询较小日志文件,筛选key值出现次数及对应行日志
package com.ni.gm;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class service {
private String key; //输入筛选key值
int outCount =0; //输出点击上报次数
public service(String key){
this.key = key;
}
public void selectLog(String filePath) throws Exception{
//复制文件
//打开copy文件
FileInputStream logFile = new FileInputStream(filePath);
//读取文件
try {
InputStreamReader isr = new InputStreamReader(logFile, "utf8");
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
//正则匹配key
Pattern pattern = Pattern.compile(key,Pattern.CASE_INSENSITIVE); //Pattern.CASE_INSENSITIVE参数控制正则不区分大小写,如果完全匹配去掉该参数
Matcher matcher = pattern.matcher(line);
while(matcher.find()){
//String countline = matcher.group(0); //GROUP(0)全量匹配
outCount +=1;
System.out.println("筛选日志:" + line);
}
}
} catch (Exception e) {
e.printStackTrace();
}
//输出,点击上报次数
System.out.println("筛选次数:"+ outCount);
//输出页面停留时间(待补充)
}
public String copyFile(String srcPath, String targetPath) throws IOException {
File srcFile = new File(srcPath);
File targetFile = new File(targetPath);
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(targetFile);
byte[] buf = new byte[8 * 1024];
int len ;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
in.close();
out.flush();
out.close();
return targetPath;
}
}
主函数
package com.ni.gm;
import org.testng.annotations.Test;
@Test
public class AppTest
{
service se = new service("userdetailpage_nav_item_click");
@Test
public void shouldAnswerWithTrue() throws Exception
{
String filePath = se.copyFile("D:\\log.txt","D:\\log1.txt");
se.selectLog(filePath);
}
}