python分析性能数据(2)

后续改造:
统计cpu,res,uss的数据并自动写入到excel表中

import os
import sys
import time

import numpy as np
import pandas as pd
import xlrd
import xlwt
from xlutils.copy import copy


class Method_manger:
    def __init__(self, file_path, process_name='xxx'):
        self.data_path = file_path
        self.process_name = process_name

    def analyze_data(self):
        """
        file1: 文件路径
        package1: 包名,例如:aiapps

        baseline
        1:统计cpu
        2:统计res
        3:统计uss
        """
        with open(self.data_path, "r") as f:
            aiapps_cpu = []
            remote_cpu = []
            swan0_cpu = []
            swan1_cpu = []
            aiapps_res = []
            remote_res = []
            swan0_res = []
            swan1_res = []
            aiapps_uss = []
            remote_uss = []
            swan0_uss = []
            swan1_uss = []
            return_data = []
            data = f.readlines()
            n = 0
            for line in data:
                list1 = list(line.split())
                # line = line.strip("\n")
                # 宿主
                # 28158 12 256M 193M S  100  13.5 
                if self.process_name in line and ":swan0" not in line and ":remote" not in line and ":swan1" not in line:
                    n = n + 1
                    aiapps_cpu.append(list1[5])
                    aiapps_res.append(list1[2].rstrip("M"))
                    uss = int(list1[2].rstrip("M")) - int(list1[3].rstrip("M"))
                    aiapps_uss.append(uss)
                # :remote
                elif self.process_name in line and "remote" in line:
                    remote_cpu.append(list1[5])
                    remote_res.append(list1[2].rstrip("M"))
                    uss = int(list1[2].rstrip("M")) - int(list1[3].rstrip("M"))
                    remote_uss.append(uss)
                # swan0
                elif self.process_name in line and ":swan0" in line:
                    swan0_cpu.append(list1[5])
                    swan0_res.append(list1[2].rstrip("M"))
                    uss = int(list1[2].rstrip("M")) - int(list1[3].rstrip("M"))
                    swan0_uss.append(uss)
                # # swan1
                elif self.process_name in line and ":swan1" in line:
                    swan1_cpu.append(list1[5])
                    swan1_res.append(list1[2].rstrip("M"))
                    uss = int(list1[2].rstrip("M")) - int(list1[3].rstrip("M"))
                    swan1_uss.append(uss)

            print("列表长度:%s" % n)
            time.sleep(2)

            for i in (aiapps_cpu, remote_cpu, swan0_cpu, swan1_cpu, aiapps_res, remote_res, swan0_res, swan1_res,
                      aiapps_uss, remote_uss, swan0_uss, swan1_uss):

                try:
                    array_num = np.array(i, dtype='float_')
                    time.sleep(0.5)
                    average_num = np.mean(array_num)
                    max_num = np.max(array_num)
                # print("平均值是:%s" % average_num)
                # print("最大值是:%s" % max_num)
                except ValueError:
                    average_num = "NA"
                    max_num = "NA"
                return_data.append(average_num)
                return_data.append(max_num)

        return return_data

    # 设置表格样式
    @staticmethod
    def set_style(name, height, bold=False):
        style = xlwt.XFStyle()
        font = xlwt.Font()
        font.name = name
        font.bold = bold
        font.color_index = 4
        font.height = height
        style.font = font
        return style

    # 写Excel
    def write_excel(self):
        try:
            xls = pd.read_excel('性能数据结果.xls')
            n = xls.shape[0]  # 行数
            # 加载已存在的xls
            old_workbook = xlrd.open_workbook('性能数据结果.xls')
            # 将已存在的excel拷贝进新的excel
            new_workbook = copy(old_workbook)
            # 获取sheet
            new_worksheet = new_workbook.get_sheet(0)

            datasets = self.analyze_data()
            # print(datasets)
            # 第一列
            scene = os.path.basename(self.data_path)
            new_worksheet.write(n + 1, 0, scene)
            for i in range(0, len(datasets)):
                new_worksheet.write(n + 1, i + 1, datasets[i])

            new_workbook.save('性能数据结果.xls')

        except FileNotFoundError:
            f = xlwt.Workbook()
            sheet1 = f.add_sheet('性能采集', cell_overwrite_ok=True)
            row0 = ["主进程平均值(%)", "主进程最大值(%)", "远程进程平均值(%)", "远程进程最大值(%)", "swan0平均值(%)", "swan0最大值(%)",
                    "swan1平均值(%)", "swan1最大值(%)", "主进程平均值(MB)", "主进程最大值(MB)",   "远程进程平均值(MB)", "远程进程最大值(MB)",
                    "swan0平均值(MB)", "swan0最大值(MB)", "swan1平均值(MB)", "swan1最大值(MB)", "主进程平均值(MB)", "主进程最大值(MB)",
                    "远程进程平均值(MB)",  "远程进程最大值(MB)", "swan0平均值(MB)", "swan0最大值(MB)", "swan1平均值(MB)", "swan1最大值(MB)"]
            colum0 = ["场景名"]
            # 写第一行
            sheet1.write_merge(0, 0, 1, 8, "CPU", self.set_style('Arial', 220, True))
            sheet1.write_merge(0, 0, 9, 16, "MEM", self.set_style('Arial', 220, True))
            sheet1.write_merge(0, 0, 17, 24, "USS", self.set_style('Arial', 220, True))

            # 写第二行
            for i in range(0, len(row0)):
                sheet1.write(1, i+1, row0[i], self.set_style('Times New Roman', 220, True))

            # 写第一列第二行
            for i in range(0, len(colum0)):
                sheet1.write(i + 1, 0, colum0[i], self.set_style('Times New Roman', 220, True))

            datasets = self.analyze_data()
            # print(datasets)
            # 写第一列
            scene = os.path.basename(self.data_path)
            sheet1.write(2, 0, scene)

            for i in range(0, len(datasets)):
                sheet1.write(2, i + 1, datasets[i])

            f.save('性能数据结果.xls')


