#!/bin/sh

if [ $# -ne 1 ]; then
    echo "Usage: $(basename $0) NUM_TABLES"
    exit 1;
fi

launchctl limit maxfiles
echo "mysqld open files: $(lsof -n | grep mysqld | wc -l)"

DB_NAME="mysql_descriptors_test"
mysql -u root -e "DROP DATABASE IF EXISTS $DB_NAME"
mysql -u root -e "CREATE DATABASE $DB_NAME"

for i in $(seq 1 $1); do
    mysql -u root $DB_NAME -e "CREATE TABLE tmp_$i (id INT UNSIGNED)"
    if [ $? -ne 0 ]; then
       echo "Failed creating table #$i"
       exit 1
    fi

    if [ $(expr $i % 100) -eq 0 ]; then
        echo Iteration: $i: $(mysql -u root -sse "show variables like 'open_files_limit'") mysqld open files: $(lsof -n | grep mysqld | wc -l)
    fi
done

exit 0;
