xref: /aosp_15_r20/external/bc/scripts/benchmark.sh (revision 5a6e848804d15c18a0125914844ee4eb0bda4fcf)
1#! /bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2018-2024 Gavin D. Howard and contributors.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions are met:
9#
10# * Redistributions of source code must retain the above copyright notice, this
11#   list of conditions and the following disclaimer.
12#
13# * Redistributions in binary form must reproduce the above copyright notice,
14#   this list of conditions and the following disclaimer in the documentation
15#   and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27# POSSIBILITY OF SUCH DAMAGE.
28#
29
30# This script depends on the GNU time utility, but I am okay with that because
31# this script is only for maintainers.
32
33# Just print the usage and exit with an error. This can receive a message to
34# print.
35# @param 1  A message to print.
36usage() {
37	if [ $# -eq 1 ]; then
38		printf '%s\n\n' "$1"
39	fi
40	printf 'usage: %s [-n<runs>] [-p<pause>] dir benchmark...\n' "$0" 1>&2
41	printf '    -d will delete the generated benchmark(s).\n'
42	printf '    -n runs is how many runs to run the benchmark, default 10.\n'
43	printf '    -p pause is how many seconds to pause before running the benchmarks.\n'
44	printf '\n'
45	printf 'The fields are put in this order:\n'
46	printf '1.  Elapsed Time\n'
47	printf '2.  System Time\n'
48	printf '3.  User Time\n'
49	printf '4.  Max RSS\n'
50	printf '5.  Average RSS\n'
51	printf '6.  Average Total Memory Use\n'
52	printf '7.  Average Unshared Data\n'
53	printf '8.  Average Unshared Stack\n'
54	printf '9.  Average Shared Text\n'
55	printf '10. Major Page Faults\n'
56	printf '11. Minor Page Faults\n'
57	printf '12. Swaps\n'
58	printf '13. Involuntary Context Switches\n'
59	printf '14. Voluntary Context Switches\n'
60	printf '15. Inputs\n'
61	printf '16. Outputs\n'
62	printf '17. Signals Delivered\n'
63	exit 1
64}
65
66script="$0"
67scriptdir=$(dirname "$script")
68
69. "$scriptdir/functions.sh"
70
71runs=10
72pause=0
73delete=0
74
75# Process command-line arguments.
76while getopts "dn:p:" opt; do
77
78	case "$opt" in
79		d) delete=1 ;;
80		n) runs="$OPTARG" ;;
81		p) pause="$OPTARG" ;;
82		?) usage "Invalid option: $opt" ;;
83	esac
84
85done
86
87while [ "$#" -gt 0 ] && [ "$OPTIND" -gt 1 ]; do
88
89	OPTIND=$(bin/bc -e "$OPTIND - 1")
90	shift
91
92done
93
94if [ "$#" -lt 2 ]; then
95	usage "Not enough arguments"
96fi
97
98cd "$scriptdir/.."
99
100d="$1"
101shift
102check_d_arg "$d"
103
104benchmarks=""
105
106# Create the list of benchmarks from the arguments.
107while [ "$#" -gt 0 ]; do
108
109	if [ "$benchmarks" = "" ]; then
110		benchmarks="$1"
111	else
112		benchmarks="$benchmarks $1"
113	fi
114
115	shift
116done
117
118files=""
119
120# Create the list of files from the benchmarks.
121for b in $benchmarks; do
122
123	f=$(printf "benchmarks/%s/%s.txt" "$d" "$b")
124
125	if [ "$files" = "" ]; then
126		files="$f"
127	else
128		files="$files $f"
129	fi
130
131done
132
133if [ "$d" = "bc" ]; then
134	opts="-lq"
135	halt="halt"
136else
137	opts="-x"
138	halt="q"
139fi
140
141# Generate all of the benchmarks.
142for b in $benchmarks; do
143
144	if [ "$delete" -ne 0 ] || [ ! -f "./benchmarks/$d/$b.txt" ]; then
145		printf 'Benchmarking generation of benchmarks/%s/%s.txt...\n' "$d" "$b" >&2
146		printf '%s\n' "$halt" | /usr/bin/time -v bin/$d $opts "./benchmarks/$d/$b.$d" \
147			> "./benchmarks/$d/$b.txt"
148	fi
149
150done
151
152# We use this format to make things easier to use with ministat.
153format="%e %S %U %M %t %K %D %p %X %F %R %W %c %w %I %O %k"
154
155printf 'Benchmarking %s...\n' "$files" >&2
156
157if [ "$pause" -gt 0 ]; then
158	sleep "$pause"
159fi
160
161i=0
162
163# Run the benchmarks as many times as told to.
164while [ "$i" -lt "$runs" ]; do
165
166	printf '%s\n' "$halt" | /usr/bin/time -f "$format" bin/$d $opts $files 2>&1 > /dev/null
167
168	# Might as well use the existing bc.
169	i=$(printf '%s + 1\n' "$i" | bin/bc)
170
171done
172