if __name__ == '__main__':
    file = sys.argv[1]
    # process = sys.argv[2]
    a = Method_manger(file)
    a.write_excel()

初版:
下面展示一些 内联代码片

本文紧跟上一篇文章,这次可以绘制多条参数的线,同时利用numpy库对列表进行计算,速度很美丽。

好处:
有了可视化的图形之后对分析场景的性能数据特别方便,当RD让你出一些性能数据时,只要放好场景,执行性能脚本,最后再计算数据。

def test3(package1):
    """绘制两条线"""
    with open("cpu-07-09-04.txt", "r") as f:
        package = []
        remote = []
        swan0 = []
        swan1 = []
        data = f.readlines()
        b = []
        n = 0
        for line in data:
            line = line.strip("\n")
            # time.sleep(0.2)
            # 宿主
            if package1 in line and ":swan0" not in line and ":remote" not in line:
                # print(line)
                b.append(n)
                n = n + 1
                list1 = list(line.split())
                # package.append(list1[2].rstrip("M"))
                package.append(list1[5])
            # :remote
            elif package1 in line and ":swan0" not in line and ":remote" in line:
                list1 = list(line.split())
                # remote.append(list1[2].rstrip("M"))
                remote.append(list1[5])
            # swan0
            elif package1 in line and ":swan0" in line:
                # b.append(n)
                # n = n + 1
                list1 = list(line.split())
                # swan0.append(list1[2].rstrip("M"))
                swan0.append(list1[5])
            # swan1
            if package1 in line and ":swan1" in line:
                # b.append(n)
                # n = n + 1
                list1 = list(line.split())
                # swan1.append(list1[2].rstrip("M"))
                swan1.append(list1[5])
        print(len(package))
        print(len(remote))
        print(len(swan0))
        print(len(swan1))
        print(len(b))
        # package = np.array(package, dtype='float_')
        # print("主进程平均值:%s" % np.mean(package))
        # print("主进程最大值:%s" % np.max(package))
        #
        # remote = np.array(remote, dtype='float_')
        # print("远程进程平均值:%s" % np.mean(remote))
        # print("远程进程最大值:%s" % np.max(remote))
        #
        # swan0 = np.array(swan0, dtype='float_')
        # print("swan0平均值:%s" % np.mean(swan0))
        # print("swan0最大值:%s" % np.max(swan0))
        #
        # swan1 = np.array(swan1, dtype='float_')
        # print("swan1平均值:%s" % np.mean(swan1))
        # print("swan1最大值:%s" % np.max(swan1))
        # print(swan1)
        # print(len(b))

        # fig, ax = plt.subplots()
        # plt.xlabel('time(s)')
        # plt.ylabel('cpu(%)')
        #
        # """set interval for y label"""
        # yticks = range(0, 100, 10)
        # ax.set_yticks(yticks)
        #
        # """set min and max value for axes"""
        # ax.set_ylim([0, 20])
        # ax.set_xlim([0, 300])
        #
        # x = b
        # plt.plot(x, swan1, "x-", label="swan1")
        # # plt.plot(x, remote, "+-", label="remote")
        #
        # """open the grid"""
        # plt.grid(True)
        #
        # plt.legend(bbox_to_anchor=(1.0, 1), loc=1, borderaxespad=0.)
        #
        # plt.show()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值