Skip to content

Instantly share code, notes, and snippets.

@whitehat101
Created March 25, 2015 05:54
Show Gist options
  • Save whitehat101/bc1bcf429fe811f5156d to your computer and use it in GitHub Desktop.
Save whitehat101/bc1bcf429fe811f5156d to your computer and use it in GitHub Desktop.
SQL Benchmarks with PHP / Ruckusing Migrations
<?php
class MicroQuery
{
public static function configure($configuration)
{
// configure ruckusing db adapter
self::$adapter = null;
}
public static function test($change, $query, $times = 10)
{
$adapter = self::adapter();
$adapter->start_transaction();
// Solo Benchmark
$solo_benchmark = self::bench($adapter, $query);
// Explanation
$explain = $adapter->execute("EXPLAIN $query");
// Base Bench
$base_benchmark = self::benchmark($test);
// Change/Migration
$change_benchmark = self::bench_fx(function() use ($change) {
$change(self::migration());
});
// Post Bench
$post_benchmark = self::benchmark($test);
$adapter->rollback_transaction();
}
private static function benchmark($query, $times)
{
$times = array();
$adapter = self::adapter();
for($iterations = 0; $iterations < $times; $iterations++) {
list($ret, $time) = $this->bench($adapter, $query);
array_push($times, $time);
}
$average = $times[0]; // reduce inject
return array($average, $times);
}
private static function bench($adapter, $query) {
$start = microtime(true);
$ret = $adapter->execute($query);
$stop = microtime(true);
return array($ret, $stop-$start);
}
private static function bench_fx($fx) {
$start = microtime(true);
$ret = $fx();
$stop = microtime(true);
return array($ret, $stop-$start);
}
// Ruckusing Anonymous Migration
private static $migration = null;
private static function migration()
{
if (is_null(self::$migration)) {
self::$migration = null; // self::adapter()->migration();
}
return self::$migration;
}
// Ruckusing DB Adapter
private static $adapter = null;
private static function adapter()
{
if (is_null(self::$adapter)) {
self::$adapter = self::discover_adapter();
}
return self::$adapter;
}
private static function discover_adapter()
{
return null;
}
}
<?php
// Ruckusing Configuration
MicroQuery::configure(array(
));
// Ruckusing "up" method, SQL, iterations
MicroQuery::test(function($ruckus) {
$ruckus->add_index($table_name, $column_name, $options = array());
}, <<<SQL
SELECT *;
SQL
, 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment