| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# Copyright The containerd Authors. |
| 4 |
|
| 5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
# you may not use this file except in compliance with the License. |
| 7 |
# You may obtain a copy of the License at |
| 8 |
|
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
|
| 11 |
# Unless required by applicable law or agreed to in writing, software |
| 12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
# See the License for the specific language governing permissions and |
| 15 |
# limitations under the License. |
| 16 |
|
| 17 |
set -euo pipefail |
| 18 |
|
| 19 |
PERCENTILE="${BENCHMARK_PERCENTILE:-}" |
| 20 |
if [ "${PERCENTILE}" == "" ] ; then |
| 21 |
PERCENTILE="95" # 95 percentile by default |
| 22 |
fi |
| 23 |
|
| 24 |
# samples_num functions returns the number of samples which are identified by |
| 25 |
# image name, mode and operation(pull, create, run) in the given JSON raw data. |
| 26 |
function samples_num { |
| 27 |
local JSONFILE="${1}" |
| 28 |
local IMAGE="${2}" |
| 29 |
local MODE="${3}" |
| 30 |
local TARGET="${4}" |
| 31 |
|
| 32 |
cat "${JSONFILE}" \ |
| 33 |
| jq -r '.[] | select(.repo=="'"${IMAGE}"'" and .mode=="'"${MODE}"'")."'"${TARGET}"'"' \ |
| 34 |
| wc -l |
| 35 |
} |
| 36 |
|
| 37 |
# min_samples function returns the minimal number of samples which are identified by |
| 38 |
# image name, mode and operation(pull, create, run) in the given JSON raw data. |
| 39 |
function min_samples { |
| 40 |
local JSONFILE="${1}" |
| 41 |
local IMAGE="${2}" |
| 42 |
local MODE="${3}" |
| 43 |
|
| 44 |
PULLSAMPLES=$(samples_num "${JSONFILE}" "${IMGNAME}" "${MODE}" "elapsed_pull") |
| 45 |
CREATESAMPLES=$(samples_num "${JSONFILE}" "${IMGNAME}" "${MODE}" "elapsed_create") |
| 46 |
RUNSAMPLES=$(samples_num "${JSONFILE}" "${IMGNAME}" "${MODE}" "elapsed_run") |
| 47 |
echo $(echo "${PULLSAMPLES} ${CREATESAMPLES} ${RUNSAMPLES}" | tr ' ' '\n' | sort -n | head -1) |
| 48 |
} |
| 49 |
|
| 50 |
# percentile function returns the specified percentile value relying on numpy. |
| 51 |
# See also: https://numpy.org/doc/stable/reference/generated/numpy.percentile.html |
| 52 |
CALCTEMP=$(mktemp) |
| 53 |
function percentile { |
| 54 |
local JSONFILE="${1}" |
| 55 |
local SAMPLES="${2}" |
| 56 |
local IMAGE="${3}" |
| 57 |
local MODE="${4}" |
| 58 |
local TARGET="${5}" |
| 59 |
|
| 60 |
cat "${JSONFILE}" \ |
| 61 |
| jq -r '.[] | select(.repo=="'"${IMAGE}"'" and .mode=="'"${MODE}"'")."'"${TARGET}"'"' \ |
| 62 |
| sort -R | head -n "${SAMPLES}" | sort -n > "${CALCTEMP}" |
| 63 |
local PYTHON_BIN= |
| 64 |
if which python &> /dev/null ; then |
| 65 |
PYTHON_BIN=python |
| 66 |
elif which python3 &> /dev/null ; then |
| 67 |
# Try also with python3 |
| 68 |
PYTHON_BIN=python3 |
| 69 |
else |
| 70 |
echo "Python not found" |
| 71 |
exit 1 |
| 72 |
fi |
| 73 |
cat <<EOF | "${PYTHON_BIN}" |
| 74 |
import numpy as np |
| 75 |
f = open('${CALCTEMP}', 'r') |
| 76 |
arr = [] |
| 77 |
for line in f.readlines(): |
| 78 |
arr.append(float(line)) |
| 79 |
f.close() |
| 80 |
print(np.percentile(a=np.array(arr), q=${PERCENTILE}, interpolation='linear')) |
| 81 |
EOF |
| 82 |
} |