#!/bin/zsh
# Runs ractor_repro.rb under every condition below, one run of each in turn, ROUNDS times over.
# Interleaving matters: the crash rate depends on how busy the machine is, so conditions measured
# one after another in separate batches cannot be compared.
#
#   ./ractor_sweep.sh RUBY ROUNDS
RUBY=$1; ROUNDS=$2
DIR=${0:A:h}; LOG=$DIR/ractor_sweep.log
conditions=("class" "frozen?" "hash" "own_objects" "bind" "warm"
            "class 1" "class 2" "class 10" "class 50" "class 100" "class 250" "class 500" "class 1000")
typeset -A ok crashed other
for c in $conditions; do ok[$c]=0; crashed[$c]=0; other[$c]=0; done
for round in $(seq $ROUNDS); do
  for c in $conditions; do
    $RUBY $DIR/ractor_repro.rb ${=c} > $LOG 2>&1 &
    pid=$!; t=0; seen=0
    while kill -0 $pid 2>/dev/null && [ $t -lt 300 ]; do
      if grep -q -e "Segmentation fault" -e "SEGV received" $LOG; then seen=1; break; fi
      sleep 0.05; t=$((t+1))
    done
    if [ $seen = 1 ]; then kill -9 $pid 2>/dev/null; wait $pid 2>/dev/null; crashed[$c]=$((crashed[$c]+1))
    elif kill -0 $pid 2>/dev/null; then kill -9 $pid; wait $pid 2>/dev/null; other[$c]=$((other[$c]+1))
    else
      wait $pid; rc=$?
      if [ $rc = 0 ]; then ok[$c]=$((ok[$c]+1))
      elif grep -q -e "Segmentation fault" -e "SEGV received" $LOG; then crashed[$c]=$((crashed[$c]+1))
      else other[$c]=$((other[$c]+1)); fi
    fi
  done
done
for c in $conditions; do printf "%-14s runs=%d ok=%d crashed=%d other=%d\n" "$c" $ROUNDS ${ok[$c]} ${crashed[$c]} ${other[$c]}; done
