Wednesday, June 27, 2012

Using the Android Monkey tool

Android comes with a command-line tool (Monkey) that allows you to stress-test your application.  Monkey generates some random user events and will stop and report any errors that it encounters. It is very useful in helping to flesh out some not so obvious bugs that could be lurking in your application.  Below is an example of how we used it to test one of our applications under development.


cmd> adb shell monkey -p com.sivart.buddingbaby -v 200


Results:
:Monkey: seed=0 count=200
:AllowPackage: com.sivart.buddingbaby
:IncludeCategory: android.intent.category.LAUNCHER
:IncludeCategory: android.intent.category.MONKEY
// Event percentages:
// 0: 15.0%
// 1: 10.0%
// 2: 15.0%
// 3: 25.0%
// 4: 15.0%
// 5: 2.0%
// 6: 2.0%
// 7: 1.0%
// 8: 15.0%
:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10000000;component=com.sivart.buddingbaby/.SplashScreenActivity;end
// Allowing start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.sivart.buddingbaby/.SplashScreenActivity } in package com.sivart.buddingbaby
:Sending Pointer ACTION_MOVE x=-4.0 y=2.0
:Sending Pointer ACTION_UP x=0.0 y=0.0
// activityResuming(com.android.launcher)
// Rejecting resume of package com.android.launcher
:Sending Pointer ACTION_DOWN x=47.0 y=496.0
:Sending Pointer ACTION_UP x=29.0 y=503.0
:Sending Pointer ACTION_DOWN x=255.0 y=143.0
:Sending Pointer ACTION_UP x=255.0 y=143.0
:Sending Pointer ACTION_DOWN x=295.0 y=659.0
:Sending Pointer ACTION_UP x=290.0 y=649.0
:Sending Pointer ACTION_MOVE x=-5.0 y=3.0
:Sending Pointer ACTION_MOVE x=0.0 y=-5.0
// Rejecting start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] cmp=com.android.launcher/com.android.launcher2.Launcher } in package com.android.launcher
:Sending Pointer ACTION_DOWN x=74.0 y=803.0
:Sending Pointer ACTION_UP x=74.0 y=803.0
:Sending Pointer ACTION_MOVE x=3.0 y=-2.0
:Sending Pointer ACTION_UP x=0.0 y=0.0
:Sending Pointer ACTION_MOVE x=-4.0 y=2.0
//[calendar_time:2012-06-22 22:19:07.777 system_uptime:307227]
// Sending event #100
:Sending Pointer ACTION_MOVE x=4.0 y=2.0
:Sending Pointer ACTION_DOWN x=309.0 y=694.0
:Sending Pointer ACTION_UP x=322.0 y=694.0
:Sending Pointer ACTION_MOVE x=0.0 y=-1.0
:Sending Pointer ACTION_DOWN x=294.0 y=319.0
:Sending Pointer ACTION_UP x=294.0 y=319.0
:Sending Pointer ACTION_DOWN x=118.0 y=497.0
:Sending Pointer ACTION_UP x=119.0 y=493.0
:Sending Pointer ACTION_DOWN x=137.0 y=284.0
:Sending Pointer ACTION_UP x=124.0 y=295.0
:Sending Pointer ACTION_DOWN x=258.0 y=403.0
// Rejecting start of Intent { act=android.intent.action.CHOOSER cmp=android/com.android.internal.app.ChooserActivity } in package android
:Sending Pointer ACTION_UP x=273.0 y=403.0
:Sending Pointer ACTION_MOVE x=-5.0 y=-5.0
// Rejecting start of Intent { act=android.intent.action.CHOOSER cmp=android/com.android.internal.app.ChooserActivity } in package android
Events injected: 200
Dropped: keys=0 pointers=0 trackballs=0 flips=0
## Network stats: elapsed time=6268ms (6268ms mobile, 0ms wifi, 0ms not connected)
// Monkey finished


Have you used Monkey successfully?  Leave a comment below to share your results.