系统相关
首页 > 系统相关> > 内存测试,将内存数据直接写入csv文件中

内存测试,将内存数据直接写入csv文件中

作者:互联网

import subprocess
import csv
import re
import os
import time

'''
将内存数据直接写入csv文件中
'''
class Meminfo(object):
    meminfo_all=[]
    def run_monkey(self):
        monkey=subprocess.Popen('adb shell monkey -p cn.rainbow.westore -s 200 -v 1000 --ignore-timeouts --ignore-crashes --throttle 300',stdout=subprocess.PIPE,stdin=subprocess.PIPE)
        return monkey.stdout.read()

    def run_meminfo(self):
        meminfo=subprocess.Popen('adb shell dumpsys meminfo cn.rainbow.westore',stdout=subprocess.PIPE,stdin=subprocess.PIPE)
        # return meminfo.stdout.read()
        meminfo_info=meminfo.stdout.read()
        # print(meminfo_info)
        Native_info=re.findall(r'Native Heap *(\d+) ',str(meminfo_info,'utf-8'))
        Dalvik_info=re.findall('Dalvik Heap *(\d+) ',str(meminfo_info,'utf-8'))
        self.meminfo_all.append(Native_info[0])
        self.meminfo_all.append(Dalvik_info[0])
        return self.meminfo_all

    def write_csv(self):
        if os.path.isfile('test_meminfo1.csv'):
            with open('test_meminfo1.csv','a',newline='')as f:
                csv_write=csv.writer(f,dialect='excel')
                csv_write.writerow(self.meminfo_all)
        else:
            with open('test_meminfo1.csv','w',newline='')as f:
                csv_write=csv.writer(f,dialect='excel')
                csv_write.writerow(['Native Heap','Dalvik Heap'])
                csv_write.writerow(self.meminfo_all)

if __name__=='__main__':
    S=Meminfo()
    a=S.run_monkey()
    while True:
        S.run_meminfo()
        S.write_csv()
        S.meminfo_all.clear()
        time.sleep(1)
        # if '// Monkey finished' in a:
        #     break

 

标签:info,meminfo,self,写入,subprocess,write,内存,csv
来源: https://blog.csdn.net/lssrain/article/details/100071838