def PrintStatus(s): """Prints a bold underlined status message"""
sys.stdout.write("\n")
sys.stdout.write(BOLD)
sys.stdout.write(UNDERLINE)
sys.stdout.write(s)
sys.stdout.write(ENDCOLOR)
sys.stdout.write("\n")
def PrintCommand(cmd, env=None): """Prints a bold line of a shell command that is being run""" if not QUIET:
sys.stdout.write(BOLD) if env: for k,v in env.iteritems(): if" " in v and "\"" not in v:
sys.stdout.write("%s=\"%s\" " % (k, v.replace("\"", "\\\""))) else:
sys.stdout.write("%s=%s " % (k, v))
sys.stdout.write(" ".join(cmd))
sys.stdout.write(ENDCOLOR)
sys.stdout.write("\n")
class ExecutionException(Exception): """Thrown to cleanly abort operation."""
def __init__(self,*args,**kwargs):
Exception.__init__(self,*args,**kwargs)
class Adb(object): """Encapsulates adb functionality."""
def Exec(self, cmd, stdout=None, stderr=None): """Runs an adb command, and prints that command to stdout.
Raises:
ExecutionException: if the adb command returned an error.
Example:
adb.Exec("shell", "ls") will run "adb shell ls" """
cmd = self._command + cmd
PrintCommand(cmd)
result = subprocess.call(cmd, stdout=stdout, stderr=stderr) if result:
raise ExecutionException("adb: %s returned %s" % (cmd, result))
def WaitForDevice(self): """Waits for the android device to be available on usb with adbd running."""
self.Exec(["wait-for-device"])
def Run(self, cmd, stdout=None, stderr=None): """Waits for the device, and then runs a command.
Raises:
ExecutionException: if the adb command returned an error.
Example:
adb.Run("shell", "ls") will run "adb shell ls" """
self.WaitForDevice()
self.Exec(cmd, stdout=stdout, stderr=stderr)
def Get(self, cmd): """Waits for the device, and then runs a command, returning the output.
Raises:
ExecutionException: if the adb command returned an error.
Example:
adb.Get(["shell", "ls"]) will run "adb shell ls" """
self.WaitForDevice()
cmd = self._command + cmd
PrintCommand(cmd) try:
text = subprocess.check_output(cmd) return text.strip()
except subprocess.CalledProcessError as ex:
raise ExecutionException("adb: %s returned %s" % (cmd, ex.returncode))
def Shell(self, cmd, stdout=None, stderr=None): """Runs an adb shell command
Args:
cmd: The command to run.
Raises:
ExecutionException: if the adb command returned an error.
Example:
adb.Shell(["ls"]) will run "adb shell ls" """
cmd = ["shell"] + cmd
self.Run(cmd, stdout=stdout, stderr=stderr)
def GetProp(self, name): """Gets a system property from the device.""" return self.Get(["shell", "getprop", name])
def Reboot(self): """Reboots the device, and waits for boot to complete."""
# Reboot
self.Run(["reboot"])
# Wait until it comes back on adb
self.WaitForDevice()
# Poll until the system says it's booted while self.GetProp("sys.boot_completed") != "1":
time.sleep(2)
# Dismiss the keyguard
self.Shell(["wm", "dismiss-keyguard"]);
def GetBatteryProperties(self): """A dict of the properties from adb shell dumpsys battery"""
def ConvertVal(s): if s == "true": returnTrue
elif s == "false": returnFalse else: try: returnint(s)
except ValueError: return s
text = self.Get(["shell", "dumpsys", "battery"])
lines = [line.strip() for line in text.split("\n")][1:]
lines = [[s.strip() for s in line.split(":", 1)] for line in lines]
lines = [(k,ConvertVal(v)) for k,v in lines] return dict(lines)
def GetBatteryLevel(self): """Returns the battery level""" return self.GetBatteryProperties()["level"]
def CurrentTimestamp(): """Returns the current time in a format suitable for filenames.""" return datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
def ParseOptions(): """Parse the command line options.
Returns an argparse options object. """
parser = argparse.ArgumentParser(description="Run monkeys and collect the results.")
parser.add_argument("--dir", action="store",
help="output directory for results of monkey runs")
parser.add_argument("--events", action="store", type=int, default=125000,
help="number of events per monkey run")
parser.add_argument("-p", action="append", dest="packages",
help="package to use (default is a set of system-wide packages")
parser.add_argument("--runs", action="store", type=int, default=10000000,
help="number of monkey runs to perform")
parser.add_argument("--type", choices=["crash", "anr"],
help="only stop on errors of the given type (crash or anr)")
parser.add_argument("--description", action="store",
help="only stop if the error description contains DESCRIPTION")
options = parser.parse_args()
if not options.dir:
options.dir = "monkeys-%s" % CurrentTimestamp()
# Set up the device a little bit
PrintStatus("Setting up the device")
adb.Run(["root"])
time.sleep(2)
adb.WaitForDevice()
adb.Run(["remount"])
time.sleep(2)
adb.WaitForDevice()
adb.Shell(["echo ro.audio.silent=1 > /data/local.prop"])
adb.Shell(["chmod 644 /data/local.prop"])
# Figure out how many leading zeroes we need.
pattern = "%%0%dd" % len(str(options.runs-1))
# Make the output directory if os.path.exists(options.dir) and not os.path.isdir(options.dir):
sys.stderr.write("Output directory already exists and is not a directory: %s\n"
% options.dir)
sys.exit(1)
elif not os.path.exists(options.dir):
os.makedirs(options.dir)
# Run the tests for run in range(1, options.runs+1):
PrintStatus("Run %d of %d: %s" % (run, options.runs,
datetime.datetime.now().strftime("%A, %B %d %Y %I:%M %p")))
# Reboot and wait for30 seconds to let the system quiet down so the
# log isn't polluted with all the boot completed crap. ifTrue:
adb.Reboot()
PrintCommand(["sleep", "30"])
time.sleep(30)
# Monkeys can outrun the battery, so if it's getting low, pause to
# let it charge. ifTrue:
targetBatteryLevel = 20 whileTrue:
level = adb.GetBatteryLevel() if level > targetBatteryLevel: break
print "Battery level is %d%%. Pausing to let it charge above %d%%." % (
level, targetBatteryLevel)
time.sleep(60)
# Run monkeys
cmd = [ "monkey", "-c", "android.intent.category.LAUNCHER", "--ignore-security-exceptions", "--monitor-native-crashes", "-v", "-v", "-v"
] for pkg in options.packages:
cmd.append("-p")
cmd.append(pkg) if options.type == "anr":
cmd.append("--ignore-crashes")
cmd.append("--ignore-native-crashes") if options.type == "crash":
cmd.append("--ignore-timeouts") if options.description:
cmd.append("--match-description")
cmd.append("'" + options.description + "'")
cmd.append(str(options.events)) try:
adb.Shell(cmd, stdout=monkeyFile, stderr=monkeyFile)
needReport = False
except ExecutionException:
# Monkeys failed, take a bugreport
bugreportFile = file(bugreportFilename, "w")
adb.Shell(["bugreport"], stdout=bugreportFile, stderr=None)
needReport = True finally:
monkeyFile.close() try:
logcatProcess.terminate()
except OSError:
pass # it must have died on its own
logcatThread.join()
logcatFile.close() if bugreportFile:
bugreportFile.close()
if needReport:
# Generate the html
cmd = ["bugreport", "--monkey", monkeyFilename, "--html", htmlFilename, "--logcat", logcatFilename, bugreportFilename]
PrintCommand(cmd)
result = subprocess.call(cmd)
if __name__ == "__main__":
main()
# vim: set ts=2 sw=2 sts=2 expandtab nocindent autoindent:
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.22 Sekunden
(vorverarbeitet am 2026-07-07)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.