* set up some query profiling
This commit is contained in:
Sarjuuk
2020-05-28 15:15:19 +02:00
parent c290f845d6
commit d22e90ca85
3 changed files with 54 additions and 0 deletions

View File

@@ -15,6 +15,8 @@ class DB
private static $optionsCache = []; private static $optionsCache = [];
private static $connectionCache = []; private static $connectionCache = [];
private static $logs = [];
private static function createConnectSyntax(&$options) private static function createConnectSyntax(&$options)
{ {
return 'mysqli://'.$options['user'].':'.$options['pass'].'@'.$options['host'].'/'.$options['db']; return 'mysqli://'.$options['user'].':'.$options['pass'].'@'.$options['host'].'/'.$options['db'];
@@ -58,6 +60,35 @@ class DB
exit; exit;
} }
public static function logger($self, $query, $trace)
{
if ($trace) // actual query
self::$logs[] = [substr(str_replace("\n", ' ', $query), 0, 200)];
else // the statistics
{
end(self::$logs);
self::$logs[key(self::$logs)][] = substr(explode(';', $query)[0], 5);
}
}
public static function getLogs()
{
$out = '<pre><table style="font-size:12;"><tr><th></th><th>Time</th><th>Query</th></tr>';
foreach (self::$logs as $i => [$l, $t])
{
$c = 'inherit';
preg_match('/(\d+)/', $t, $m);
if ($m[1] > 100)
$c = '#FFA0A0';
else if ($m[1] > 20)
$c = '#FFFFA0';
$out .= '<tr><td>'.$i.'.</td><td style="background-color:'.$c.';">'.$t.'</td><td>'.$l.'</td></tr>';
}
return Util::jsEscape($out).'</table></pre>';
}
public static function getDB($idx) public static function getDB($idx)
{ {
return self::$interfaceCache[$idx]; return self::$interfaceCache[$idx];

View File

@@ -230,6 +230,20 @@ if (!CLI)
if (!empty($AoWoWconf['aowow']) && User::init()) if (!empty($AoWoWconf['aowow']) && User::init())
User::save(); // save user-variables in session User::save(); // save user-variables in session
// set up some logging (~10 queries will execute before we init the user and load the config)
if (CFG_DEBUG && User::isInGroup(U_GROUP_DEV | U_GROUP_ADMIN))
{
DB::Aowow()->setLogger(['DB', 'logger']);
DB::World()->setLogger(['DB', 'logger']);
if (DB::isConnected(DB_AUTH))
DB::Auth()->setLogger(['DB', 'logger']);
if (!empty($AoWoWconf['characters']))
foreach ($AoWoWconf['characters'] as $idx => $__)
if (DB::isConnected(DB_CHARACTERS . $idx))
DB::Characters($idx)->setLogger(['DB', 'logger']);
}
// hard-override locale for this call (should this be here..?) // hard-override locale for this call (should this be here..?)
// all strings attached.. // all strings attached..
if (!empty($AoWoWconf['aowow'])) if (!empty($AoWoWconf['aowow']))

View File

@@ -32,5 +32,14 @@ endif;
</noscript> </noscript>
<script type="text/javascript">DomContentLoaded.now()</script> <script type="text/javascript">DomContentLoaded.now()</script>
<?php
if (CFG_DEBUG && User::isInGroup(U_GROUP_DEV | U_GROUP_ADMIN)):
?>
<script type="text/javascript">
window.open("/", "SqlLog", "width=1800,height=200,top=100,left=100,status=no,location=no,toolbar=no,menubar=no").document.write('<?=DB::getLogs();?>');
</script>
<?php
endif;
?>
</body> </body>
</html> </html>