0%

接口访问频率数据统计

通过服务端的接口访问log,按访问的次数,统计用户常用的接口,给接口自动化和用例设计做数据支撑。

主要是通过正则匹配,获取log中的接口数据,统计次数,写入excel,再做个排序或插入个图表,数据就一目了然了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# -*-coding:utf-8 -*-

import os,sys,time,re
import xlwt

fileName = './yuan.log'
newFile = './new.log'
#获取所有接口访问数据
interfaces = []
#接口数据及频率
interface_total = []

#正则提取接口数据
for line in open(fileName):
_interface = ' "POST (.+?) HTTP/1.1" '
interface = re.compile(_interface).findall(line)
if interface:
interface = interface[0]
interfaces.append(interface)
else:
_interface = ' "GET (.+?)\?" '
interface = re.compile(_interface).findall(line)
if interface:
interface = interface[0]
interfaces.append(interface)

#去重数据
_interfaces = set(interfaces)
interfaces_only = [i for i in _interfaces]

#统计重复数据
for item in interfaces_only:
new_item = [item,interfaces.count(item)]
interface_total.append(new_item)

#写入excel表新建excel表空间
xls = xlwt.Workbook()
sheet = xls.add_sheet("Sheet1")
row = 0

for data in interface_total:
interface_name = data[0]
count = data[1]
if interface_name:
sheet.write(row, 0, interface_name)
sheet.write(row, 1, count)
row += 1

xls.save('interface_count.xls')