Look, this is just dumb.
I don't even know why I felt like writing this.
This script will keep a certain percentage of the tables in your MySQL database locked. It will lock the first N% of your database, and then move forward one table at a time, locking the next table in line and releasing the lock on the one at the end of the tail.
Why would someone use this script? For benchmarking some completely unrealistic scenario? To decrease database efficiency for no good reason? Who can say.
Note: you'll get to lock/release tables a lot more quickly if you run this script locally.
<?php $percentage_of_database_to_lock = .25; require('./shared/mysql.php'); $mysql = new MySQLWrapper(); $mysql->server_port = 'host:3306'; $mysql->database = 'some_db'; $mysql->username = 'some_user'; $mysql->password = 'some_pass'; $res = $mysql->Execute('SHOW TABLES'); $tables = array(); if ($res) { while ($row = mysql_fetch_array($res)) { $tables[] = $row[0]; } } $tables_to_lock = floor($percentage_of_database_to_lock * count($tables)); if ($tables_to_lock > 1) { $cycles = 0; $start_at = 0; while (true) { $query = 'LOCK TABLE'; $tables_so_far = 0; while ($tables_so_far < $tables_to_lock) { $next_index = $start_at + $tables_so_far; if($next_index >= count($tables)) { $next_index = $next_index - count($tables); } $query .= '`' . $tables[$next_index] . '` WRITE, '; $tables_so_far++; } $query = substr($query, 0, strlen($query) - 2); $mysql->Execute($query); $start_at++; if ($start_at > count($tables)) { $start_at = 0; print "Cycle " . ++$cycles . "\n"; } } } ?>
Dependencies: MySQLWrapper