主要内容

  • 热启动
  • 冷启动
  • 使用python脚本实现监控,分析

热启动

  1. adb shell am start -W org.cnodejs.android.md/.ui.activity.LaunchActivity #打开应用
  2. adb shell input keyevent 3 #Home

冷启动

  1. adb shell am start -W org.cnodejs.android.md/.ui.activity.LaunchActivity
  2. adb shell am force-stop org.cnodejs.android.md

基础功能实现

  1. import subprocess
  2. import time
  3. import csv
  4. def getstartapp_time():
  5. cmd = 'adb shell am start -W -n org.cnodejs.android.md/.ui.activity.LaunchActivity'
  6. p = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
  7. thistime = 0
  8. for line in p.stdout.readlines():
  9. line = line.decode('utf8')
  10. if "ThisTime" in line:
  11. newline = line.split(':')
  12. thistime = newline[1]
  13. thistime = thistime.strip()
  14. return thistime
  15. def force_stopapp():
  16. cmd = 'adb shell am force-stop org.cnodejs.android.md'
  17. subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
  18. def run():
  19. with open('thistime.csv', 'w', newline='') as csvfile:
  20. spamwriter = csv.writer(csvfile, delimiter=' ',
  21. quotechar='|', quoting=csv.QUOTE_MINIMAL)
  22. spamwriter.writerow(['title','time'])
  23. for x in range(20):
  24. thistime = getstartapp_time()
  25. with open('thistime.csv', 'a', newline='') as csvfile:
  26. spamwriter = csv.writer(csvfile, delimiter=' ',
  27. quotechar='|', quoting=csv.QUOTE_MINIMAL)
  28. spamwriter.writerow(["thistime",thistime])
  29. time.sleep(3)
  30. force_stopapp()
  31. time.sleep(1)
  32. run()

Python脚本

  1. import subprocess
  2. import csv
  3. import time
  4. class App(object):
  5. def __init__(self):
  6. self.startTime = 0
  7. self.content=b''
  8. #启动App
  9. def LaunchApp(self):
  10. cmd = 'adb shell am start -W com.android.browser/.BrowserActivity'
  11. self.content=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
  12. #Home
  13. def BackAppHome(self):
  14. cmd = 'adb shell input keyevent 3'
  15. subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
  16. def ForeceStopApp(self):
  17. cmd = 'adb shell am force-stop com.android.browser'
  18. subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
  19. #获取冷启动时间
  20. def GetLaunchedTime(self):
  21. for line in self.content.stdout.readlines():
  22. line = line.decode('utf-8')
  23. if "ThisTime" in line:
  24. self.startTime = line.split(":")[1]
  25. self.startTime =self.startTime.strip()
  26. break
  27. return self.startTime
  28. #控制类
  29. class Controller(object):
  30. def __init__(self, count):
  31. self.app = App()
  32. self.counter = count
  33. self.alldata = [["timestamp", "elapsedtime"]]
  34. #单次测试过程
  35. def testprocess(self):
  36. self.app.LaunchApp()
  37. time.sleep(5)
  38. elpasedtime = self.app.GetLaunchedTime()
  39. self.app.ForeceStopApp()
  40. time.sleep(3)
  41. currenttime = self.getCurrentTime()
  42. self.alldata.append([currenttime, elpasedtime])
  43. #多次执行测试过程
  44. def run(self):
  45. while self.counter >0:
  46. self.testprocess()
  47. self.counter = self.counter - 1
  48. #获取当前的时间戳
  49. def getCurrentTime(self):
  50. currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  51. return currentTime
  52. #数据的存储
  53. def SaveDataToCSV(self):
  54. csvfile = open('startTime2.csv', 'w', newline='')
  55. writer = csv.writer(csvfile)
  56. # self.alldata.decode('utf-8')
  57. for line in self.alldata:
  58. writer.writerow(line)
  59. csvfile.close()
  60. if __name__ == "__main__":
  61. # 设置运行次数
  62. controller = Controller(10)
  63. controller.run()
  64. controller.SaveDataToCSV()