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# 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 benchmarks-prod2 \
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
37fi
38# Use the "official" BQ tables so that the measurements will show up in the
39# "official" public dashboard.
40BIGQUERY_TABLE_8CORE=e2e_benchmarks.ci_master_results_8core
41BIGQUERY_TABLE_32CORE=e2e_benchmarks.ci_master_results_32core
42# END differentiate experimental configuration from master configuration.
43CLOUD_LOGGING_URL="https://source.cloud.google.com/results/invocations/${KOKORO_BUILD_ID}"
44PREBUILT_IMAGE_PREFIX="gcr.io/grpc-testing/e2etest/prebuilt/${LOAD_TEST_PREFIX}"
45UNIQUE_IDENTIFIER="$(date +%Y%m%d%H%M%S)"
46ROOT_DIRECTORY_OF_DOCKERFILES="../test-infra/containers/pre_built_workers/"
47# Head of the workspace checked out by Kokoro.
48GRPC_GITREF="$(git show --format="%H" --no-patch)"
49# Prebuilt workers for core languages are always built from grpc/grpc.
50if [[ "${KOKORO_GITHUB_COMMIT_URL%/*}" == "https://github.com/grpc/grpc/commit" ]]; then
51    GRPC_CORE_GITREF="${KOKORO_GIT_COMMIT}"
52else
53    GRPC_CORE_GITREF="$(git ls-remote -h https://github.com/grpc/grpc.git master | cut -f1)"
54fi
55GRPC_DOTNET_GITREF="$(git ls-remote -h https://github.com/grpc/grpc-dotnet.git master | cut -f1)"
56GRPC_GO_GITREF="$(git ls-remote -h https://github.com/grpc/grpc-go.git master | cut -f1)"
57GRPC_JAVA_GITREF="$(git ls-remote -h https://github.com/grpc/grpc-java.git master | cut -f1)"
58# Kokoro jobs run on dedicated pools.
59DRIVER_POOL=drivers-ci
60WORKER_POOL_8CORE=workers-c2-8core-ci
61# c2-standard-30 is the closest machine spec to 32 core there is
62WORKER_POOL_32CORE=workers-c2-30core-ci
63# Prefix for log URLs in cnsviewer.
64LOG_URL_PREFIX="http://cnsviewer/placer/prod/home/kokoro-dedicated/build_artifacts/${KOKORO_BUILD_ARTIFACTS_SUBDIR}/github/grpc/"
65
66# Clone test-infra repository and build all tools.
67pushd ..
68git clone https://github.com/grpc/test-infra.git
69cd test-infra
70# Tools are built from HEAD.
71git checkout --detach
72make all-tools
73popd
74
75# Build test configurations.
76buildConfigs() {
77    local -r pool="$1"
78    local -r table="$2"
79    shift 2
80    tools/run_tests/performance/loadtest_config.py "$@" \
81        -t ./tools/run_tests/performance/templates/loadtest_template_prebuilt_all_languages.yaml \
82        -s driver_pool="${DRIVER_POOL}" -s driver_image= \
83        -s client_pool="${pool}" -s server_pool="${pool}" \
84        -s big_query_table="${table}" -s timeout_seconds=900 \
85        -s prebuilt_image_prefix="${PREBUILT_IMAGE_PREFIX}" \
86        -s prebuilt_image_tag="${UNIQUE_IDENTIFIER}" \
87        -a ci_buildNumber="${KOKORO_BUILD_NUMBER}" \
88        -a ci_buildUrl="${CLOUD_LOGGING_URL}" \
89        -a ci_jobName="${KOKORO_JOB_NAME}" \
90        -a ci_gitCommit="${GRPC_GITREF}" \
91        -a ci_gitCommit_go="${GRPC_GO_GITREF}" \
92        -a ci_gitCommit_java="${GRPC_JAVA_GITREF}" \
93        -a ci_gitActualCommit="${KOKORO_GIT_COMMIT}" \
94        --prefix="${LOAD_TEST_PREFIX}" -u "${UNIQUE_IDENTIFIER}" -u "${pool}" \
95        -a pool="${pool}" --category=scalable \
96        --allow_client_language=c++ --allow_server_language=c++ \
97        -o "loadtest_with_prebuilt_workers_${pool}.yaml"
98}
99
100buildConfigs "${WORKER_POOL_8CORE}" "${BIGQUERY_TABLE_8CORE}" -l c++ -l dotnet -l go -l java -l python -l ruby
101buildConfigs "${WORKER_POOL_32CORE}" "${BIGQUERY_TABLE_32CORE}" -l c++ -l dotnet -l go -l java
102
103# Delete prebuilt images on exit.
104deleteImages() {
105    echo "deleting images on exit"
106    ../test-infra/bin/delete_prebuilt_workers \
107    -p "${PREBUILT_IMAGE_PREFIX}" \
108    -t "${UNIQUE_IDENTIFIER}"
109}
110trap deleteImages EXIT
111
112# Build and push prebuilt images for running tests.
113time ../test-infra/bin/prepare_prebuilt_workers \
114    -l "cxx:${GRPC_CORE_GITREF}" \
115    -l "dotnet:${GRPC_DOTNET_GITREF}" \
116    -l "go:${GRPC_GO_GITREF}" \
117    -l "java:${GRPC_JAVA_GITREF}" \
118    -l "python:${GRPC_CORE_GITREF}" \
119    -l "ruby:${GRPC_CORE_GITREF}" \
120    -p "${PREBUILT_IMAGE_PREFIX}" \
121    -t "${UNIQUE_IDENTIFIER}" \
122    -r "${ROOT_DIRECTORY_OF_DOCKERFILES}"
123
124# Run tests.
125time ../test-infra/bin/runner \
126    -i "loadtest_with_prebuilt_workers_${WORKER_POOL_8CORE}.yaml" \
127    -i "loadtest_with_prebuilt_workers_${WORKER_POOL_32CORE}.yaml" \
128    -log-url-prefix "${LOG_URL_PREFIX}" \
129    -polling-interval 5s \
130    -delete-successful-tests \
131    -c "${WORKER_POOL_8CORE}:2" -c "${WORKER_POOL_32CORE}:2" \
132    -o "runner/sponge_log.xml"
133