Add ability to parse more kinds of test results
[csit.git] / resources / libraries / bash / function / per_patch.sh
1 # Copyright (c) 2023 Cisco and/or its affiliates.
2 # Copyright (c) 2023 PANTHEON.tech s.r.o.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 set -exuo pipefail
16
17 # This library defines functions used mainly by per patch entry scripts.
18 # Generally, the functions assume "common.sh" library has been sourced already.
19 # Keep functions ordered alphabetically, please.
20
21 function archive_test_results () {
22
23     # Arguments:
24     # - ${1}: Directory to archive to. Required. Parent has to exist.
25     # Variable set:
26     # - TARGET - Target directory.
27     # Variables read:
28     # - ARCHIVE_DIR - Path to where robot result files are created in.
29     # - VPP_DIR - Path to existing directory, root for to relative paths.
30     # Directories updated:
31     # - ${1} - Created, and robot and parsing files are moved/created there.
32     # Functions called:
33     # - die - Print to stderr and exit, defined in common.sh
34
35     set -exuo pipefail
36
37     cd "${VPP_DIR}" || die "Change directory command failed."
38     TARGET="$(readlink -f "$1")"
39     mkdir -p "${TARGET}" || die "Directory creation failed."
40     file_list=("output.xml" "log.html" "report.html" "tests")
41     for filename in "${file_list[@]}"; do
42         mv "${ARCHIVE_DIR}/${filename}" "${TARGET}/${filename}" || {
43             die "Attempt to move '${filename}' failed."
44         }
45     done
46 }
47
48
49 function archive_parse_test_results () {
50
51     # Arguments:
52     # - ${1}: Directory to archive to. Required. Parent has to exist.
53     # Variables read:
54     # - TARGET - Target directory.
55     # Functions called:
56     # - die - Print to stderr and exit, defined in common.sh
57     # - archive_test_results - Archiving results.
58     # - parse_results - See definition in this file.
59
60     set -exuo pipefail
61
62     archive_test_results "$1" || die
63     parse_results "${TARGET}" || {
64         die "The function should have died on error."
65     }
66 }
67
68
69 function build_vpp_ubuntu_amd64 () {
70
71     # This function is using make pkg-verify to build VPP with all dependencies
72     # that is ARCH/OS aware. VPP repo is SSOT for building mechanics and CSIT
73     # is consuming artifacts. This way if VPP will introduce change in building
74     # mechanics they will not be blocked by CSIT repo.
75     # Arguments:
76     # - ${1} - String identifier for echo, can be unset.
77     # Variables read:
78     # - MAKE_PARALLEL_FLAGS - Make flags when building VPP.
79     # - MAKE_PARALLEL_JOBS - Number of cores to use when building VPP.
80     # - VPP_DIR - Path to existing directory, parent to accessed directories.
81     # Directories updated:
82     # - ${VPP_DIR} - Whole subtree, many files (re)created by the build process.
83     # Functions called:
84     # - die - Print to stderr and exit, defined in common.sh
85
86     set -exuo pipefail
87
88     cd "${VPP_DIR}" || die "Change directory command failed."
89     if [ -n "${MAKE_PARALLEL_FLAGS-}" ]; then
90         echo "Building VPP. Number of cores for build set with" \
91              "MAKE_PARALLEL_FLAGS='${MAKE_PARALLEL_FLAGS}'."
92     elif [ -n "${MAKE_PARALLEL_JOBS-}" ]; then
93         echo "Building VPP. Number of cores for build set with" \
94              "MAKE_PARALLEL_JOBS='${MAKE_PARALLEL_JOBS}'."
95     else
96         echo "Building VPP. Number of cores not set, " \
97              "using build default ($(grep -c ^processor /proc/cpuinfo))."
98     fi
99
100     make UNATTENDED=y pkg-verify || die "VPP build with make pkg-verify failed."
101     echo "* VPP ${1-} BUILD SUCCESSFULLY COMPLETED" || {
102         die "Argument not found."
103     }
104 }
105
106
107 function compare_test_results () {
108
109     # Variables read:
110     # - VPP_DIR - Path to directory with VPP git repo (at least built parts).
111     # - ARCHIVE_DIR - Path to where robot result files are created in.
112     # - PYTHON_SCRIPTS_DIR - Path to directory holding comparison utility.
113     # Directories recreated:
114     # - csit_parent - Sibling to csit directory, for holding results
115     #   of parent build.
116     # Functions called:
117     # - die - Print to stderr and exit, defined in common.sh
118     # Exit code:
119     # - 0 - If the comparison utility sees no regression (nor data error).
120     # - 1 - If the comparison utility sees a regression (or data error).
121
122     set -exuo pipefail
123
124     cd "${VPP_DIR}" || die "Change directory operation failed."
125     # Reusing CSIT main virtualenv.
126     python3 "${TOOLS_DIR}/integrated/compare_perpatch.py"
127     # The exit code determines the vote result.
128 }
129
130
131 function initialize_csit_dirs () {
132
133     set -exuo pipefail
134
135     # Variables read:
136     # - VPP_DIR - Path to WORKSPACE, parent of created directories.
137     # Directories created:
138     # - csit_current - Holding test results of the patch under test (PUT).
139     # - csit_parent - Holding test results of parent of PUT.
140     # Functions called:
141     # - die - Print to stderr and exit, defined in common.sh
142
143     set -exuo pipefail
144
145     cd "${VPP_DIR}" || die "Change directory operation failed."
146     rm -rf "csit_current" "csit_parent" || {
147         die "Directory deletion failed."
148     }
149     mkdir -p "csit_current" "csit_parent" || {
150         die "Directory creation failed."
151     }
152 }
153
154
155 function parse_results () {
156
157     # Currently "parsing" is just few greps on output.xml.
158     # TODO: Parse json outputs properly.
159     #
160     # The current implementation attempts to parse for BMRR, PDR and passrate.
161     # If failures are present, they are reported as fake throughput values,
162     # enabling bisection to focus on the cause (or the fix) of the failures.
163     #
164     # The fake values are created with MRR multiplicity,
165     # otherwise jumpavg (which dislikes short groups) could misclassify them.
166     #
167     # Arguments:
168     # - ${1} - Path to (existing) directory holding robot output.xml result.
169     # Files read:
170     # - output.xml - From argument location.
171     # Files updated:
172     # - results.txt - (Re)created, in argument location.
173     # Variables read:
174     # - CSIT_PERF_TRIAL_MULTIPLICITY - To create fake results of this length.
175     # Functions called:
176     # - die - Print to stderr and exit, defined in common.sh
177     # - parse_results_mrr - See definition in this file.
178     # - parse_results_ndrpdr - See definition in this file.
179     # - parse_results_passrate - See definition in this file.
180     # - parse_results_soak - See definition in this file.
181
182     set -exuo pipefail
183
184     rel_dir="$(readlink -e "${1}")" || die "Readlink failed."
185     in_file="${rel_dir}/output.xml" || die
186     out_file="${rel_dir}/results.txt" || die
187     echo "Parsing ${in_file} putting results into ${out_file}" || die
188     # Frst attempt: (B)MRR.
189     if parse_results_mrr "${in_file}" "${out_file}"; then
190         return 0
191     fi
192     # BMRR parsing failed. Attempt PDR/NDR.
193     if parse_results_ndrpdr "${in_file}" "${out_file}"; then
194         return 0
195     fi
196     # PDR/NDR parsing failed. Attempt soak.
197     if parse_results_soak "${in_file}" "${out_file}"; then
198         return 0
199     fi
200     # Soak parsing failed.
201     # Probably not a perf test at all (or a failed one),
202     # but we can still bisect by passrate.
203     parse_results_passrate "${in_file}" "${out_file}" || die
204 }
205
206
207 function parse_results_mrr () {
208
209     # Parse MRR test message(s) into JSON-readable output.
210     #
211     # Return non-zero if parsing fails.
212     #
213     # Arguments:
214     # - ${1} - Path to (existing) input file. Required.
215     # - ${2} - Path to (overwritten if exists) output file. Required.
216     # Files read:
217     # - output.xml - The input file from argument location.
218     # Files updated:
219     # - results.txt - (Re)created, in argument location.
220     # Functions called:
221     # - die - Print to stderr and exit, defined in common.sh
222
223     set -exuo pipefail
224
225     in_file="${1}" || die "Two arguments needed."
226     out_file="${2}" || die "Two arguments needed."
227     pattern='Maximum Receive Rate trial results in .*' || die
228     pattern+=' per second: .*\]</status>' || die
229     # RC of the following line is returned.
230     grep -o "${pattern}" "${in_file}" | grep -o '\[.*\]' > "${out_file}"
231 }
232
233
234 function parse_results_ndrpdr () {
235
236     # Parse NDRPDR test message(s) for PDR_LOWER, into JSON-readable output.
237     #
238     # Return non-zero if parsing fails.
239     # Parse for PDR, unless environment variable says NDR.
240     #
241     # Arguments:
242     # - ${1} - Path to (existing) input file. Required.
243     # - ${2} - Path to (overwritten if exists) output file. Required.
244     # Variables read:
245     # - FDIO_CSIT_PERF_PARSE_NDR - If defined and "yes", parse for NDR, not PDR.
246     # Files read:
247     # - output.xml - The input file from argument location.
248     # Files updated:
249     # - results.txt - (Re)created, in argument location.
250     # Functions called:
251     # - die - Print to stderr and exit, defined in common.sh
252
253     set -exuo pipefail
254
255     in_file="${1}" || die "Two arguments needed."
256     out_file="${2}" || die "Two arguments needed."
257     if [[ "${FDIO_CSIT_PERF_PARSE_NDR:-no}" == "yes" ]]; then
258         pattern1="Arguments: [ '\\nNDR_LOWER: " || die
259     else
260         pattern1="Arguments: [ '\\nPDR_LOWER: " || die
261     fi
262     # Adapted from https://superuser.com/a/377084
263     pattern2='(?<=R: ).*(?= pps)' || die
264     if fgrep "${pattern1}" "${in_file}" | grep -Po "${pattern2}" >> "${out_file}"
265     then
266         # Add bracket https://www.shellhacks.com/sed-awk-add-end-beginning-line/
267         sed -i 's/.*/[&]/' "${out_file}"
268         # Returns nonzero if fails.
269         return "$?"
270     fi
271     # Maybe it was CPS instead of pps?
272     pattern2='(?<=R: ).*(?= CPS)' || die
273     if fgrep "${pattern1}" "${in_file}" | grep -Po "${pattern2}" >> "${out_file}"
274     then
275         # Add bracket https://www.shellhacks.com/sed-awk-add-end-beginning-line/
276         sed -i 's/.*/[&]/' "${out_file}"
277         # Returns nonzero if fails.
278         return "$?"
279     else
280         return 1
281     fi
282 }
283
284
285 function parse_results_passrate () {
286
287     # Create fake values for failed tests.
288     #
289     # This function always passes (or dies).
290     #
291     # A non-zero but small value is chosen for failed run, to distinguish from
292     # real nonzero perf (which are big in general) and real zero values.
293     # A medium sized value is chosen for a passed run.
294     # This way bisect can search for breakages and fixes in device tests.
295     # At least in theory, as device tests are bootstrapped too differently.
296     #
297     # The fake value is repeated according to BMRR multiplicity,
298     # because a single value can be lost in high stdev data.
299     # (And it does not hurt for single value outputs such as NDR.)
300     #
301     # TODO: Count number of tests and generate fake results for every one.
302     #       Currently that would interfere with test retry logic.
303     #
304     # Arguments:
305     # - ${1} - Path to (existing) input file. Required.
306     # - ${2} - Path to (overwritten if exists) output file. Required.
307     # Variables read:
308     # - CSIT_PERF_TRIAL_MULTIPLICITY - To create fake results of this length.
309     # Files read:
310     # - output.xml - The input file from argument location.
311     # Files updated:
312     # - results.txt - (Re)created, in argument location.
313     # Functions called:
314     # - die - Print to stderr and exit, defined in common.sh
315
316     set -exuo pipefail
317
318     in_file="${1}" || die "Two arguments needed."
319     out_file="${2}" || die "Two arguments needed."
320     # The last status is the top level (global) robot status.
321     # It only passes if there were no (critical) test failures.
322     if fgrep '<status status=' "${out_file}" | tail -n 1 | fgrep '"PASS"'; then
323         fake_value="30.0" || die
324     else
325         fake_value="2.0" || die
326     fi
327     out_arr=("[") || die
328     for i in `seq "${CSIT_PERF_TRIAL_MULTIPLICITY:-1}"`; do
329         out_arr+=("${fake_value}" ",") || die
330     done
331     # The Python part uses JSON parser, the last comma has to be removed.
332     # Requires Bash 4.3 https://stackoverflow.com/a/36978740
333     out_arr[-1]="]" || die
334     # TODO: Is it possible to avoid space separation by manipulating IFS?
335     echo "${out_arr[@]}" > "${out_file}" || die
336 }
337
338
339 function parse_results_soak () {
340
341     # Parse soak test message(s) for lower bound, into JSON-readable output.
342     #
343     # Return non-zero if parsing fails.
344     #
345     # Arguments:
346     # - ${1} - Path to (existing) input file. Required.
347     # - ${2} - Path to (overwritten if exists) output file. Required.
348     # Files read:
349     # - output.xml - The input file from argument location.
350     # Files updated:
351     # - results.txt - (Re)created, in argument location.
352     # Functions called:
353     # - die - Print to stderr and exit, defined in common.sh
354
355     set -exuo pipefail
356
357     in_file="${1}" || die "Two arguments needed."
358     out_file="${2}" || die "Two arguments needed."
359     pattern1='PLRsearch lower bound: .*, .*<' || die
360     # Adapted from https://superuser.com/a/377084
361     pattern2='(?<=: ).*(?= pps)' || die
362     if grep "${pattern1}" "${in_file}" | grep -Po "${pattern2}" >> "${out_file}"
363     then
364         # Add bracket https://www.shellhacks.com/sed-awk-add-end-beginning-line/
365         sed -i 's/.*/[&]/' "${out_file}"
366         # Returns nonzero if fails.
367     else
368         return 1
369     fi
370 }
371
372
373 function select_build () {
374
375     # Arguments:
376     # - ${1} - Path to directory to copy VPP artifacts from. Required.
377     # Variables read:
378     # - DOWNLOAD_DIR - Path to directory where Robot takes builds to test from.
379     # - VPP_DIR - Path to existing directory, root for relative paths.
380     # Directories read:
381     # - ${1} - Existing directory with built new VPP artifacts (and DPDK).
382     # Directories updated:
383     # - ${DOWNLOAD_DIR} - Old content removed, .deb files from ${1} copied here.
384     # Functions called:
385     # - die - Print to stderr and exit, defined in common.sh
386
387     set -exuo pipefail
388
389     cd "${VPP_DIR}" || die "Change directory operation failed."
390     source_dir="$(readlink -e "$1")"
391     rm -rf "${DOWNLOAD_DIR}"/* || die "Cleanup of download dir failed."
392     cp "${source_dir}"/*".deb" "${DOWNLOAD_DIR}" || die "Copy operation failed."
393     # TODO: Is there a nice way to create symlinks,
394     #   so that if job fails on robot, results can be archived?
395 }
396
397
398 function set_aside_commit_build_artifacts () {
399
400     # Function is copying VPP built artifacts from actual checkout commit for
401     # further use and clean git.
402     # Variables read:
403     # - VPP_DIR - Path to existing directory, parent to accessed directories.
404     # Directories read:
405     # - build-root - Existing directory with built VPP artifacts (also DPDK).
406     # Directories updated:
407     # - ${VPP_DIR} - A local git repository, parent commit gets checked out.
408     # - build_current - Old contents removed, content of build-root copied here.
409     # Functions called:
410     # - die - Print to stderr and exit, defined in common.sh
411
412     set -exuo pipefail
413
414     cd "${VPP_DIR}" || die "Change directory operation failed."
415     rm -rf "build_current" || die "Remove operation failed."
416     mkdir -p "build_current" || die "Directory creation failed."
417     mv "build-root"/*".deb" "build_current"/ || die "Move operation failed."
418     # The previous build could have left some incompatible leftovers,
419     # e.g. DPDK artifacts of different version (in build/external).
420     # Also, there usually is a copy of dpdk artifact in build-root.
421     git clean -dffx "build"/ "build-root"/ || die "Git clean operation failed."
422     # Finally, check out the parent commit.
423     git checkout HEAD~ || die "Git checkout operation failed."
424     # Display any other leftovers.
425     git status || die "Git status operation failed."
426 }
427
428
429 function set_aside_parent_build_artifacts () {
430
431     # Function is copying VPP built artifacts from parent checkout commit for
432     # further use. Checkout to parent is not part of this function.
433     # Variables read:
434     # - VPP_DIR - Path to existing directory, parent of accessed directories.
435     # Directories read:
436     # - build-root - Existing directory with built VPP artifacts (also DPDK).
437     # Directories updated:
438     # - build_parent - Old directory removed, build-root debs moved here.
439     # Functions called:
440     # - die - Print to stderr and exit, defined in common.sh
441
442     set -exuo pipefail
443
444     cd "${VPP_DIR}" || die "Change directory operation failed."
445     rm -rf "build_parent" || die "Remove failed."
446     mkdir -p "build_parent" || die "Directory creation operation failed."
447     mv "build-root"/*".deb" "build_parent"/ || die "Move operation failed."
448 }
449
450
451 function set_perpatch_dut () {
452
453     # Variables set:
454     # - DUT - CSIT test/ subdirectory containing suites to execute.
455
456     # TODO: Detect DUT from job name, when we have more than just VPP perpatch.
457
458     set -exuo pipefail
459
460     DUT="vpp"
461 }
462
463
464 function set_perpatch_vpp_dir () {
465
466     # Variables read:
467     # - CSIT_DIR - Path to existing root of local CSIT git repository.
468     # Variables set:
469     # - VPP_DIR - Path to existing root of local VPP git repository.
470     # Functions called:
471     # - die - Print to stderr and exit, defined in common.sh
472
473     set -exuo pipefail
474
475     # In perpatch, CSIT is cloned inside VPP clone.
476     VPP_DIR="$(readlink -e "${CSIT_DIR}/..")" || die "Readlink failed."
477 }