aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/test/stress_test.sh
blob: 1f7c9e124038ca59db333d22a2a71f9eea4a28d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env bash

# Copyright (c) 2020 Egor Tensin <Egor.Tensin@gmail.com>
# This file is part of the "math-server" project.
# For details, see https://github.com/egor-tensin/math-server.
# Distributed under the MIT License.

set -o errexit -o nounset -o pipefail

script_name="$( basename -- "${BASH_SOURCE[0]}" )"
readonly script_name
script_dir="$( dirname -- "${BASH_SOURCE[0]}" )"
script_dir="$( cd -- "$script_dir" && pwd )"
readonly script_dir

server_path=
client_path=
readonly stress_test_path="$script_dir/stress_test.py"
readonly server_port=16666
server_pid=

dump() {
    local msg
    for msg; do
        echo "$script_name: $msg"
    done
}

kill_server() {
    dump "Killing the server with PID $server_pid..."
    kill "$server_pid"
    dump "Waiting for the server to terminate..."
    wait "$server_pid"
    dump "Done"
}

run_server() {
    dump "Running the server..."
    "$server_path" --port "$server_port" &
    server_pid="$!"
    dump "Its PID is $server_pid"
    trap kill_server EXIT
}

run_stress_test() {
    dump "Running stress_test.py..."
    "$stress_test_path"         \
        --client "$client_path" \
        --port "$server_port"   \
        --processes 4           \
        --expressions 1000
}

script_usage() {
    echo "usage: $script_name SERVER_PATH CLIENT_PATH"
}

parse_args() {
    if [ "$#" -ne 2 ]; then
        script_usage >&2
        return 1
    fi
    server_path="$1"
    client_path="$2"
}

main() {
    parse_args "$@"
    run_server
    run_stress_test
}

main "$@"