From 7348f04e27975bc5fc004769883bf8e7d3fcbed0 Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Thu, 24 Nov 2011 16:32:18 +0100 Subject: [PATCH] Added a simple benchmark --- com.woltlab.wcf/coreObject.xml | 3 + com.woltlab.wcf/template/footer.tpl | 20 +++ .../lib/system/benchmark/Benchmark.class.php | 140 ++++++++++++++++++ .../statement/PreparedStatement.class.php | 11 +- 4 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 wcfsetup/install/files/lib/system/benchmark/Benchmark.class.php diff --git a/com.woltlab.wcf/coreObject.xml b/com.woltlab.wcf/coreObject.xml index 9fab9c63ba..6f75bc6d48 100644 --- a/com.woltlab.wcf/coreObject.xml +++ b/com.woltlab.wcf/coreObject.xml @@ -13,5 +13,8 @@ + + + diff --git a/com.woltlab.wcf/template/footer.tpl b/com.woltlab.wcf/template/footer.tpl index acf43d2c92..51ca1ca5a9 100644 --- a/com.woltlab.wcf/template/footer.tpl +++ b/com.woltlab.wcf/template/footer.tpl @@ -11,6 +11,26 @@
{include file=footerMenu}
+ +

Execution time: {@$__wcf->getBenchmark()->getExecutionTime()}s ({#($__wcf->getBenchmark()->getExecutionTime()-$__wcf->getBenchmark()->getQueryExecutionTime())/$__wcf->getBenchmark()->getExecutionTime()*100}% PHP, {#$__wcf->getBenchmark()->getQueryExecutionTime()/$__wcf->getBenchmark()->getExecutionTime()*100}% SQL) | SQL queries: {#$__wcf->getBenchmark()->getQueryCount()}

+ + + + + {event name='copyright'} diff --git a/wcfsetup/install/files/lib/system/benchmark/Benchmark.class.php b/wcfsetup/install/files/lib/system/benchmark/Benchmark.class.php new file mode 100644 index 0000000000..c9fa90279b --- /dev/null +++ b/wcfsetup/install/files/lib/system/benchmark/Benchmark.class.php @@ -0,0 +1,140 @@ + + * @package com.woltlab.wcf + * @subpackage system.benchmark + * @category Community Framework + */ +class Benchmark extends SingletonFactory { + const TYPE_SQL_QUERY = 1; + const TYPE_OTHER = 0; + + /** + * general benchmark start time + * @var integer + */ + protected $startTime = 0; + + /** + * benchmark items + * @var array + */ + protected $items = array(); + + /** + * number of executed sql queries + * @var integer + */ + protected $queryCount = 0; + + /** + * total sql query execution time + * @var unknown_type + */ + protected $queryTime = 0; + + /** + * Creates a new Benchmark object. + */ + protected function init() { + $this->startTime = self::getMicrotime(); + } + + /** + * Starts a benchmark. + * + * @param string $text + * @param integer $type + * @return integer index + */ + public function start($text, $type = self::TYPE_OTHER) { + $newIndex = count($this->items); + $this->items[$newIndex]['text'] = $text; + $this->items[$newIndex]['type'] = $type; + $this->items[$newIndex]['before'] = self::getMicrotime(); + $this->items[$newIndex]['start'] = self::compareMicrotimes($this->startTime, $this->items[$newIndex]['before']); + return $newIndex; + } + + /** + * Stops an active benchmark. + * + * @param integer $index + */ + public function stop($index = null) { + if ($index === null) { + $index = count($this->items) - 1; + } + + $this->items[$index]['after'] = self::getMicrotime(); + $this->items[$index]['use'] = self::compareMicrotimes($this->items[$index]['before'], $this->items[$index]['after']); + $this->items[$index]['end'] = self::compareMicrotimes($this->startTime, $this->items[$index]['after']); + if ($this->items[$index]['type'] == self::TYPE_SQL_QUERY) { + $this->queryCount++; + $this->queryTime += $this->items[$index]['use']; + } + } + + /** + * Returns the execution time. + * + * @return integer + */ + public function getExecutionTime() { + return $this->compareMicrotimes($this->startTime, self::getMicrotime()); + } + + /** + * Returns the sql query execution time + * + * @return integer + */ + public function getQueryExecutionTime() { + return $this->queryTime; + } + + /** + * Returns the number of executed sql queries. + * + * @return integer + */ + public function getQueryCount() { + return $this->queryCount; + } + + /** + * Returns the logged items. + * + * @return array + */ + public function getItems() { + return $this->items; + } + + /** + * Returns the current unix timestamp as a float. + * + * @return float unix timestamp + */ + protected static function getMicrotime() { + return microtime(true); + } + + /** + * Calculates the difference of two unix timestamps. + * + * @param float $startTime + * @param float $endTime + * @return float difference + */ + protected static function compareMicrotimes($startTime, $endTime) { + return round($endTime - $startTime, 4); + } +} diff --git a/wcfsetup/install/files/lib/system/database/statement/PreparedStatement.class.php b/wcfsetup/install/files/lib/system/database/statement/PreparedStatement.class.php index 4a18ffa8d0..8d649b1cf1 100644 --- a/wcfsetup/install/files/lib/system/database/statement/PreparedStatement.class.php +++ b/wcfsetup/install/files/lib/system/database/statement/PreparedStatement.class.php @@ -1,6 +1,7 @@ database->incrementQueryCount(); try { - if (!count($parameters)) return $this->pdoStatement->execute(); - return $this->pdoStatement->execute($parameters); + Benchmark::getInstance()->start($this->query, Benchmark::TYPE_SQL_QUERY); + + if (!count($parameters)) $result = $this->pdoStatement->execute(); + else $result = $this->pdoStatement->execute($parameters); + + Benchmark::getInstance()->stop(); + + return $result; } catch (\PDOException $e) { throw new DatabaseException('Could not execute prepared statement: '.$e->getMessage(), $this->database, $this); -- 2.20.1