github.com/kata-containers/tests@v0.0.0-20240307153542-772105b56064/cmd/checkmetrics/history/history.sh (about)

     1  #!/usr/bin/env bash
     2  #
     3  # Copyright (c) 2019 Intel Corporation
     4  #
     5  # SPDX-License-Identifier: Apache-2.0
     6  
     7  # Extract Jenkins metrics server historic data.
     8  # Useful when re-setting the checkmetrics baseline data.
     9  
    10  set -e
    11  
    12  export KATA_HYPERVISOR="${KATA_HYPERVISOR:-qemu}"
    13  
    14  # Base dir of where we store the downloaded data
    15  datadir=$(dirname "$0")/data
    16  
    17  # How many recent builds do we evaluate
    18  NUM_BUILDS=5
    19  
    20  # What is the default set of repos (Jenkins jobs) we evaluate
    21  default_repos=()
    22  default_repos+=("kata-containers-2.0-metrics-ubuntu-20-04-PR")
    23  default_repos+=("kata-containers-2.0-tests-metrics-ubuntu-20-04-PR")
    24  repos=()
    25  
    26  # What test results do we evaluate for each build
    27  tests=()
    28  test_queries=()
    29  tests+=("boot-times")
    30  test_queries+=(".\"boot-times\".Results | [.[] | .\"to-workload\".Result] | add / length")
    31  
    32  tests+=("blogbench")
    33  test_queries+=(".\"blogbench\".Results | .[] | .write.Result")
    34  
    35  tests+=("blogbench")
    36  test_queries+=(".\"blogbench\".Results | .[] | .read.Result")
    37  
    38  tests+=("memory-footprint")
    39  test_queries+=(".\"memory-footprint\".Results | .[] | .average.Result")
    40  
    41  tests+=("memory-footprint-ksm")
    42  test_queries+=(".\"memory-footprint-ksm\".Results | .[] | .average.Result")
    43  
    44  tests+=("memory-footprint-inside-container")
    45  test_queries+=(".\"memory-footprint-inside-container\".Results | .[] | .memtotal.Result")
    46  
    47  if [ "${KATA_HYPERVISOR}" == "cloud-hypervisor" ]; then
    48  	tests+=("network-iperf3")
    49  	test_queries+=(".\"network-iperf3\".Results | .[] | .cpu.Result")
    50  
    51  	tests+=("latency")
    52  	test_queries+=(".\"latency\".Results | .[] | .latency.Result")
    53  
    54  	tests+=("network-iperf3")
    55  	test_queries+=(".\"network-iperf3\".Results | .[] | .parallel.Result")
    56  
    57  	tests+=("network-iperf3")
    58  	test_queries+=(".\"network-iperf3\".Results | .[] | .jitter.Result")
    59  
    60  	tests+=("network-iperf3")
    61  	test_queries+=(".\"network-iperf3\".Results | .[] | .bandwidth.Result")
    62  fi
    63  
    64  # What is the base URL of the Jenkins server
    65  url_base="http://jenkins.katacontainers.io/job"
    66  
    67  # Where do we find the recent build number information
    68  url_index="api/json"
    69  
    70  # Where do we get the actual build results from
    71  url_artifacts="artifact/go/src/github.com/kata-containers/tests/metrics/results/artifacts"
    72  
    73  # Gather up the results (json) files from all the defined repos for the range
    74  # of dates?
    75  gather_data() {
    76  	for repo in "${repos[@]}"; do
    77  		echo "Getting history for repo $repo"
    78  		local outpath="${indexdir}/${repo}"
    79  		local outname="${outpath}/index.json"
    80  		mkdir -p "${outpath}"
    81  		local url="${url_base}/${repo}/${url_index}"
    82  		# First, we need the index file for the job so we can get the list of the
    83  		# last 'n' jobs run.
    84  		curl -L -o ${outname} $url
    85  
    86  		builds=$(jq '.builds | .[] | .number' ${outname} | head -n ${NUM_BUILDS})
    87  
    88  		echo "Examining builds: $builds"
    89  
    90  		# For each build, for each test, pull down the json results file, if it
    91  		# exists
    92  		for build in $builds; do
    93  			echo "Get results for build $build"
    94  			local builddir="${resultsdir}/${repo}/${build}"
    95  			mkdir -p ${builddir}
    96  			local build_url="${url_base}/${repo}/${build}/${url_artifacts}/${testfilename}"
    97  			echo "Pulling result from $build_url"
    98  			for test in "${tests[@]}"; do
    99  				local testfile=${builddir}/${KATA_HYPERVISOR}-${test}.json
   100  				local test_url="${build_url}/${KATA_HYPERVISOR}-${test}.json"
   101  				echo "    $test_url"
   102  				# Can fail if the build failed to generate any results
   103  				curl -L -o ${testfile} $test_url || true
   104  			done
   105  		done
   106  	done
   107  }
   108  
   109  # For each test type, process all the relevant data files in the results subdir.
   110  # *NOTE*, this does *not* take into account the number or list of build numbers we
   111  # pulled down - it will evaluate all files it finds. If you want to only evaluate
   112  # the data you pulled, ensure the result directory is empty (or non-existant) before
   113  # you run the script.
   114  process_data() {
   115  	local count=0
   116  	for test in "${tests[@]}"; do
   117  		query="${test_queries[$count]}"
   118  		echo "Processing $test"
   119  		echo " Query '$query'"
   120  		count=$((count+1))
   121  
   122  		local allvalues=""
   123  		local found=0
   124  		local total=0
   125  		local min=$(printf "%u" -1)
   126  		local max=0
   127  		files=$(find ${resultsdir} -name ${KATA_HYPERVISOR}-${test}.json -print)
   128  		for file in ${files}; do
   129  			echo "  Look at file $file"
   130  			value=$(jq "$query" $file || true)
   131  			echo "   Result $value"
   132  			if [ -n "$value" ]; then
   133  				allvalues="$value $allvalues"
   134  				found=$((found+1))
   135  				total=$(echo $total+$value | bc)
   136  
   137  				(( $(echo "$value > $max" | bc) )) && max=${value}
   138  				(( $(echo "$value < $min" | bc) )) && min=${value}
   139  			fi
   140  		done
   141  
   142  		mean=$(echo "scale=2; $total/$found" | bc)
   143  		minpc=$(echo "scale=2; ($min/$mean)*100" | bc)
   144  		maxpc=$(echo "scale=2; ($max/$mean)*100" | bc)
   145  		pc_95=$(echo "scale=2; $mean*0.95" | bc)
   146  		pc_105=$(echo "scale=2; $mean*1.05" | bc)
   147  
   148  		echo "allvalues are [$allvalues]"
   149  		echo "${test}: mean $mean, 95% mean ${pc_95}, 105% mean ${pc_105}"
   150  		echo "         min $min ($minpc% of mean), max $max ($maxpc% of mean)"
   151  	done
   152  }
   153  
   154  help() {
   155  	usage=$(cat << EOF
   156  Usage: $0 [-h] [options]
   157     Description:
   158          Gather statistics from recent Jenkins CI metrics builds. The resulting
   159          data is useful for configuring the metrics slave checkmetrics baselines.
   160  
   161          To change which metrics tests are evaluated, edit the values in this
   162          script directly. Default tests evaluated are:
   163            "${tests[@]}"
   164  
   165     Options:
   166          -d <path>,   Directory to store downloaded data (default: ${datadir})
   167          -h,          Print this help
   168          -n <n>,      Fetch last 'n' build data from Jenkins server (default: ${NUM_BUILDS})
   169                       Note: The statistics calculations include *all* data files in the
   170                       directory: ${resultsdir}. If previous data exists, it will be counted.
   171          -r <remote>, Which Jenkins build jobs to gather data from.
   172             (default: "${default_repos[@]}")
   173  EOF
   174  )
   175  	echo "$usage"
   176  }
   177  
   178  main() {
   179  	local OPTIND
   180  	while getopts "d:hn:r:" opt;do
   181  		case ${opt} in
   182  		d)
   183  		    datadir="${OPTARG}"
   184  		    ;;
   185  		h)
   186  		    help
   187  		    exit 0;
   188  		    ;;
   189  		n)
   190  		    NUM_BUILDS="${OPTARG}"
   191  		    ;;
   192  		r)
   193  		    repos+=("${OPTARG}")
   194  		    ;;
   195  		?)
   196  		    # parse failure
   197  		    help
   198  		    echo "Failed to parse arguments" >&2
   199  		    exit -1
   200  		    ;;
   201  		esac
   202  	done
   203  	shift $((OPTIND-1))
   204  
   205  	[ -z "${repos[@]}" ] && repos=(${default_repos[@]})
   206  
   207  	resultsdir="${datadir}/results"
   208  	indexdir="${datadir}/indexes"
   209  
   210  	gather_data
   211  	process_data
   212  }
   213  
   214  main "$@"