Skip to content

Instantly share code, notes, and snippets.

@rogierslag
Created June 3, 2014 12:03
Show Gist options
  • Save rogierslag/65882ec5b37b895679d8 to your computer and use it in GitHub Desktop.
Save rogierslag/65882ec5b37b895679d8 to your computer and use it in GitHub Desktop.
<?php
$db = new PDO('mysql:dbname=test;host=localhost','root','rogier');
$db->query('DROP TABLE test');
$db->query('CREATE TABLE IF NOT EXISTS `test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`intval` int(11) NOT NULL,
`textval` text NOT NULL,
`varcharval` varchar(128) NOT NULL,
`datetime` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1');
$st = $db->prepare("INSERT INTO test (intval,textval,varcharval,datetime) VALUES (:intval,:textval,:varcharval,NOW())");
$runCount = (int)$argv[1];
$inTransaction = strcasecmp($argv[2],'true')==0?true:false;
if ( $runCount % 1000 != 0 ) {
die('The run count should be a complete multiple of 1000! Aborting...');
}
echo sprintf('A total of %d operations will be performed.'."\n",$runCount);
echo sprintf('%s will be used for the operations'."\n\n\n",$inTransaction?'A transaction':'No transaction');
try {
for ( $i = 0; $i < $runCount; $i++) {
if ( $inTransaction && $i % 1000 == 0) {
$db->beginTransaction();
}
$st->bindValue(':intval',getIntval());
$st->bindValue(':textval',getTextval());
$st->bindValue(':varcharval',getVarcharval());
$st->execute();
if ( $inTransaction && $i % 1000 == 999) {
$db->commit();
echo sprintf('Completed a 1000 run loop.'."\n");
}
}
} catch ( PDOException $e) {
var_dump($e,$db);
$db->rollBack();
}
function getRandomChar() {
$possibles = 'qazxswedcvfrtgbnhyujmkilopQAZXSWEDCVFRTGBNHYUJMKILOP`~§±1!2@3#4$5%6^7&8*9(0)-_=+{}[]"|:\;\'?><,./';
return $possibles[mt_rand(0,strlen($possibles)-1)];
}
function getIntval() {
return mt_rand();
}
function getTextval() {
$s = '';
$j = pow(2,mt_rand(8,12));
for ( $i = 0; $i < $j; $i++ ) {
$s .= getRandomChar();
}
return $s;
}
function getVarcharval() {
$s = '';
$j = mt_rand(64,128);
for ( $i = 0; $i < $j; $i++ ) {
$s .= getRandomChar();
}
return $s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment