Skip to content

Instantly share code, notes, and snippets.

@dextorer
Last active November 2, 2015 11:17
Show Gist options
  • Save dextorer/816280bae2d96ae87a52 to your computer and use it in GitHub Desktop.
Save dextorer/816280bae2d96ae87a52 to your computer and use it in GitHub Desktop.
monkeyrunner - hot start measuring
#! /usr/bin/env monkeyrunner
# hot start: not opening the app for the first time. still, previous processes are killed.
import time
import subprocess
import commands
import sys
import datetime
import re
import os
debug = 0
test_number = 20
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
if debug: print "before connection"
device = MonkeyRunner.waitForConnection()
if debug: print "after connection"
package = 'your_package_name'
startActivity = 'your_start_activity'
# sets the name of the component to start
startComponent = package + '/' + startActivity
device.shell('am force-stop ' + package)
time.sleep(2)
test_counter = 0
times = []
while test_counter < test_number:
# Flush logcat
p = subprocess.call([ "adb", "logcat", "-c" ], shell=False, stdout=subprocess.PIPE)
# Run the component
if debug: print "starting activity"
device.startActivity(component=startComponent)
#p = subprocess.call([ "adb", "logcat", "-d", "ActivityManager:I", "*:S", ">", "mxm.log"], shell=False, stdout=subprocess.PIPE)
time.sleep(3)
os.system('adb logcat -d ActivityManager:I *:S > mxm.log')
if debug: print "grepping logcat"
f = open('app.log', 'r')
for line in f:
m = re.search(r'your_regex_goes_here', line)
# e.g., re.search(r'Displayed\scom\.company\.app\/com\.company\.app\.MainActivity(.*)', line)
if m:
last_part = m.group(1)
result = re.match(r'[\s\+:]*([0-9]{1})s([0-9]{3})ms', last_part)
if not result:
result = re.match(r'([0-9]{3})ms', last_part)
if not result:
continue
seconds = 0
milliseconds = 0
if len(result.groups()) == 2:
seconds = result.group(1)
milliseconds = result.group(2)
else:
milliseconds = result.group(1)
times.append(float(seconds + "." + milliseconds))
print "(", test_counter, ")", "Startup elapsed time: ", seconds + "." + milliseconds
break
f.close()
test_counter = test_counter + 1
device.shell('am force-stop ' + package)
time.sleep(2)
average = sum(times) / len(times)
print "Average: ", average
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment