You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
1.7 KiB
80 lines
1.7 KiB
<?php
|
|
|
|
require('common.php');
|
|
|
|
if ($argc < 2)
|
|
{
|
|
die('Syntax:'.PHP_EOL."\t".basename($argv[0]).' <generator-sql>'.PHP_EOL);
|
|
}
|
|
|
|
// Load the generator
|
|
$generator_code = file_get_contents($argv[1]);
|
|
|
|
if ($generator_code === false)
|
|
{
|
|
die('Could not read generator code.'.PHP_EOL);
|
|
}
|
|
|
|
if (strlen($generator_code) > 5 && substr($generator_code, 0, 5) == '<?php')
|
|
{
|
|
$generator_code = substr($generator_code, 5);
|
|
}
|
|
|
|
// Spawn workers
|
|
$children = [];
|
|
|
|
for ($i=0; $i<WORKERS; $i++)
|
|
{
|
|
// Reseed the rng for each worker so they don't all generate the same
|
|
// queries
|
|
srand((int)(microtime(true)*($i+1)));
|
|
$pid = pcntl_fork();
|
|
if ($pid == 0)
|
|
{
|
|
define('WORKER_NUMBER', $i);
|
|
break;
|
|
}
|
|
$children[] = $pid;
|
|
}
|
|
|
|
if (sizeof($children) == WORKERS)
|
|
{
|
|
echo "[Master] Start".PHP_EOL;
|
|
// Wait for all children to exit
|
|
while (isset($children) && sizeof($children))
|
|
{
|
|
$status = null;
|
|
$pid = pcntl_waitpid(0, $status);
|
|
$idx = array_search($pid, $children);
|
|
if ($idx !== null)
|
|
unset($children, $idx);
|
|
}
|
|
echo "[Master] Stop".PHP_EOL;
|
|
}
|
|
else
|
|
{
|
|
echo "[Worker ".WORKER_NUMBER."] Start".PHP_EOL;
|
|
// Connect to database and open log file
|
|
$pdo = new PDO('pgsql:host='.DB_HOST.';port='.DB_PORT.';dbname='.DB_DATABASE, DB_USERNAME, DB_PASSWORD);
|
|
$fp = fopen('worker.'.WORKER_NUMBER.'.csv', 'w');
|
|
fputcsv($fp, ['Query', 'Duration']);
|
|
$start = microtime(true);
|
|
|
|
while (microtime(true) < $start + DURATION)
|
|
{
|
|
// Generate a query, run it
|
|
$sql = eval($generator_code);
|
|
$queryStart = microtime(true);
|
|
$result = $pdo->query($sql);
|
|
$queryDuration = microtime(true) - $queryStart;
|
|
$result->closeCursor();
|
|
// Write result to log file
|
|
fputcsv($fp, [$sql, $queryDuration]);
|
|
}
|
|
|
|
fclose($fp);
|
|
echo "[Worker ".WORKER_NUMBER."] Stop".PHP_EOL;
|
|
}
|
|
|
|
|
|
|
|
|