1#!/bin/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.
15
16# This script generates a set of load test examples from templates.
17
18LOADTEST_CONFIG=tools/run_tests/performance/loadtest_config.py
19
20if (( $# < 1 )); then
21    echo "Usage: ${0} <output directory>" >&2
22    exit 1
23fi
24
25if [[ ! -x "${LOADTEST_CONFIG}" ]]; then
26    echo "${LOADTEST_CONFIG} not found." >&2
27    exit 1
28fi
29
30outputbasedir="${1}"
31
32mkdir -p "${outputbasedir}/templates"
33
34example_file() {
35    local scenario="${1}"
36    local suffix="${2}"
37    if [[ "${scenario#cpp_}" != "${scenario}" ]]; then
38        echo "cxx${suffix}"
39        return
40    fi
41    if [[ "${scenario#python_asyncio_}" != "${scenario}" ]]; then
42        echo "python_asyncio${suffix}"
43        return
44    fi
45    if [[ "${scenario#php7_protobuf_c_}" != "${scenario}" ]]; then
46        echo "php7_protobuf_c${suffix}"
47        return
48    fi
49    echo "${scenario%%_*}${suffix}"
50}
51
52example_language() {
53    local filename="${1}"
54    if [[ "${filename#cxx_}" != "${filename}" ]]; then
55        echo "c++"
56        return
57    fi
58    if [[ "${filename#python_asyncio_}" != "${filename}" ]]; then
59        echo "python_asyncio"
60        return
61    fi
62    if [[ "${filename#php7_protobuf_c_}" != "${filename}" ]]; then
63        echo "php7_protobuf_c"
64        return
65    fi
66    echo "${filename%%_*}"
67}
68
69scenarios=(
70    "cpp_generic_async_streaming_ping_pong_secure"
71    "csharp_protobuf_async_unary_ping_pong"
72    "go_generic_sync_streaming_ping_pong_secure"
73    "java_generic_async_streaming_ping_pong_secure"
74    "node_to_node_generic_async_streaming_ping_pong_secure"
75    "php7_protobuf_php_extension_to_cpp_protobuf_sync_unary_ping_pong"
76    "php7_protobuf_c_extension_to_cpp_protobuf_sync_unary_ping_pong"
77    "python_generic_sync_streaming_ping_pong"
78    "python_asyncio_generic_async_streaming_ping_pong"
79    "ruby_protobuf_sync_streaming_ping_pong"
80)
81
82# Basic examples are intended to be runnable _as is_, so substitution keys
83# are stripped. Fields can be inserted manually following the pattern of the
84# prebuilt examples.
85basic_example() {
86    local -r scenario="${1}"
87    local -r outputdir="${2}"
88    local -r outputfile="$(example_file "${scenario}" _example_loadtest.yaml)"
89    local -r language="$(example_language "${outputfile}")"
90    ${LOADTEST_CONFIG} \
91        -l "${language}" \
92        -t ./tools/run_tests/performance/templates/loadtest_template_basic_all_languages.yaml \
93        -s client_pool= -s server_pool= -s big_query_table= \
94        -s timeout_seconds=900 --prefix=examples -u basic -r "^${scenario}$" \
95        --allow_client_language=c++ --allow_server_language=c++ \
96        --allow_server_language=node \
97        -o "${outputdir}/${outputfile}"
98    echo "Created example: ${outputfile}"
99}
100
101# Prebuilt examples contain substitution keys, so must be processed before
102# running.
103prebuilt_example() {
104    local -r scenario="${1}"
105    local -r outputdir="${2}"
106    local -r outputfile="$(example_file "${scenario}" _example_loadtest_with_prebuilt_workers.yaml)"
107    local -r language="$(example_language "${outputfile}")"
108    ${LOADTEST_CONFIG} \
109        -l "${language}" \
110        -t ./tools/run_tests/performance/templates/loadtest_template_prebuilt_all_languages.yaml \
111        -s driver_pool="\${driver_pool}" -s driver_image="\${driver_image}" \
112        -s client_pool="\${workers_pool}" -s server_pool="\${workers_pool}" \
113        -s big_query_table="\${big_query_table}" -s timeout_seconds=900 \
114        -s prebuilt_image_prefix="\${prebuilt_image_prefix}" \
115        -s prebuilt_image_tag="\${prebuilt_image_tag}" --prefix=examples -u prebuilt \
116        -a pool="\${workers_pool}" -r "^${scenario}$" \
117        --allow_client_language=c++ --allow_server_language=c++ \
118        --allow_server_language=node \
119        -o "${outputdir}/${outputfile}"
120    echo "Created example: ${outputfile}"
121}
122
123for scenario in "${scenarios[@]}"; do
124    basic_example "${scenario}" "${outputbasedir}"
125done
126
127for scenario in "${scenarios[@]}"; do
128    prebuilt_example "${scenario}" "${outputbasedir}/templates"
129done
130