lin
2025-07-30 fcd736bf35fd93b563e9bbf594f2aa7b62028cc9
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
#!/bin/bash
 
 
# DEFAULT VALUES
TESTS=()
MIN_TIME=0.0
MIN_ITER=0
BATCH_MODE=0
GRAPH_LINE=0
GRAPH_ZERO=0
VARIABLES=
 
 
# DEFINES
TEST_NAMES=(
  LEVELS_VEC3_RELAXED            LEVELS_VEC4_RELAXED           LEVELS_VEC3_FULL              LEVELS_VEC4_FULL       BLUR_RADIUS_25              # 00-04
  INTRINSIC_BLUR_RADIUS_25       GREYSCALE                     GRAIN                         FISHEYE_FULL           FISHEYE_RELAXED             # 05-09
  FISHEYE_APPROXIMATE_FULL       FISHEYE_APPROXIMATE_RELAXED   VIGNETTE_FULL                 VIGNETTE_RELAXED       VIGNETTE_APPROXIMATE_FULL   # 10-14
  VIGNETTE_APPROXIMATE_RELAXED   GROUP_TEST_EMULATED           GROUP_TEST_NATIVE             CONVOLVE_3X3           INTRINSICS_CONVOLVE_3X3     # 15-19
  COLOR_MATRIX                   INTRINSICS_COLOR_MATRIX       INTRINSICS_COLOR_MATRIX_GREY  COPY                   CROSS_PROCESS_USING_LUT     # 20-24
  CONVOLVE_5X5                   INTRINSICS_CONVOLVE_5X5       MANDELBROT_FLOAT              INTRINSICS_BLEND       INTRINSICS_BLUR_25G         # 25-29
  VIBRANCE                       BW_FILTER                     SHADOWS                       CONTRAST               EXPOSURE                    # 30-34
  WHITE_BALANCE                  COLOR_CUBE                    COLOR_CUBE_3D_INTRINSIC       ARTISTIC1              RESIZE_BI_SCRIPT            # 35-39
  RESIZE_BI_INTRINSIC            POSTERIZE_INVOKE              POSTERIZE_SET                 HISTOGRAM_INTRINSIC    HISTOGRAM_SCRIPT            # 40-44
  MANDELBROT_DOUBLE              BLUR_RADIUS_25_HALF                                                                                            # 45-46
)
 
FUNCTION_NAMES=(
  testLevelsVec3Relaxed          testLevelsVec4Relaxed         testLevelsVec3Full            testLevelsVec4Full     testBlurRadius25            # 00-04
  testIntrinsicBlurRadius25      testGreyscale                 testGrain                     testFisheyeFull        testFishEyeRelaxed          # 05-09
  testFisheyeApproximateFull     testFisheyeApproximateRelaxed testVignetteFull              testVignetteRelaxed    testVignetteApproximateFull # 10-14
  testVignetteApproximateRelaxed testGroupTestEmulated         testGroupTestNative           testConvolve3x3        testIntrinsicsConvolve3x3   # 15-19
  testColorMatrix                testIntrinsicsColorMatrix     testIntrinsicsColorMatrixGrey testCopy               testCrossProcessUsingLUT    # 20-24
  testConvolve5x5                testIntrinsicsConvolve5x5     testMandelbrot                testIntrinsicsBlend    testIntrinsicsBlur25G       # 25-29
  testVibrance                   testBWFilter                  testShadows                   testContrast           testExposure                # 30-34
  testWhiteBalance               testColorCube                 testColorCube3DIntrinsic      testArtistic1          testResizeBiCubicScript     # 35-39
  testResizeBiCubicIntrinsic     testPosterizeInvoke           testPosterizeSet              testHistogramIntrinsic testHistogramScript         # 40-44
  testMandelbrotfp64             testBlurRadius25Half                                                                                           # 45-46
)
 
RUNNER="com.android.rs.imagejb/androidx.test.runner.AndroidJUnitRunner"
TEST_ROOT="com.android.rs.imagejb.ImageProcessingTest"
 
 
# Helper functions
show_help() {
  echo "
    Usage: ${0##*/} [OPTIONS]...
    Do stuff with FILE and write the result to standard output. With no FILE
    or when FILE is -, read standard input.
 
    -h, -?, --?, --help      display this help message
    -t, --time K             set the minimum time (in seconds) each test should run
    -i, --iteration[s] K     set the minimum number of iterations each test should run
    -c, --case[s] T1 [T2]... specify the number corresponding to each test to be run;
                             the test IDs are below
    -b, --batch              run all tests together for speed
    -f, --bestfit            add best fit line to graph
    -z, --zero               set graph's minimum yrange to 0
  "
  echo "Available test cases:"
  for i in `seq 0 $((${#TEST_NAMES[@]} - 1))`; do
    echo "  $i: ${TEST_NAMES[$i]}"
  done
  echo
}
fileexists() {
  [ `adb shell "[ -f $1 ] && echo found"` ]
}
 
 
# display help if blank argument list
if [ $# -eq 0 ]; then
  show_help
  exit
fi
 
# command line parsing
using_cases=0
while [ $# -gt 0 ]; do
  case $1 in
    -h|-\?|--\?|--help)
      show_help
      exit
      ;;
    -t|--time)
      using_cases=0
      if [ -n "$2" ]; then
        MIN_TIME=$2
        shift
        shift
      else
        echo 'ERROR: "--time" requires a non-empty option argument.'
        exit 1
      fi
      ;;
    -i|--iter|--iters|--iteration|--iterations)
      using_cases=0
      if [ -n "$2" ]; then
        MIN_ITER=$2
        shift
        shift
      else
        echo 'ERROR: "--iteration" requires a non-empty option argument.'
        exit 1
      fi
      ;;
    -c|--case|--cases)
      if [ -n "$2" ]; then
        using_cases=1
        shift
      else
        echo 'ERROR: "--case" requires a non-empty option argument.'
        exit 1
      fi
      ;;
    -b|--batch)
      using_cases=0
      BATCH_MODE=1
      shift
      ;;
    -f|--bestfit)
      using_cases=0
      GRAPH_LINE=1
      shift
      ;;
    -z|--zero)
      using_cases=0
      GRAPH_ZERO=1
      shift
      ;;
    all)
      if [ $using_cases -eq 1 ]; then
        using_cases=0
        TESTS=(`seq 0 $((${#TEST_NAMES[@]} - 1))`)
        shift
      else
        echo "ERROR: Invalid option: $1"
      fi
      ;;
    [0-9]*)
      if [ $using_cases -eq 1 ]; then
        TESTS+=($1)
        shift
      else
        echo "ERROR: Invalid option: $1"
        exit 1
      fi
      ;;
    *)
      echo "ERROR: Invalid option: $1"
      exit 1
      ;;
  esac
done
 
# error checking
if [ ${#TESTS[@]} -eq 0 ]; then
  echo "ERROR: need at least one --case"
  exit 1
fi
 
# configure launch variables
if [[ ( $MIN_TIME == "0.0" ) && ( $MIN_ITER -eq 0 ) ]]; then
  MIN_TIME=1.0
  MIN_ITER=2
fi
VARIABLES="$VARIABLES -e minimum_test_runtime $MIN_TIME -e minimum_test_iterations $MIN_ITER"
TESTS=( `printf "%s\n" "${TESTS[@]}" | sort -n | uniq` )
 
# print prep
echo
[ $BATCH_MODE -eq 0 ] && echo "Running tests:" || echo "Running tests (in batch mode):"
for i in ${TESTS[@]}; do
  echo $i: ${TEST_NAMES[$i]}
done
echo "with minimum runtime per test of $MIN_TIME seconds and
minimum number of $MIN_ITER iterations per test"
echo
 
# setup
echo
if [[ "`adb shell id | tr -d '\r' | awk -F'[()]' '{print $2}'`" != "root" ]]; then
  adb root
  adb wait-for-device
fi
 
# grant permission
adb shell pm grant com.android.rs.imagejb android.permission.READ_EXTERNAL_STORAGE
adb shell pm grant com.android.rs.imagejb android.permission.WRITE_EXTERNAL_STORAGE
 
# Run each test individually...
if [[ $BATCH_MODE -eq 0 ]]; then
 
  # run and plot each test result separately
  for num in `seq 0 $((${#TESTS[@]} - 1))`; do
 
    # alias
    testId=${TESTS[$num]}
 
    # report progress
    printf "Running ${TEST_NAMES[$testId]} ($(($num + 1))/${#TESTS[@]})"
 
    # run individual test
    adb shell "am instrument -w $VARIABLES -e class $TEST_ROOT#${FUNCTION_NAMES[$testId]} $RUNNER" > /dev/null
 
    # extract relevant data if present, write to temporary file
    if fileexists /sdcard/rsTimes/${TEST_NAMES[$testId]}_DATA.txt; then
      adb shell cat /sdcard/rsTimes/${TEST_NAMES[$testId]}_DATA.txt > timing.tmp
    else
      printf "\r                                                                       \r"
      echo "File ${TEST_NAMES[$testId]} not saved. Is ImageProcessing_jb able to run on this device?"
      continue
    fi
 
    # calculate avg and stdcoef
    AVG=`cat timing.tmp | awk '{sum+=$2}END{printf "%.2f",sum/NR}'`
    STDCOEF=`cat timing.tmp | awk '{sum+=$2;sos+=$2*$2}END{printf "%.2f",sqrt((sos-sum*sum/NR)/NR)/(sum/NR)*100}'`
 
    # create plot file
    echo "# temporary file" > plot.tmp
    echo "set style line 1 lc rgb '#0060ad' lt 1 lw 2 pt 7 ps 1" >> plot.tmp  # --- blue
    echo "set style line 2 lc rgb '#ff0000' lt 1 lw 2" >> plot.tmp  # --- green
    echo "set title \"${TEST_NAMES[$testId]} \(avg=$AVG ms, stdcoef=$STDCOEF%\)\"" >> plot.tmp
    echo "set xlabel \"Iteration #\"" >> plot.tmp
    echo "set ylabel \"Elapsed Time (ms)\"" >> plot.tmp
    if [ $GRAPH_ZERO -eq 1 ]; then
      echo "set yrange [0.0:*]" >> plot.tmp
    fi
    if [ $GRAPH_LINE -eq 1 ]; then
      echo "set fit quiet" >> plot.tmp
      echo "set fit logfile '/dev/null'" >> plot.tmp
      echo "f(x) = a*x + b" >> plot.tmp
      echo "fit f(x) 'timing.tmp' using 1:2 via a, b" >> plot.tmp
      echo "string = sprintf('%.3fe-3*x+%.3f', a*1000, b)" >> plot.tmp
      echo "plot 'timing.tmp' with linespoints ls 1 title 'Data', f(x) ls 2 title string" >> plot.tmp
    else
      echo "plot 'timing.tmp' with linespoints ls 1 title 'Data'" >> plot.tmp
    fi
 
    # plot data as simple line graph
    gnuplot -p plot.tmp
 
    # clear line
    printf "\r                                                                       \r"
 
  done
 
# ...or all at once
else
 
  # concatenate all tests together to run in batch
  TESTS_TEXT="-e class $TEST_ROOT#${FUNCTION_NAMES[${TESTS[0]}]}"
  for num in `seq 1 $((${#TESTS[@]} - 1))`; do
    TESTS_TEXT=$TESTS_TEXT,"$TEST_ROOT#${FUNCTION_NAMES[${TESTS[$num]}]}"
  done
 
  # run command
  adb shell "am instrument -w $VARIABLES $TESTS_TEXT $RUNNER"
 
  # run and plot each test result separately
  for num in `seq 0 $((${#TESTS[@]} - 1))`; do
 
    # alias
    testId=${TESTS[$num]}
 
    # extract relevant data if present, write to temporary file
    if fileexists /sdcard/rsTimes/${TEST_NAMES[$testId]}_DATA.txt; then
      adb shell cat /sdcard/rsTimes/${TEST_NAMES[$testId]}_DATA.txt > timing.tmp
    else
      echo "File ${TEST_NAMES[$testId]} not saved. Is ImageProcessing_jb able to run on this device?"
      continue
    fi
 
    # calculate avg and stdcoef
    AVG=`cat timing.tmp | awk '{sum+=$2}END{printf "%.2f",sum/NR}'`
    STDCOEF=`cat timing.tmp | awk '{sum+=$2;sos+=$2*$2}END{printf "%.2f",sqrt((sos-sum*sum/NR)/NR)/(sum/NR)*100}'`
 
    # create plot file
    echo "# temporary file" > plot.tmp
    echo "set style line 1 lc rgb '#0060ad' lt 1 lw 2 pt 7 ps 1" >> plot.tmp  # --- blue
    echo "set style line 2 lc rgb '#ff0000' lt 1 lw 2" >> plot.tmp  # --- green
    echo "set title \"${TEST_NAMES[$testId]} \(avg=$AVG ms, stdcoef=$STDCOEF%\)\"" >> plot.tmp
    echo "set xlabel \"Iteration #\"" >> plot.tmp
    echo "set ylabel \"Elapsed Time (ms)\"" >> plot.tmp
    if [ $GRAPH_ZERO -eq 1 ]; then
      echo "set yrange [0.0:*]" >> plot.tmp
    fi
    if [ $GRAPH_LINE -eq 1 ]; then
      echo "set fit quiet" >> plot.tmp
      echo "set fit logfile '/dev/null'" >> plot.tmp
      echo "f(x) = a*x + b" >> plot.tmp
      echo "fit f(x) 'timing.tmp' using 1:2 via a, b" >> plot.tmp
      echo "string = sprintf('%.3fe-3*x+%.3f', a*1000, b)" >> plot.tmp
      echo "plot 'timing.tmp' with linespoints ls 1 title 'Data', f(x) ls 2 title string" >> plot.tmp
    else
      echo "plot 'timing.tmp' with linespoints ls 1 title 'Data'" >> plot.tmp
    fi
 
    # plot data as simple line graph
    gnuplot -p plot.tmp
 
  done
 
fi # single or batch mode
 
# cleanup
rm -f plot.tmp
rm -f timing.tmp