1#!/usr/bin/env bash
2# Copyright 2021 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
23# Environment variables to select repos and branches for various repos.
24# You can edit these lines if you want to run from a fork.
25GRPC_CORE_REPO=grpc/grpc
26GRPC_CORE_GITREF=master
27GRPC_DOTNET_REPO=grpc/grpc-dotnet
28GRPC_DOTNET_GITREF=master
29GRPC_GO_REPO=grpc/grpc-go
30GRPC_GO_GITREF=master
31GRPC_JAVA_REPO=grpc/grpc-java
32GRPC_JAVA_GITREF=master
33TEST_INFRA_REPO=grpc/test-infra
34TEST_INFRA_GITREF=master
35
36# This is to ensure we can push and pull images from gcr.io. We do not
37# necessarily need it to run load tests, but will need it when we employ
38# pre-built images in the optimization.
39gcloud auth configure-docker
40
41# Connect to benchmarks-prod2 cluster.
42gcloud config set project grpc-testing
43gcloud container clusters get-credentials benchmarks-prod2 \
44    --zone us-central1-b --project grpc-testing
45
46# Set up environment variables.
47LOAD_TEST_PREFIX="${KOKORO_BUILD_INITIATOR}"
48# BEGIN differentiate experimental configuration from master configuration.
49if [[ "${KOKORO_BUILD_INITIATOR%%-*}" == kokoro ]]; then
50    LOAD_TEST_PREFIX=kokoro-test
51fi
52BIGQUERY_TABLE_8CORE=e2e_benchmarks.experimental_results
53BIGQUERY_TABLE_32CORE=e2e_benchmarks.experimental_results_32core
54# END differentiate experimental configuration from master configuration.
55CLOUD_LOGGING_URL="https://source.cloud.google.com/results/invocations/${KOKORO_BUILD_ID}"
56PREBUILT_IMAGE_PREFIX="gcr.io/grpc-testing/e2etest/prebuilt/${LOAD_TEST_PREFIX}"
57UNIQUE_IDENTIFIER="$(date +%Y%m%d%H%M%S)"
58ROOT_DIRECTORY_OF_DOCKERFILES="../test-infra/containers/pre_built_workers/"
59# Head of the workspace checked out by Kokoro.
60GRPC_COMMIT="$(git show --format="%H" --no-patch)"
61# Prebuilt workers for core languages are always built from grpc/grpc.
62if [[ "${KOKORO_GITHUB_COMMIT_URL%/*}" == "https://github.com/grpc/grpc/commit" ]]; then
63    GRPC_CORE_COMMIT="${KOKORO_GIT_COMMIT}"
64else
65    GRPC_CORE_COMMIT="$(git ls-remote -h "https://github.com/${GRPC_CORE_REPO}.git" "${GRPC_CORE_GITREF}" | cut -f1)"
66fi
67
68GRPC_DOTNET_COMMIT="$(git ls-remote "https://github.com/${GRPC_DOTNET_REPO}.git" "${GRPC_DOTNET_GITREF}" | cut -f1)"
69GRPC_GO_COMMIT="$(git ls-remote "https://github.com/${GRPC_GO_REPO}.git" "${GRPC_GO_GITREF}" | cut -f1)"
70GRPC_JAVA_COMMIT="$(git ls-remote "https://github.com/${GRPC_JAVA_REPO}.git" "${GRPC_JAVA_GITREF}" | cut -f1)"
71# Kokoro jobs run on dedicated pools.
72DRIVER_POOL=drivers-ci
73WORKER_POOL_8CORE=workers-c2-8core-ci
74# c2-standard-30 is the closest machine spec to 32 core there is
75WORKER_POOL_32CORE=workers-c2-30core-ci
76# Prefix for log URLs in cnsviewer.
77LOG_URL_PREFIX="http://cnsviewer/placer/prod/home/kokoro-dedicated/build_artifacts/${KOKORO_BUILD_ARTIFACTS_SUBDIR}/github/grpc/"
78
79# Clone test-infra repository and build all tools.
80mkdir ../test-infra
81pushd ../test-infra
82git clone "https://github.com/${TEST_INFRA_REPO}.git" .
83git checkout "${TEST_INFRA_GITREF}"
84make all-tools
85popd
86
87# Build test configurations.
88buildConfigs() {
89    local -r pool="$1"
90    local -r table="$2"
91    shift 2
92    tools/run_tests/performance/loadtest_config.py "$@" \
93        -t ./tools/run_tests/performance/templates/loadtest_template_prebuilt_all_languages.yaml \
94        -s driver_pool="${DRIVER_POOL}" -s driver_image= \
95        -s client_pool="${pool}" -s server_pool="${pool}" \
96        -s big_query_table="${table}" -s timeout_seconds=900 \
97        -s prebuilt_image_prefix="${PREBUILT_IMAGE_PREFIX}" \
98        -s prebuilt_image_tag="${UNIQUE_IDENTIFIER}" \
99        -a ci_buildNumber="${KOKORO_BUILD_NUMBER}" \
100        -a ci_buildUrl="${CLOUD_LOGGING_URL}" \
101        -a ci_jobName="${KOKORO_JOB_NAME}" \
102        -a ci_gitCommit="${GRPC_COMMIT}" \
103        -a ci_gitCommit_core="${GRPC_CORE_COMMIT}" \
104        -a ci_gitCommit_dotnet="${GRPC_DOTNET_COMMIT}" \
105        -a ci_gitCommit_go="${GRPC_GO_COMMIT}" \
106        -a ci_gitCommit_java="${GRPC_JAVA_COMMIT}" \
107        -a ci_gitActualCommit="${KOKORO_GIT_COMMIT}" \
108        --prefix="${LOAD_TEST_PREFIX}" -u "${UNIQUE_IDENTIFIER}" -u "${pool}" \
109        -a pool="${pool}" --category=scalable \
110        --allow_client_language=c++ --allow_server_language=c++ \
111        -o "loadtest_with_prebuilt_workers_${pool}.yaml"
112}
113
114# Add languages
115declare -a configLangArgs8core=()
116declare -a configLangArgs32core=()
117declare -a runnerLangArgs=()
118
119# c++
120configLangArgs8core+=( -l c++ )
121configLangArgs32core+=( -l c++ )
122runnerLangArgs+=( -l "cxx:${GRPC_CORE_REPO}:${GRPC_CORE_COMMIT}" )
123
124# dotnet
125configLangArgs8core+=( -l dotnet )
126configLangArgs32core+=( -l dotnet )
127runnerLangArgs+=( -l "dotnet:${GRPC_DOTNET_REPO}:${GRPC_DOTNET_COMMIT}" )
128
129# go
130configLangArgs8core+=( -l go )
131configLangArgs32core+=( -l go )
132runnerLangArgs+=( -l "go:${GRPC_GO_REPO}:${GRPC_GO_COMMIT}" )
133
134# java
135configLangArgs8core+=( -l java )
136configLangArgs32core+=( -l java )
137runnerLangArgs+=( -l "java:${GRPC_JAVA_REPO}:${GRPC_JAVA_COMMIT}" )
138
139# python
140configLangArgs8core+=( -l python )  # 8-core only.
141runnerLangArgs+=( -l "python:${GRPC_CORE_REPO}:${GRPC_CORE_COMMIT}" )
142
143# ruby
144configLangArgs8core+=( -l ruby )  # 8-core only.
145runnerLangArgs+=( -l "ruby:${GRPC_CORE_REPO}:${GRPC_CORE_COMMIT}" )
146
147buildConfigs "${WORKER_POOL_8CORE}" "${BIGQUERY_TABLE_8CORE}" "${configLangArgs8core[@]}"
148buildConfigs "${WORKER_POOL_32CORE}" "${BIGQUERY_TABLE_32CORE}" "${configLangArgs32core[@]}"
149
150# Delete prebuilt images on exit.
151deleteImages() {
152    echo "deleting images on exit"
153    ../test-infra/bin/delete_prebuilt_workers \
154    -p "${PREBUILT_IMAGE_PREFIX}" \
155    -t "${UNIQUE_IDENTIFIER}"
156}
157trap deleteImages EXIT
158
159# Build and push prebuilt images for running tests.
160time ../test-infra/bin/prepare_prebuilt_workers "${runnerLangArgs[@]}" \
161    -p "${PREBUILT_IMAGE_PREFIX}" \
162    -t "${UNIQUE_IDENTIFIER}" \
163    -r "${ROOT_DIRECTORY_OF_DOCKERFILES}"
164
165# Run tests.
166time ../test-infra/bin/runner \
167    -i "loadtest_with_prebuilt_workers_${WORKER_POOL_8CORE}.yaml" \
168    -i "loadtest_with_prebuilt_workers_${WORKER_POOL_32CORE}.yaml" \
169    -log-url-prefix "${LOG_URL_PREFIX}" \
170    -polling-interval 5s \
171    -delete-successful-tests \
172    -c "${WORKER_POOL_8CORE}:2" -c "${WORKER_POOL_32CORE}:2" \
173    -o "runner/sponge_log.xml"
174