1#!/usr/bin/env bash
2# Copyright 2022 The gRPC Authors
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15set -ex
16
17# Enter the gRPC repo root.
18cd "$(dirname "$0")/../../.."
19
20source tools/internal_ci/helper_scripts/prepare_build_linux_rc
21
22# This is to ensure we can push and pull images from gcr.io. We do not
23# necessarily need it to run load tests, but will need it when we employ
24# pre-built images in the optimization.
25gcloud auth configure-docker
26
27# Connect to benchmarks-prod2 cluster.
28gcloud config set project grpc-testing
29gcloud container clusters get-credentials psm-benchmarks-performance \
30    --zone us-central1-b --project grpc-testing
31
32# Set up environment variables.
33LOAD_TEST_PREFIX="${KOKORO_BUILD_INITIATOR}"
34# BEGIN differentiate experimental configuration from master configuration.
35if [[ "${KOKORO_BUILD_INITIATOR%%-*}" == kokoro ]]; then
36    LOAD_TEST_PREFIX=kokoro-test
37fi
38BIGQUERY_TABLE_8CORE=e2e_benchmarks.psm_experimental_results_8core
39# END differentiate experimental configuration from master configuration.
40CLOUD_LOGGING_URL="https://source.cloud.google.com/results/invocations/${KOKORO_BUILD_ID}"
41PREBUILT_IMAGE_PREFIX="gcr.io/grpc-testing/e2etest/prebuilt/${LOAD_TEST_PREFIX}"
42UNIQUE_IDENTIFIER="$(date +%Y%m%d%H%M%S)"
43ROOT_DIRECTORY_OF_DOCKERFILES="../test-infra/containers/pre_built_workers/"
44# Head of the workspace checked out by Kokoro.
45GRPC_GITREF="$(git show --format="%H" --no-patch)"
46# Prebuilt workers for core languages are always built from grpc/grpc.
47if [[ "${KOKORO_GITHUB_COMMIT_URL%/*}" == "https://github.com/grpc/grpc/commit" ]]; then
48    GRPC_CORE_GITREF="${KOKORO_GIT_COMMIT}"
49else
50    GRPC_CORE_GITREF="$(git ls-remote -h https://github.com/grpc/grpc.git master | cut -f1)"
51fi
52GRPC_JAVA_GITREF="$(git ls-remote -h https://github.com/grpc/grpc-java.git master | cut -f1)"
53# Kokoro jobs run on dedicated pools.
54DRIVER_POOL=drivers-ci
55WORKER_POOL_8CORE=workers-c2-8core-ci
56# Prefix for log URLs in cnsviewer.
57LOG_URL_PREFIX="http://cnsviewer/placer/prod/home/kokoro-dedicated/build_artifacts/${KOKORO_BUILD_ARTIFACTS_SUBDIR}/github/grpc/"
58
59# Clone test-infra repository and build all tools.
60pushd ..
61git clone https://github.com/grpc/test-infra.git
62cd test-infra
63# Tools are built from HEAD.
64git checkout --detach
65make all-tools
66
67# Find latest release tag of test-infra.
68git fetch --all --tags
69TEST_INFRA_VERSION=$(git describe --tags "$(git rev-list --tags --max-count=1)")
70popd
71
72# PSM tests related ENV
73PSM_IMAGE_PREFIX=gcr.io/grpc-testing/e2etest/runtime
74PSM_IMAGE_TAG=${TEST_INFRA_VERSION}
75
76# Build psm test configurations.
77psmBuildConfigs() {
78    local -r pool="$1"
79    local -r table="$2"
80    local -r proxy_type="$3"
81
82    shift 3
83    tools/run_tests/performance/loadtest_config.py "$@" \
84        -t ./tools/run_tests/performance/templates/loadtest_template_psm_"${proxy_type}"_prebuilt_all_languages.yaml \
85        -s driver_pool="${DRIVER_POOL}" -s driver_image= \
86        -s client_pool="${pool}" -s server_pool="${pool}" \
87        -s big_query_table="${table}" -s timeout_seconds=900 \
88        -s prebuilt_image_prefix="${PREBUILT_IMAGE_PREFIX}" \
89        -s prebuilt_image_tag="${UNIQUE_IDENTIFIER}" \
90        -s psm_image_prefix="${PSM_IMAGE_PREFIX}" \
91        -s psm_image_tag="${PSM_IMAGE_TAG}" \
92        -a ci_buildNumber="${KOKORO_BUILD_NUMBER}" \
93        -a ci_buildUrl="${CLOUD_LOGGING_URL}" \
94        -a ci_jobName="${KOKORO_JOB_NAME}" \
95        -a ci_gitCommit="${GRPC_GITREF}" \
96        -a ci_gitCommit_java="${GRPC_JAVA_GITREF}" \
97        -a ci_gitActualCommit="${KOKORO_GIT_COMMIT}" \
98        -a enablePrometheus=true \
99        --prefix="${LOAD_TEST_PREFIX}" -u "${UNIQUE_IDENTIFIER}" -u "${pool}" -u "${proxy_type}"\
100        -a pool="${pool}" \
101        --category=psm \
102        --allow_client_language=c++ --allow_server_language=c++ \
103        -o "psm_${proxy_type}_loadtest_with_prebuilt_workers_${pool}.yaml"
104}
105
106psmBuildConfigs "${WORKER_POOL_8CORE}" "${BIGQUERY_TABLE_8CORE}" proxied -a queue="${WORKER_POOL_8CORE}-proxied"  -l c++ -l java --client_channels=8 --server_threads=16 --offered_loads 100 300 500 700 900 1000 1500 2000 2500 4000 6000 8000 10000 12000 14000 16000 18000 20000 22000 24000 26000 28000 30000
107
108psmBuildConfigs "${WORKER_POOL_8CORE}" "${BIGQUERY_TABLE_8CORE}" proxyless -a queue="${WORKER_POOL_8CORE}-proxyless" -l c++ -l java --client_channels=8 --server_threads=16 --offered_loads 100 300 500 700 900 1000 1500 2000 2500 4000 6000 8000 10000 12000 14000 16000 18000 20000 22000 24000 26000 28000 30000
109
110# Build regular test configurations.
111buildConfigs() {
112    local -r pool="$1"
113    local -r table="$2"
114    shift 2
115    tools/run_tests/performance/loadtest_config.py "$@" \
116        -t ./tools/run_tests/performance/templates/loadtest_template_prebuilt_all_languages.yaml \
117        -s driver_pool="${DRIVER_POOL}" -s driver_image= \
118        -s client_pool="${pool}" -s server_pool="${pool}" \
119        -s big_query_table="${table}" -s timeout_seconds=900 \
120        -s prebuilt_image_prefix="${PREBUILT_IMAGE_PREFIX}" \
121        -s prebuilt_image_tag="${UNIQUE_IDENTIFIER}" \
122        -a ci_buildNumber="${KOKORO_BUILD_NUMBER}" \
123        -a ci_buildUrl="${CLOUD_LOGGING_URL}" \
124        -a ci_jobName="${KOKORO_JOB_NAME}" \
125        -a ci_gitCommit="${GRPC_GITREF}" \
126        -a ci_gitCommit_java="${GRPC_JAVA_GITREF}" \
127        -a ci_gitActualCommit="${KOKORO_GIT_COMMIT}" \
128        -a enablePrometheus=true \
129        --prefix="${LOAD_TEST_PREFIX}" -u "${UNIQUE_IDENTIFIER}" -u "${pool}" \
130        -a pool="${pool}" --category=psm \
131        --allow_client_language=c++ --allow_server_language=c++ \
132        -o "regular_loadtest_with_prebuilt_workers_${pool}.yaml"
133}
134
135buildConfigs "${WORKER_POOL_8CORE}" "${BIGQUERY_TABLE_8CORE}" -a queue="${WORKER_POOL_8CORE}-regular" -l c++ -l java --client_channels=8 --server_threads=16 --offered_loads 100 300 500 700 900 1000 1500 2000 2500 4000 6000 8000 10000 12000 14000 16000 18000 20000 22000 24000 26000 28000 30000
136
137# Delete prebuilt images on exit.
138deleteImages() {
139    echo "deleting images on exit"
140    ../test-infra/bin/delete_prebuilt_workers \
141    -p "${PREBUILT_IMAGE_PREFIX}" \
142    -t "${UNIQUE_IDENTIFIER}"
143}
144trap deleteImages EXIT
145
146# Build and push prebuilt images for running tests.
147time ../test-infra/bin/prepare_prebuilt_workers \
148    -l "cxx:${GRPC_CORE_GITREF}" \
149    -l "java:${GRPC_JAVA_GITREF}" \
150    -p "${PREBUILT_IMAGE_PREFIX}" \
151    -t "${UNIQUE_IDENTIFIER}" \
152    -r "${ROOT_DIRECTORY_OF_DOCKERFILES}"
153
154# Run tests.
155time ../test-infra/bin/runner \
156    -i "psm_proxied_loadtest_with_prebuilt_workers_${WORKER_POOL_8CORE}.yaml" \
157    -i "psm_proxyless_loadtest_with_prebuilt_workers_${WORKER_POOL_8CORE}.yaml" \
158    -i "regular_loadtest_with_prebuilt_workers_${WORKER_POOL_8CORE}.yaml" \
159    -log-url-prefix "${LOG_URL_PREFIX}" \
160    -annotation-key queue \
161    -polling-interval 5s \
162    -delete-successful-tests \
163    -c "${WORKER_POOL_8CORE}-proxied:1" \
164    -c "${WORKER_POOL_8CORE}-proxyless:1" \
165    -c "${WORKER_POOL_8CORE}-regular:1" \
166    -o "runner/sponge_log.xml"
167