• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#! /bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2018-2023 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 '    -n runs is how many runs to run the benchmark, default 10.\n'
42	printf '    -p pause is how many seconds to pause before running the benchmarks.\n'
43	printf '\n'
44	printf 'The fields are put in this order:\n'
45	printf '1.  Elapsed Time\n'
46	printf '2.  System Time\n'
47	printf '3.  User Time\n'
48	printf '4.  Max RSS\n'
49	printf '5.  Average RSS\n'
50	printf '6.  Average Total Memory Use\n'
51	printf '7.  Average Unshared Data\n'
52	printf '8.  Average Unshared Stack\n'
53	printf '9.  Average Shared Text\n'
54	printf '10. Major Page Faults\n'
55	printf '11. Minor Page Faults\n'
56	printf '12. Swaps\n'
57	printf '13. Involuntary Context Switches\n'
58	printf '14. Voluntary Context Switches\n'
59	printf '15. Inputs\n'
60	printf '16. Outputs\n'
61	printf '17. Signals Delivered\n'
62	exit 1
63}
64
65script="$0"
66scriptdir=$(dirname "$script")
67
68. "$scriptdir/functions.sh"
69
70runs=10
71pause=0
72
73# Process command-line arguments.
74while getopts "n:p:" opt; do
75
76	case "$opt" in
77		n) runs="$OPTARG" ;;
78		p) pause="$OPTARG" ;;
79		?) usage "Invalid option: $opt" ;;
80	esac
81
82done
83
84while [ "$#" -gt 0 ] && [ "$OPTIND" -gt 1 ]; do
85
86	OPTIND=$(bin/bc -e "$OPTIND - 1")
87	shift
88
89done
90
91if [ "$#" -lt 2 ]; then
92	usage "Not enough arguments"
93fi
94
95cd "$scriptdir/.."
96
97d="$1"
98shift
99check_d_arg "$d"
100
101benchmarks=""
102
103# Create the list of benchmarks from the arguments.
104while [ "$#" -gt 0 ]; do
105
106	if [ "$benchmarks" = "" ]; then
107		benchmarks="$1"
108	else
109		benchmarks="$benchmarks $1"
110	fi
111
112	shift
113done
114
115files=""
116
117# Create the list of files from the benchmarks.
118for b in $benchmarks; do
119
120	f=$(printf "benchmarks/%s/%s.txt" "$d" "$b")
121
122	if [ "$files" = "" ]; then
123		files="$f"
124	else
125		files="$files $f"
126	fi
127
128done
129
130if [ "$d" = "bc" ]; then
131	opts="-lq"
132	halt="halt"
133else
134	opts="-x"
135	halt="q"
136fi
137
138# Generate all of the benchmarks.
139for b in $benchmarks; do
140
141	if [ ! -f "./benchmarks/$d/$b.txt" ]; then
142		printf 'Benchmarking generation of benchmarks/%s/%s.txt...\n' "$d" "$b" >&2
143		printf '%s\n' "$halt" | /usr/bin/time -v bin/$d $opts "./benchmarks/$d/$b.$d" \
144			> "./benchmarks/$d/$b.txt"
145	fi
146done
147
148# We use this format to make things easier to use with ministat.
149format="%e %S %U %M %t %K %D %p %X %F %R %W %c %w %I %O %k"
150
151printf 'Benchmarking %s...\n' "$files" >&2
152
153if [ "$pause" -gt 0 ]; then
154	sleep "$pause"
155fi
156
157i=0
158
159# Run the benchmarks as many times as told to.
160while [ "$i" -lt "$runs" ]; do
161
162	printf '%s\n' "$halt" | /usr/bin/time -f "$format" bin/$d $opts $files 2>&1 > /dev/null
163
164	# Might as well use the existing bc.
165	i=$(printf '%s + 1\n' "$i" | bin/bc)
166
167done
168