Skip to content

Instantly share code, notes, and snippets.

@Sebb767
Created December 2, 2015 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sebb767/38613b584358890b74f1 to your computer and use it in GitHub Desktop.
Save Sebb767/38613b584358890b74f1 to your computer and use it in GitHub Desktop.
Benchmark script for the medoo isAlive() function
<?php
# wait_timeout & interactive_timeout is set to 3 secs each
include("medoo.php");
$db = new medoo([
'database_type' => 'mysql',
'database_name' => 'mysql',
'server' => '127.0.0.1',
'username' => 'root',
'password' => '',
'charset' => 'utf8'
]);
// returns ms
function benchmark($fn, $callcount = 10000)
{
static $empty = null;
// check time for empty run
if($empty = null)
{
$empty = 0;
$empty = benchmark(function() {});
}
$start = microtime(true);
for($i = 0; $i < $callcount; $i++) $fn();
return ((microtime(true) - $start)/$callcount - $empty) // get time divided by the callcount and substract the empty run time
*1000; // s -> ms
}
echo "Testing time when connected ...\n", benchmark(function() use ($db) { $db->isAlive(); }), " ms ", $db->isAlive() ? "alive" : "dead", "\n";
// wait for the connection to timeout
echo "Connected, sleeping\n";
sleep(10);
// test the connection
echo "Testing time when disconnected ...\n", benchmark(function() use ($db) { $db->isAlive(); }), " ms ", $db->isAlive() ? "alive" : "dead", "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment