利用python分析日志文件

越来越喜欢用python来处理日常问题了,用起来简直太方便。

最近需要分析系统的日志文件,背景是这样的,FPGA和GPU跑相同深度学习的模型网络,输入相同的数据,对比其输出。日志量非常大,如果符合输出结果,日志文件中会有如下条目:

"scores":0.801309,"classes"

那么首先使用cat命令将文件中包含scores的条目全部按行保存:

cat fpgadetection.log | grep scores > fpga_detection_scores.log

接下来提取scores大于0.8的结果到一个文件,使用python:

srcFile = open('fpga_detection_scores.log', 'r+')
dstFile = open('fpga_detection_scores_0.8.log', 'w+')
lines = srcFile.readlines()

count = 0
for line in lines:
    #print(line)
    data = line.split('scores":')[1].split(',')[0] #[1]表示split后取右边,[0]表示取左右
    print(data)
    if(float(data) > 0.8):
        dstFile.write(line)
        count = count + 1
        print(count, data)

srcFile.close()
dstFile.close()

对比GPU筛选scores后的log文件,还是使用python,由于测试时,FPGA与GPU的输入数据(url)不完全一致,因此,做用fpga_detection_scores_0.8在GPU的日志文件中做一个筛选,把有相同输入的条目保留下来。

srcFile_fpga = open('fpga_detection_scores_0_8.log', 'r+')
srcFile_gpu = open('gpu_detection.log', 'r+')

dstFile_fpga = open('fpga_compare_scores_0.8.log', 'w+')
dstFile_gpu = open('gpu_compare.log', 'w+')

lines_fpga = srcFile_fpga.readlines()
lines_gpu = srcFile_gpu.readlines()
count = 0
for line1 in lines_fpga:
    #print(line)
    url_fpga = line1.split(' ')[0]
    for line2 in lines_gpu:
        url_gpu = line2.split(' ')[0]
        if(url_fpga == url_gpu):
            dstFile_fpga.write(line1)
            dstFile_gpu.write(line2)
            count = count + 1
            print(count)
            break

srcFile_fpga.close()
srcFile_gpu.close()
dstFile_fpga.close()
dstFile_gpu.close()

分析gpu_detection.log中大于0.8的条目,就可以得到一定阈值下的差异结果。

只用了python的readlines和split两个方法,就分分钟搞定日志问题。

最后,python安装建议使用anaconda发行版,这里面集成了numpy、matplotlib等相关的库,非常方便。

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值