-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcont_monkey.sh
executable file
·279 lines (259 loc) · 7.57 KB
/
cont_monkey.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/bin/sh
#DEVICE_SN='CDRTCYC3Y16CFDXG'
#ADB_DEVICE="adb -s "$DEVICE_SN
ADB_DEVICE="adb"
#echo $ADB_DEVICE
#MONKEY_SEED='-s 1234567890'
RUN_TIME=1
EVENT_EXERCUTED=0
function normalCat(){
echo *****normal finish*****
mkdir -p normal
LOGPATH=normal/
}
function unknownCat(){
echo *****unknown reason interruption*****
mkdir -p unknown
cd unknown
mkdir -p ${DATETIME}
cd ..
LOGPATH=unknown/${DATETIME}/
}
function anrCat(){
echo *****error cause:ANR*****
mkdir -p ANR
cd ANR
ERROR_POS=$(echo ${ERROR_INFO} | cut -d \/ -f 2 | cut -d \) -f 1 | awk -F . '{print $NF}')
ERROR_MODULE=$(echo ${ERROR_INFO} | awk '{print $3}')
mkdir -p ${ERROR_MODULE}
cd ${ERROR_MODULE}
mkdir -p ${ERROR_POS}
cd ${ERROR_POS}
mkdir -p ${DATETIME}
cd ..
cd ..
cd ..
LOGPATH=ANR/${ERROR_MODULE}/${ERROR_POS}/${DATETIME}/
}
function crashCat(){
echo *****error cause:CRASH*****
ERROR_MODULE=$(cat monkey_log_${DATETIME}.txt | grep 'CRASH:' | awk '{printf $3}')
ERROR_POS=$(cat monkey_log_${DATETIME}.txt | grep '.java:' | head -1 | awk '{printf $3}' | cut -d \( -f 1)
if test -n "$ERROR_POS" ;then
echo 'error cause:CRASH'
echo 'error position:'${ERROR_POS}
mkdir -p CRASH
cd CRASH
mkdir -p ${ERROR_MODULE}
cd ${ERROR_MODULE}
mkdir -p ${ERROR_POS}
cd ${ERROR_POS}
mkdir -p ${DATETIME}
cd ..
cd ..
cd ..
LOGPATH=CRASH/${ERROR_MODULE}/${ERROR_POS}/${DATETIME}/
fi
}
function moveMonkeyLog(){
FILE_SIZE=$(wc -c monkey_log_${DATETIME}.txt | awk '{print int($1/1024/1024)}')
#echo 'FILE_SIZE:'$FILE_SIZE
if test ${FILE_SIZE} -gt 10
then
zip monkey_log_${DATETIME}.zip monkey_log_${DATETIME}.txt
mv monkey_log_${DATETIME}.zip ${LOGPATH}
rm monkey_log_${DATETIME}.txt
else
mv monkey_log_${DATETIME}.txt ${LOGPATH}
fi
}
function pullLogAndMove(){
${ADB_DEVICE} pull /data/system/dropbox/ ${LOGPATH}
cd ${LOGPATH}
mkdir -p log
cp -f *_anr* *_crash* log/
rm *@*
cp -f log/* .
rm -rf log
cd -
${ADB_DEVICE} pull /data/tombstones/ ${LOGPATH}
${ADB_DEVICE} shell rm -f /data/tombstones/*
${ADB_DEVICE} shell rm -f /data/system/dropbox/*
${ADB_DEVICE} shell /system/bin/screencap -p /sdcard/screenshot.png
${ADB_DEVICE} pull /sdcard/screenshot.png ${LOGPATH}
#echo $PWD
FILE_SIZE=$(wc -c main_log_${DATETIME}.txt | awk '{print int($1/1024/1024)}')
#echo 'FILE_SIZE:'$FILE_SIZE
if test ${FILE_SIZE} -gt 10
then
zip main_log_${DATETIME}.zip main_log_${DATETIME}.txt
mv main_log_${DATETIME}.zip ${LOGPATH}
rm main_log_${DATETIME}.txt
else
mv main_log_${DATETIME}.txt ${LOGPATH}
fi
FILE_SIZE=$(wc -c event_log_${DATETIME}.txt | awk '{print int($1/1024/1024)}')
#echo 'FILE_SIZE:'$FILE_SIZE
if test ${FILE_SIZE} -gt 10
then
zip event_log_${DATETIME}.zip event_log_${DATETIME}.txt
mv event_log_${DATETIME}.zip ${LOGPATH}
rm event_log_${DATETIME}.txt
else
mv event_log_${DATETIME}.txt ${LOGPATH}
fi
}
function dumpstateAndMove(){
FILE_SIZE=$(wc -c dumpstate_${DATETIME}.txt | awk '{print int($1/1024/1024)}')
#echo 'FILE_SIZE:'$FILE_SIZE
if test ${FILE_SIZE} -gt 10
then
zip dumpstate_${DATETIME}.zip dumpstate_${DATETIME}.txt
mv dumpstate_${DATETIME}.zip ${LOGPATH}
rm dumpstate_${DATETIME}.txt
else
mv dumpstate_${DATETIME}.txt ${LOGPATH}
fi
}
while getopts "r:n:l:s:" arg
do
case ${arg} in
l ) #echo $OPTARG
DATETIME=$(echo $OPTARG | cut -d _ -f 3 | cut -d . -f 1)
#echo $DATETIME
;;
r ) #echo $OPTARG
if test -z "$DATETIME"
then
RUN_TIME=$OPTARG
fi
#echo 'RUN_TIME:'$RUN_TIME
;;
n ) #echo $OPTARG
if test -z "$DATETIME"
then
DEVICE_SN=$OPTARG
ADB_DEVICE="adb -s "${DEVICE_SN}
fi
#echo 'DEVICE_SN:'$DEVICE_SN
;;
s ) #echo $OPTARG
if test -z "$DATETIME"
then
MONKEY_SEED='-s '$OPTARG
fi
#echo 'MONKEY_SEED:'$MONKEY_SEED
;;
? ) echo usage:
echo 'script will run 1 time if no arg found'
echo
echo '-l \<monkey log file\ > - analyse monkey log file. log file name should be monkey_log_yyyymmdd-hhmmss.txt'
echo '-r \<monkey event count\ > - specified monkey test event count'
echo '-n \<device hardware sn\ > - specified device sn for ads'
echo '-s \<monkey test seed\ > - specified monkey test seed'
exit 1
;;
esac
done
#echo 'DATETIME:'$DATETIME
# process monkey log file only
if test -n "$DATETIME"
then
echo "*****analyse monkey log*****"
RANDOM_SEED=$(cat monkey_log_${DATETIME}.txt | grep -o 'seed=[0-9]*' | cut -d = -f 2)
EVENT_COUNT=$(cat monkey_log_${DATETIME}.txt | grep -o 'Events injected: [0-9]*' | awk '{print $3}')
if test -z "$EVENT_COUNT" ;then
EVENT_COUNT=0
unknownCat
fi
echo 'random seed:'${RANDOM_SEED}
echo 'event count:'${EVENT_COUNT}
EVENT_EXERCUTED=$(($EVENT_EXERCUTED+$EVENT_COUNT))
echo 'event exercuted:'${EVENT_EXERCUTED}
ERROR_INFO=$(cat monkey_log_${DATETIME}.txt | grep 'ANR in')
if test -n "$ERROR_INFO" ;then
anrCat
moveMonkeyLog
fi
if test -z "$ERROR_INFO" ;then
ERROR_INFO=$(cat monkey_log_${DATETIME}.txt | grep 'CRASH:')
if test -n "$ERROR_INFO" ;then
crashCat
moveMonkeyLog
fi
if test -z "$ERROR_INFO" ;then
normalCat
moveMonkeyLog
fi
fi
# run monkey for specified events count
else
if test -z ${RUN_TIME}
then
echo RUN_TIME is null
exit 1
fi
while test ${EVENT_EXERCUTED} -lt ${RUN_TIME}
do
# reboot devices for next run
echo "*****reboot device*****"
${ADB_DEVICE} kill-server
${ADB_DEVICE} reboot
# wait for device connection
echo "*****wait for device*****"
${ADB_DEVICE} wait-for-device
${ADB_DEVICE} push blacklist.txt /data/
# run monkey with a random seed
if test -z "$MONKEY_SEED" ;then
echo "*****run monkey with a random seed*****"
fi
if test -n "$MONKEY_SEED" ;then
echo "****run monkey with specified seed" ${MONKEY_SEED} "*****"
fi
DATETIME=`date +%Y%m%d-%H%M%S`
nohup ${ADB_DEVICE} logcat *:W > main_log_${DATETIME}.txt &
nohup ${ADB_DEVICE} logcat -b events -v time > event_log_${DATETIME}.txt &
#--pct-touch 18 --pct-motion 12 --pct-pinchzoom 2 --pct-trackball 0 --pct-nav 30 --pct-majornav 18 --pct-syskeys 2 --pct-appswitch 2 --pct-flip 1 --pct-anyevent 15 --throttle 50
#$ADB_DEVICE shell monkey --pkg-blacklist-file /data/blacklist.txt --pct-touch 0 --pct-trackball 0 --throttle 50 $MONKEY_SEED -v -v -v $RUN_TIME > monkey_log_$DATETIME.txt
${ADB_DEVICE} shell monkey --pkg-blacklist-file /data/blacklist.txt --pct-majornav 40 --pct-nav 30 --pct-syskeys 20 --throttle 50 --pct-appswitch 5 --pct-anyevent 5 ${MONKEY_SEED} -v -v -v ${RUN_TIME} > monkey_log_${DATETIME}.txt
# analyse monkey log, figure out error catagory and pull log to pc
echo "*****analyse monkey log*****"
RANDOM_SEED=$(cat monkey_log_${DATETIME}.txt | grep -o 'seed=[0-9]*' | cut -d = -f 2)
EVENT_COUNT=$(cat monkey_log_${DATETIME}.txt | grep -o 'Events injected: [0-9]*' | awk '{print $3}')
if test -z "$EVENT_COUNT" ;then
EVENT_COUNT=0
unknownCat
moveMonkeyLog
pullLogAndMove
${ADB_DEVICE} shell dumpstate > dumpstate_${DATETIME}.txt
dumpstateAndMove
fi
echo 'random seed:'${RANDOM_SEED}
echo 'event count:'${EVENT_COUNT}
EVENT_EXERCUTED=$(($EVENT_EXERCUTED+$EVENT_COUNT))
echo 'event exercuted:'${EVENT_EXERCUTED}
ERROR_INFO=$(cat monkey_log_${DATETIME}.txt | grep 'ANR in')
if test -n "$ERROR_INFO" ;then
anrCat
moveMonkeyLog
pullLogAndMove
${ADB_DEVICE} shell dumpstate > dumpstate_${DATETIME}.txt
dumpstateAndMove
fi
if test -z "$ERROR_INFO" ;then
ERROR_INFO=$(cat monkey_log_${DATETIME}.txt | grep 'CRASH:')
if test -n "$ERROR_INFO" ;then
crashCat
moveMonkeyLog
pullLogAndMove
${ADB_DEVICE} shell dumpstate > dumpstate_${DATETIME}.txt
dumpstateAndMove
fi
if test -z "$ERROR_INFO" ;then
normalCat
moveMonkeyLog
pullLogAndMove
fi
fi
done
fi