Created
December 2, 2015 13:39
-
-
Save Sebb767/38613b584358890b74f1 to your computer and use it in GitHub Desktop.
Benchmark script for the medoo isAlive() function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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