| 1 | #!/usr/bin/env bash
 | 
| 2 | #
 | 
| 3 | # Synthetic test with 1000 regexes.
 | 
| 4 | #
 | 
| 5 | # Usage:
 | 
| 6 | #   benchmarks/regcomp-cache/run.sh <function name>
 | 
| 7 | #
 | 
| 8 | # Example:
 | 
| 9 | #   benchmarks/regcomp-cache/run.sh match-many
 | 
| 10 | 
 | 
| 11 | set -o nounset
 | 
| 12 | set -o pipefail
 | 
| 13 | set -o errexit
 | 
| 14 | 
 | 
| 15 | match-many() {
 | 
| 16 |   local num_pat=${1:-300}
 | 
| 17 |   local num_str=${2:-300}
 | 
| 18 |   local repeat=${3:-1}
 | 
| 19 | 
 | 
| 20 |   # 1 2 3
 | 
| 21 |   local repeat_str
 | 
| 22 |   repeat_str=$(seq $repeat)
 | 
| 23 | 
 | 
| 24 |   echo BASH_VERSION=${BASH_VERSION:-}
 | 
| 25 |   echo OILS_VERSION=${OILS_VERSION:-}
 | 
| 26 | 
 | 
| 27 |   declare -a REGEXES=()
 | 
| 28 |   for i in $(seq $num_pat); do
 | 
| 29 |     REGEXES[i]="$i?($i*)$i+"  # last char is modified with ? then * and +
 | 
| 30 |   done
 | 
| 31 | 
 | 
| 32 |   echo "${REGEXES[@]}"
 | 
| 33 | 
 | 
| 34 |   local num_yes=0
 | 
| 35 |   local num_tried=0
 | 
| 36 | 
 | 
| 37 |   for i in $(seq $num_str); do
 | 
| 38 |     local str="$i$i$i"  # 3 copies
 | 
| 39 |     for j in $(seq $num_pat); do
 | 
| 40 |       local re="${REGEXES[j]}"
 | 
| 41 |       for k in $repeat_str; do  # potentially use the cache more
 | 
| 42 |         if [[ $str =~ $re ]]; then
 | 
| 43 |           echo "string $str matches pattern $re - captured '${BASH_REMATCH[1]}'"
 | 
| 44 |           num_yes=$(( num_yes + 1 ))
 | 
| 45 |         fi
 | 
| 46 |         num_tried=$(( num_tried + 1 ))
 | 
| 47 |       done
 | 
| 48 |     done
 | 
| 49 |   done
 | 
| 50 | 
 | 
| 51 |   echo
 | 
| 52 |   echo "num_yes = $num_yes"
 | 
| 53 |   echo "num_tried = $num_tried"
 | 
| 54 | }
 | 
| 55 | 
 | 
| 56 | compare() {
 | 
| 57 |   # must do ./NINJA-config.sh first
 | 
| 58 | 
 | 
| 59 |   local bin=_bin/cxx-opt/osh
 | 
| 60 |   ninja $bin
 | 
| 61 | 
 | 
| 62 |   local dir=_tmp/regcomp-cache
 | 
| 63 |   mkdir -p $dir
 | 
| 64 | 
 | 
| 65 |   # with bash
 | 
| 66 |   { time $0 match-many "$@"; } >$dir/bash-stdout.txt 2>$dir/bash-time.txt
 | 
| 67 | 
 | 
| 68 |   # with OSH
 | 
| 69 |   { time $bin $0 match-many "$@"; } >$dir/osh-stdout.txt 2>$dir/osh-time.txt
 | 
| 70 | 
 | 
| 71 |   # should have equal output except for version
 | 
| 72 |   diff $dir/*-stdout.txt || true
 | 
| 73 | 
 | 
| 74 |   # show timings
 | 
| 75 |   head $dir/*-time.txt
 | 
| 76 | }
 | 
| 77 | 
 | 
| 78 | 
 | 
| 79 | "$@"
 | 
| 80 | 
 |