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