<commit_before>#!/bin/bash

SERVER='localhost'
PORT=11400
./beanstalkd -p $PORT >/dev/null 2>/dev/null &
bg=$!

sleep .1
if ! ps $bg >/dev/null; then
  echo "Could not start beanstalkd for testing, port $PORT is taken"
  exit 1
fi

clean_exit() {
  kill -9 $1
  rm .tmp_test
  exit $2
}

fgrep -v "#" $1 | nc -q 1 $SERVER $PORT > .tmp_test

# diff is "false" if they match
if diff $2 .tmp_test; then
  clean_exit $bg 0 2>/dev/null
fi

clean_exit $bg 1 2>/dev/null
<commit_msg>Make this script safer and more portable.<commit_after>#!/bin/sh

server=localhost
port=11400
tmpdir="$TMPDIR"
test -z "$tmpdir" && tmpdir=/tmp
tmpf="${tmpdir}/bnch$$"
nc='nc -q 1'

commands="$1"; shift
expected="$1"; shift

cleanup() {
    {
        test -z "$bpid" || kill -9 $bpid
        rm -f "$tmpf"
    } >/dev/null 2>&1
}

catch() {
    echo '' Interrupted
    exit 3
}

trap cleanup EXIT
trap catch HUP INT QUIT TERM

./beanstalkd -p $port >/dev/null 2>/dev/null &
bpid=$!

sleep .1
if ! ps $bpid >/dev/null; then
  echo "Could not start beanstalkd for testing, port $port is taken"
  exit 2
fi

# Run the test
fgrep -v "#" $commands | $nc $server $port > "$tmpf"

# Check the output
diff $expected "$tmpf" || exit 1


{'fraction_non_alphanumeric': 0.1571072319201995, 'fraction_numerical': 0.02826267664172901, 'mean_word_length': 3.08135593220339, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6736969', 'n_tokens_mistral': 503, 'n_tokens_neox': 473, 'n_words': 160}