-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperfsnippet.sh
More file actions
358 lines (276 loc) · 8.65 KB
/
perfsnippet.sh
File metadata and controls
358 lines (276 loc) · 8.65 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/bin/bash
[[ "" == "$PS_INTERVAL_DEFAULT" ]] && \
export readonly PS_INTERVAL_DEFAULT=5
[[ "" == "$PS_DURATION_DEFAULT" ]] && \
export readonly PS_DURATION_DEFAULT=300
[[ "" == "$PS_OUTPUTDIR_DEFAULT" ]] && \
export readonly PS_OUTPUTDIR_DEFAULT=output
export ENABLE_DEBUG
export ENABLE_MEMINFO
export ENABLE_CPUINFO
export ENABLE_GFXINFO
export CONFIG_PS_INTERVAL
export CONFIG_PS_DURATION
export CONFIG_PS_ADBTARGET
export REQUEST_STARTDATETIME
export REQUEST_OUTPUTPATH
export REQUEST_OUTPUTFILE
export REQUEST_TERMINATE
export TESTSTEP_START
export TESTSTEP_DURATIONEND
export TESTSTEP_END
export TESTSTEP_ELPASED
export TESTSTEP_INDEX
export TESTSTEP_RECORD
export PRINTSTEP_FILENAME
function perfsnippet_parse() {
REQUEST_TERMINATE=false
ENABLE_DEBUG=false
[[ "1" == "$ps_debug" ]] && { \
ENABLE_DEBUG=true
}
ENABLE_MEMINFO=true
[[ "1" == "$ps_meminfo_disabled" ]] && { \
ENABLE_MEMINFO=false
}
ENABLE_CPUINFO=true
[[ "1" == "$ps_cpuinfo_disabled" ]] && { \
ENABLE_CPUINFO=false
}
ENABLE_GFXINFO=true
[[ "1" == "$ps_gfxinfo_disabled" ]] && { \
ENABLE_GFXINFO=false
}
CONFIG_PS_INTERVAL=$PS_INTERVAL_DEFAULT
[[ "0" -lt "$ps_interval" ]] && { \
CONFIG_PS_INTERVAL=$ps_interval
}
CONFIG_PS_DURATION=$PS_DURATION_DEFAULT
[[ "0" -lt "$ps_duration" ]] && { \
CONFIG_PS_DURATION=$ps_duration
}
CONFIG_PS_ADBTARGET=
[[ "" -ne "$ps_adbtarget" ]] && { \
CONFIG_PS_ADBTARGET=$ps_adbtarget
}
}
function perfsnippet_testplan_print() {
echo "ENABLE_DEBUG\t$ENABLE_DEBUG"
echo "ENABLE_MEMINFO\t$ENABLE_MEMINFO"
echo "ENABLE_CPUINFO\t$ENABLE_CPUINFO"
echo "ENABLE_GFXINFO\t$ENABLE_GFXINFO"
echo "CONFIG_PS_INTERVAL\t$CONFIG_PS_INTERVAL"
echo "CONFIG_PS_DURATION\t$CONFIG_PS_DURATION"
}
function perfsnippet_stop() {
echo PerfSnippet STOPPING
[[ "true" == "$ENABLE_MEMINFO" ]] && { \
mem_entry_exit
}
[[ "true" == "$ENABLE_CPUINFO" ]] && { \
cpu_entry_exit
}
# TODO
# [[ "true" == "$ENABLE_GFXINFO" ]] && { \
# gfx_entry_exit
# }
printer_finalize
}
function perfsnippet_testloop_start() {
perfsnippet_printstep_prerun
perfsnippet_teststep_prerun
while
[[ "true" == "$REQUEST_TERMINATE" ]] && break;
let ++TESTSTEP_INDEX
local now=$(timing_print_nowsecond)
perfsnippet_teststep_run
perfsnippet_printstep_run
local elpased=`timing_print_elpasedsecond $now`
let TESTSTEP_ELPASED+=elpased
perfsnippet_teststep_once "$elpased"
[[ "false" == $(perfsnippet_testplan_review) ]]
do :; done
perfsnippet_teststep_postrun
perfsnippet_printstep_postrun
}
function perfsnippet_start() {
perfsnippet_testplan_print
echo PerfSnippet STARTING
perfsnippet_testloop_start
perfsnippet_finish
}
function perfsnippet_finish() {
echo PerfSnippet FINISHED
[[ "true" == "$ENABLE_MEMINFO" ]] && \
mem_recorder_exit
[[ "true" == "$ENABLE_CPUINFO" ]] && \
cpu_recorder_exit
# [[ "true" == "$ENABLE_GFXINFO" ]] && { \
# module_import gfx/utils.sh
# }
plot_exit
printer_exit
utils_exit
timing_exit
module_exit
}
function perfsnippet_teststep_prerun() {
TESTSTEP_INDEX=0
TESTSTEP_ELPASED=0
TESTSTEP_END=0
TESTSTEP_START=$(timing_print_nowsecond)
TESTSTEP_DURATIONEND=$(timing_print_timeoutsecond $CONFIG_PS_DURATION)
local title="PerfSnippet record"
local generatedby="PerfSnippet"
local datetime="$REQUEST_STARTDATETIME"
local description="Some performance data of Android device"
# 1. Print gnuplot data file header
local header="`plot_datafile_generateheader "$title" "$generatedby" "$datetime" "$description"`"
printer_println "$header"
printer_println "#"
# 2. Print PerfSnippet parameters
local testplan="`perfsnippet_testplan_print`"
printer_println "#" $testplan
printer_println "#"
# 3. Print table column items
local tableitem="`perfsnippet_generatetableitem`"
printer_println "#" $tableitem
}
function perfsnippet_teststep_run() {
perfsnippet_printdebug "PerfSnippet RUNNING"
[[ "true" == "$ENABLE_MEMINFO" ]] && { \
TESTSTEP_RECORD="$TESTSTEP_RECORD`mem_recorder_print`"
}
perfsnippet_printdebug "teststep_run MEM: $TESTSTEP_RECORD"
[[ "true" == "$ENABLE_CPUINFO" ]] && { \
cpu_recorder_statstep
local cpustat="`cpu_recorder_get`"
TESTSTEP_RECORD="$TESTSTEP_RECORD $cpustat"
}
perfsnippet_printdebug "teststep_run CPU: $TESTSTEP_RECORD"
[[ "true" == "$ENABLE_GFXINFO" ]] && { \
local get_fps="`source gfx/get_fps.sh`"
local fps="`echo -e "$get_fps" | awk '{print $2}'`"
TESTSTEP_RECORD="$TESTSTEP_RECORD $fps"
}
perfsnippet_printdebug "teststep_run GFX: $TESTSTEP_RECORD"
}
# 一小步测试步骤完成,执行间隔休眠使其满足间隔要求,根据interval-elpased计算本次需要休眠的时间
function perfsnippet_teststep_once() {
TESTSTEP_RECORD=
local elpased="$1"
local would_sleep="$(echo $CONFIG_PS_INTERVAL-$elpased | bc)"
[[ "0" -gt "$would_sleep" ]] && would_sleep=0
[[ "true" == "$ENABLE_DEBUG" ]] && { \
local progress="$(echo "$TESTSTEP_ELPASED" "$CONFIG_PS_DURATION" | \
awk '{printf "%3.2f%%", $1 * 100 / $2}')"
perfsnippet_printdebug "[$TESTSTEP_ELPASED/$CONFIG_PS_DURATION] $progress" \
"ElpasedSum/elpased/Sleep: $TESTSTEP_ELPASED/$elpased/$would_sleep"
}
[[ "0" -ge "$would_sleep" ]] && return
sleep $would_sleep
let TESTSTEP_ELPASED+=would_sleep
}
# 确认测试计划是否完成,决定是否继续进行测试
function perfsnippet_testplan_review() {
local testplan_reached=`perfsnippet_testplan_reached`
[[ "true" == "$testplan_reached" ]] && { \
perfsnippet_request_terminate
}
echo $testplan_reached
}
# 是否达成测试目标
function perfsnippet_testplan_reached() {
[[ "$TESTSTEP_ELPASED" -lt "$CONFIG_PS_DURATION" ]] && { \
echo false
return
}
echo true
}
function perfsnippet_teststep_postrun() {
TESTSTEP_END=$(timing_print_nowsecond)
perfsnippet_signal
}
function perfsnippet_printstep_prerun() {
PRINTSTEP_FILENAME="perfsnippet_$REQUEST_STARTDATETIME.data"
cpu_recorder_init
REQUEST_OUTPUTPATH="$PS_OUTPUTDIR_DEFAULT/$REQUEST_STARTDATETIME"
REQUEST_OUTPUTFILE="$REQUEST_OUTPUTPATH/$PRINTSTEP_FILENAME"
printer_init "$REQUEST_OUTPUTFILE"
printer_targetinfo
}
function perfsnippet_printstep_run() {
local elpased_seconds=0
let elpased_seconds="TESTSTEP_INDEX*CONFIG_PS_INTERVAL"
perfsnippet_printdebug perfsnippet_printstep_run: "$TESTSTEP_INDEX" "$elpased_seconds" "$TESTSTEP_RECORD"
printer_println "$elpased_seconds" "$TESTSTEP_RECORD"
}
function perfsnippet_printstep_postrun() {
printer_finalize
PRINTSTEP_FILENAME=
}
function perfsnippet_loadmodule() {
source module.sh
module_import utils.sh
module_import timing.sh
module_import printer.sh
module_import gnuplot/gnuplot_wrapper.sh
[[ "true" == "$ENABLE_MEMINFO" ]] && { \
module_import mem/mem_entry.sh
}
[[ "true" == "$ENABLE_CPUINFO" ]] && { \
module_import cpu/cpu_entry.sh
}
[[ "true" == "$ENABLE_GFXINFO" ]] && { \
module_import gfx/utils.sh
}
}
function perfsnippet() {
perfsnippet_signal true
perfsnippet_parse $*
perfsnippet_loadmodule
REQUEST_STARTDATETIME="$ps_startdatetime"
[[ "" == "$REQUEST_STARTDATETIME" ]] && { \
REQUEST_STARTDATETIME=`timing_print_nowdatetime`
}
mkdir -p $PS_OUTPUTDIR_DEFAULT/$REQUEST_STARTDATETIME
perfsnippet_start
}
function perfsnippet_signal() {
[[ "true" == "$1" ]] && { \
trap perfsnippet_request_terminate SIGHUP SIGINT SIGTERM EXIT
return
}
trap - SIGHUP SIGINT SIGTERM EXIT
}
function perfsnippet_request_terminate() {
REQUEST_TERMINATE=true
perfsnippet_stop
}
function perfsnippet_printdebug() {
[[ "true" != "$ENABLE_DEBUG" ]] && return
>&2 \
echo [debug]"\t$*"
}
function perfsnippet_generatetableitem() {
# Index
local items='Index'
# Meminfo
[[ "true" == "$ENABLE_MEMINFO" ]] && {\
items="$items MemFree(MB)"
items="$items MemAvai(MB)"
}
# Cpuinfo
[[ "true" == "$ENABLE_CPUINFO" ]] && {\
items="$items Usage(%)"
items="$items USR(%)"
items="$items SYS(%)"
items="$items Other(%)"
items="$items Idle(%)"
}
# Gfxinfo
[[ "true" == "$ENABLE_GFXINFO" ]] && {\
items="$items FPS"
}
echo -e "$items"
}